Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(sessions): reorder sessions in the sidebar by drag-drop
enesccd4714b74e5
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex 92d42f18e..58698506e 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -108,6 +108,7 @@ import {flowToNodeStates,groupSelectedNodes,nodeStatesToFlow,+ reorderNodeBefore,reparentNode,ungroupNodes,type CanvasNode@@ -1920,6 +1921,21 @@ export function Canvas() {[activeProjectId, setNodes, markDirty, writeDisk])+ // Sidebar reorder: place draggedId immediately before beforeId (sidebar order = node order),+ // joining the target's container if they differ.+ const reorderSession = useCallback(+ (projectId: string, draggedId: string, beforeId: string) => {+ if (projectId === activeProjectId) {+ setNodes((ns) => reorderNodeBefore(ns, draggedId, beforeId))+ markDirty()+ } else {+ useProjects.getState().reorderNode(projectId, draggedId, beforeId)+ void writeDisk()+ }+ },+ [activeProjectId, setNodes, markDirty, writeDisk]+ )+const onRowContextMenu = useCallback((e: React.MouseEvent, projectId: string, id: string) => {e.preventDefault()@@ -2450,6 +2466,7 @@ export function Canvas() {onAiNameSession={aiNameSession}onAiNameGroup={aiNameGroup}onMoveToGroup={moveSessionToGroup}+ onReorder={reorderSession}onRowContextMenu={onRowContextMenu}onAddToProject={addToProject}onMouseEnter={openSessionsPeek}diff --git a/src/renderer/components/SessionsSidebar.tsx b/src/renderer/components/SessionsSidebar.tsxindex f1b06e679..46ede2dc0 100644--- a/src/renderer/components/SessionsSidebar.tsx+++ b/src/renderer/components/SessionsSidebar.tsx@@ -43,6 +43,8 @@ export interface SessionsSidebarProps {onMoveToGroup(projectId: string, nodeId: string, groupId: string | null): void/** Name a canvas group with AI from its member terminals' output. */onAiNameGroup(projectId: string, groupId: string, memberIds: string[], cwd?: string): void | Promise<void>+ /** Reorder a session to sit immediately before another (within/across containers). */+ onReorder(projectId: string, draggedId: string, beforeId: string): voidonMouseEnter?(): voidonMouseLeave?(): void}@@ -127,22 +129,48 @@ export function SessionsSidebar(props: SessionsSidebarProps): JSX.Element | nullconst dropClass = (projectId: string, groupId: string | null): string =>dropKey === dropTargetKey(projectId, groupId) ? ' is-drop-target' : ''- const renderRow = (projectId: string, row: (typeof groups)[number]['ungrouped'][number]): JSX.Element => (- <SessionRow- key={row.id}- row={row}- onClick={() => props.onFocusNode(row.id)}- onClose={() => props.onCloseSession(projectId, row.id)}- onRename={(title) => props.onRenameSession(projectId, row.id, title)}- onAiName={() => props.onAiNameSession(projectId, row.id, row.cwd)}- onContextMenu={(e) => props.onRowContextMenu(e, projectId, row.id)}- onDragStart={() => setDrag({ projectId, nodeId: row.id })}- onDragEnd={() => {- setDrag(null)- setDropKey(null)- }}- />- )+ // A row is both draggable and a drop target: dropping another row onto it reorders the+ // dragged session to sit immediately before this one. stopPropagation keeps the enclosing+ // group/ungrouped drop zone (which appends) from also firing.+ const renderRow = (projectId: string, row: (typeof groups)[number]['ungrouped'][number]): JSX.Element => {+ const rowKey = `row:${row.id}`+ const canDrop = !!drag && drag.projectId === projectId && drag.nodeId !== row.id+ return (+ <div+ key={row.id}+ className={`ss-rowdrop${dropKey === rowKey ? ' is-drop-before' : ''}`}+ onDragOver={(e) => {+ if (!canDrop) return+ e.preventDefault()+ e.stopPropagation()+ if (dropKey !== rowKey) setDropKey(rowKey)+ }}+ onDragLeave={() => setDropKey((k) => (k === rowKey ? null : k))}+ onDrop={(e) => {+ if (!canDrop) return+ e.preventDefault()+ e.stopPropagation()+ props.onReorder(projectId, drag.nodeId, row.id)+ setDrag(null)+ setDropKey(null)+ }}+ >+ <SessionRow+ row={row}+ onClick={() => props.onFocusNode(row.id)}+ onClose={() => props.onCloseSession(projectId, row.id)}+ onRename={(title) => props.onRenameSession(projectId, row.id, title)}+ onAiName={() => props.onAiNameSession(projectId, row.id, row.cwd)}+ onContextMenu={(e) => props.onRowContextMenu(e, projectId, row.id)}+ onDragStart={() => setDrag({ projectId, nodeId: row.id })}+ onDragEnd={() => {+ setDrag(null)+ setDropKey(null)+ }}+ />+ </div>+ )+ }if (!open) return nulldiff --git a/src/renderer/state/projects.sessions.test.ts b/src/renderer/state/projects.sessions.test.tsindex 52b9b20fe..e4a3171f7 100644--- a/src/renderer/state/projects.sessions.test.ts+++ b/src/renderer/state/projects.sessions.test.ts@@ -109,3 +109,49 @@ describe('moveNodeToGroup', () => {expect(useProjects.getState().getProject('p1')!.nodes).toBe(before)})})++describe('reorderNode', () => {+ const setup = (nodes: CanvasNodeState[]): void => {+ useProjects.setState({+ projects: [+ { id: 'p1', name: 'P1', color: '#111', viewport: { x: 0, y: 0, zoom: 1 }, nodes }+ ],+ activeProjectId: 'p1'+ })+ }+ const order = (): string[] =>+ useProjects.getState().getProject('p1')!.nodes.map((n) => n.id)++ it('moves a node to sit immediately before another in the same container', () => {+ setup([mkNode('a'), mkNode('b'), mkNode('c')])+ useProjects.getState().reorderNode('p1', 'c', 'a')+ expect(order()).toEqual(['c', 'a', 'b'])+ })++ it('joins the target container when reordering across groups', () => {+ const grp: CanvasNodeState = {+ id: 'g1',+ kind: 'group',+ position: { x: 50, y: 50 },+ size: { width: 400, height: 300 },+ title: 'g1',+ color: '#fff',+ group: null+ }+ const t1: CanvasNodeState = { ...mkNode('t1'), position: { x: 10, y: 10 }, parentId: 'g1' }+ const t2: CanvasNodeState = { ...mkNode('t2'), position: { x: 200, y: 150 } }+ setup([grp, t1, t2])+ useProjects.getState().reorderNode('p1', 't2', 't1')+ const out = useProjects.getState().getProject('p1')!.nodes.find((n) => n.id === 't2')!+ expect(out.parentId).toBe('g1')+ expect(out.position).toEqual({ x: 150, y: 100 })+ })++ it('is a no-op for same / missing ids', () => {+ setup([mkNode('a'), mkNode('b')])+ const before = useProjects.getState().getProject('p1')!.nodes+ useProjects.getState().reorderNode('p1', 'a', 'a')+ useProjects.getState().reorderNode('p1', 'nope', 'a')+ expect(useProjects.getState().getProject('p1')!.nodes).toBe(before)+ })+})diff --git a/src/renderer/state/projects.ts b/src/renderer/state/projects.tsindex 4836ea34d..c5a500c8f 100644--- a/src/renderer/state/projects.ts+++ b/src/renderer/state/projects.ts@@ -27,12 +27,38 @@ interface ProjectsState {/** Moves a node into a group frame (groupId) or out to the top level (null), keeping its* on-canvas position fixed by converting absolute/relative coordinates. */moveNodeToGroup(projectId: string, nodeId: string, groupId: string | null): void+ /** Reorders a node to sit immediately before another (sidebar order = array order),+ * joining the target's container if they differ. */+ reorderNode(projectId: string, draggedId: string, beforeId: string): void/** Removes a project; returns the id that should become active (never deletes the last one). */deleteProject(id: string): stringtoWorkspace(): Workspace}+/** Returns `node` repositioned for a new parent (groupId, or null for top level), keeping its+ * on-canvas position fixed (one-level absolute↔relative). Unchanged if the target is not a+ * group. `extent` is omitted — nodeStatesToFlow re-derives it from parentId on load. */+function repositionState(+ node: CanvasNodeState,+ groupId: string | null,+ nodes: CanvasNodeState[]+): CanvasNodeState {+ const oldParent = node.parentId ? nodes.find((n) => n.id === node.parentId) : undefined+ const abs = {+ x: node.position.x + (oldParent?.position.x ?? 0),+ y: node.position.y + (oldParent?.position.y ?? 0)+ }+ if (groupId === null) return { ...node, parentId: undefined, position: abs }+ const group = nodes.find((n) => n.id === groupId)+ if (!group || group.kind !== 'group') return node+ return {+ ...node,+ parentId: group.id,+ position: { x: abs.x - group.position.x, y: abs.y - group.position.y }+ }+}+/** Returns `projects` with one project's nodes transformed; other projects untouched. */function mapProjectNodes(projects: Project[],@@ -130,28 +156,32 @@ export const useProjects = create<ProjectsState>((set, get) => ({const node = nodes.find((n) => n.id === nodeId)if (!node || node.kind === 'group') return nodesif ((node.parentId ?? null) === groupId) return nodes- const oldParent = node.parentId ? nodes.find((n) => n.id === node.parentId) : undefined- const abs = {- x: node.position.x + (oldParent?.position.x ?? 0),- y: node.position.y + (oldParent?.position.y ?? 0)- }- let next: CanvasNodeState- if (groupId === null) {- next = { ...node, parentId: undefined, position: abs }- } else {- const group = nodes.find((n) => n.id === groupId)- if (!group || group.kind !== 'group') return nodes- next = {- ...node,- parentId: group.id,- position: { x: abs.x - group.position.x, y: abs.y - group.position.y }- }- }+ const next = repositionState(node, groupId, nodes)+ if (next === node) return nodes // target group missing / not a groupreturn nodes.map((n) => (n.id === nodeId ? next : n))})}))},+ reorderNode(projectId, draggedId, beforeId) {+ set((s) => ({+ projects: mapProjectNodes(s.projects, projectId, (nodes) => {+ if (draggedId === beforeId) return nodes+ const dragged = nodes.find((n) => n.id === draggedId)+ const before = nodes.find((n) => n.id === beforeId)+ if (!dragged || !before || dragged.kind === 'group') return nodes+ const targetParent = before.parentId ?? null+ const moved =+ (dragged.parentId ?? null) === targetParent+ ? dragged+ : repositionState(dragged, targetParent, nodes)+ const without = nodes.filter((n) => n.id !== draggedId)+ const idx = without.findIndex((n) => n.id === beforeId)+ return [...without.slice(0, idx), moved, ...without.slice(idx)]+ })+ }))+ },+deleteProject(id) {const { projects, activeProjectId } = get()const index = projects.findIndex((p) => p.id === id)diff --git a/src/renderer/state/workspace.test.ts b/src/renderer/state/workspace.test.tsindex 78aeabdbe..b6dc76f6d 100644--- a/src/renderer/state/workspace.test.ts+++ b/src/renderer/state/workspace.test.ts@@ -1,5 +1,11 @@import { describe, it, expect } from 'vitest'-import { createDinoNode, flowToNodeStates, nodeStatesToFlow, reparentNode } from './workspace'+import {+ createDinoNode,+ flowToNodeStates,+ nodeStatesToFlow,+ reorderNodeBefore,+ reparentNode+} from './workspace'import type { CanvasNode } from './workspace'const term = (id: string, pos: { x: number; y: number }, parentId?: string): CanvasNode =>@@ -60,6 +66,44 @@ describe('reparentNode', () => {})})+describe('reorderNodeBefore', () => {+ const ids = (out: CanvasNode[]): string[] => out.filter((n) => n.type !== 'group').map((n) => n.id)++ it('reorders within the same container (moves dragged before target)', () => {+ const nodes = [term('a', { x: 0, y: 0 }), term('b', { x: 0, y: 0 }), term('c', { x: 0, y: 0 })]+ expect(ids(reorderNodeBefore(nodes, 'c', 'a'))).toEqual(['c', 'a', 'b'])+ expect(ids(reorderNodeBefore(nodes, 'a', 'c'))).toEqual(['b', 'a', 'c'])+ })++ it('keeps position unchanged for a same-container reorder', () => {+ const nodes = [term('a', { x: 5, y: 5 }), term('b', { x: 9, y: 9 })]+ const out = reorderNodeBefore(nodes, 'b', 'a')+ expect(out.find((n) => n.id === 'b')!.position).toEqual({ x: 9, y: 9 })+ })++ it('moves across containers (joins target group) and lands before the target', () => {+ const nodes = [+ grp('g1', { x: 50, y: 50 }),+ term('t1', { x: 10, y: 10 }, 'g1'),+ term('t2', { x: 200, y: 150 }) // ungrouped+ ]+ const out = reorderNodeBefore(nodes, 't2', 't1')+ const t2 = out.find((n) => n.id === 't2')!+ expect(t2.parentId).toBe('g1')+ expect(t2.position).toEqual({ x: 150, y: 100 }) // 200-50, 150-50+ expect(ids(out)).toEqual(['t2', 't1']) // t2 placed before t1+ })++ it('keeps group nodes first and is a no-op for same/ missing / group drags', () => {+ const nodes = [grp('g1', { x: 0, y: 0 }), term('a', { x: 0, y: 0 }), term('b', { x: 0, y: 0 })]+ expect(reorderNodeBefore(nodes, 'a', 'a')).toBe(nodes)+ expect(reorderNodeBefore(nodes, 'nope', 'a')).toBe(nodes)+ expect(reorderNodeBefore(nodes, 'g1', 'a')).toBe(nodes) // can't drag a group row+ const out = reorderNodeBefore(nodes, 'b', 'a')+ expect(out[0].id).toBe('g1')+ })+})+describe('group worktree serialization', () => {it('round-trips data.worktree on a group node', () => {const group = {diff --git a/src/renderer/state/workspace.ts b/src/renderer/state/workspace.tsindex b604df1f3..5bf49e967 100644--- a/src/renderer/state/workspace.ts+++ b/src/renderer/state/workspace.ts@@ -453,6 +453,39 @@ export function ungroupNodes(nodes: CanvasNode[], groupId: string): CanvasNode[]* before their children (React Flow requires parents first). No-op when the node is missing or* is itself a group, when it already has the requested parent, or when `groupId` is not a group.*/+/** Group (parent) nodes must precede their children in the array (React Flow requirement). */+function groupsFirst(nodes: CanvasNode[]): CanvasNode[] {+ return [...nodes.filter((n) => n.type === 'group'), ...nodes.filter((n) => n.type !== 'group')]+}++/**+ * Returns `node` repositioned for a new parent (`targetParentId`, or null for top level),+ * keeping its on-canvas position fixed via absolute↔relative conversion (one level). Returns+ * the node unchanged if the target group is missing or not a group.+ */+function repositionForParent(+ node: CanvasNode,+ targetParentId: string | null,+ nodes: CanvasNode[]+): CanvasNode {+ const oldParent = node.parentId ? nodes.find((n) => n.id === node.parentId) : undefined+ const abs = {+ x: node.position.x + (oldParent?.position.x ?? 0),+ y: node.position.y + (oldParent?.position.y ?? 0)+ }+ if (targetParentId === null) {+ return { ...node, parentId: undefined, extent: undefined, position: abs }+ }+ const group = nodes.find((n) => n.id === targetParentId)+ if (!group || group.type !== 'group') return node+ return {+ ...node,+ parentId: group.id,+ extent: 'parent' as const,+ position: { x: abs.x - group.position.x, y: abs.y - group.position.y }+ }+}+export function reparentNode(nodes: CanvasNode[],nodeId: string,@@ -462,28 +495,37 @@ export function reparentNode(if (!node || node.type === 'group') return nodesif ((node.parentId ?? null) === groupId) return nodes- const oldParent = node.parentId ? nodes.find((n) => n.id === node.parentId) : undefined- const abs = {- x: node.position.x + (oldParent?.position.x ?? 0),- y: node.position.y + (oldParent?.position.y ?? 0)- }-- let updated: CanvasNode- if (groupId === null) {- updated = { ...node, parentId: undefined, extent: undefined, position: abs }- } else {- const group = nodes.find((n) => n.id === groupId)- if (!group || group.type !== 'group') return nodes- updated = {- ...node,- parentId: group.id,- extent: 'parent' as const,- position: { x: abs.x - group.position.x, y: abs.y - group.position.y }- }- }+ const updated = repositionForParent(node, groupId, nodes)+ if (updated === node) return nodes // target group missing / not a group+ return groupsFirst(nodes.map((n) => (n.id === nodeId ? updated : n)))+}- const next = nodes.map((n) => (n.id === nodeId ? updated : n))- return [...next.filter((n) => n.type === 'group'), ...next.filter((n) => n.type !== 'group')]+/**+ * Moves `draggedId` to sit immediately before `beforeId` in the array (sidebar order follows+ * array order). The dragged node also joins `beforeId`'s container (same reposition math) so a+ * drop both reorders within a group and can move across groups. No-op when either node is+ * missing, they are the same, or the dragged node is a group.+ */+export function reorderNodeBefore(+ nodes: CanvasNode[],+ draggedId: string,+ beforeId: string+): CanvasNode[] {+ if (draggedId === beforeId) return nodes+ const dragged = nodes.find((n) => n.id === draggedId)+ const before = nodes.find((n) => n.id === beforeId)+ if (!dragged || !before || dragged.type === 'group') return nodes++ const targetParent = before.parentId ?? null+ const moved =+ (dragged.parentId ?? null) === targetParent+ ? dragged+ : repositionForParent(dragged, targetParent, nodes)++ const without = nodes.filter((n) => n.id !== draggedId)+ const idx = without.findIndex((n) => n.id === beforeId)+ const result = [...without.slice(0, idx), moved, ...without.slice(idx)]+ return groupsFirst(result)}/** Converts persisted node states into live React Flow nodes (parents first). */diff --git a/src/renderer/styles.css b/src/renderer/styles.cssindex 83ba7426e..5854a15e3 100644--- a/src/renderer/styles.css+++ b/src/renderer/styles.css@@ -4030,6 +4030,9 @@ body,.is-drop-target { border-color: var(--accent) !important; background: color-mix(in srgb, var(--accent) 14%, transparent); }.ss-row[draggable='true'] { cursor: grab; }.ss-row[draggable='true']:active { cursor: grabbing; }+/* Reorder: insertion line above the row the dragged session will land before */+.ss-rowdrop { position: relative; }+.ss-rowdrop.is-drop-before::before { content: ''; position: absolute; top: -1px; left: 6px; right: 6px; height: 2px; border-radius: 1px; background: var(--accent); pointer-events: none; }.sessions-icon-cluster { position: absolute; top: 54px; left: 14px; z-index: 11; }.sessions-icon-cluster button { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; background: rgba(40, 40, 42, 0.92); border: 1px solid var(--border); border-radius: 9px; color: var(--text); cursor: pointer; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); }