/** * The tree mode of the Tasks page: the SAME unified model rendered as the * classic indentation tree — nested children on a hairline thread, the plugin's * row metrics (12px title / 11px mono meta), the left accent bar on the current * session, dashed fold rows, and the shared bottom-right control cluster (view * toggle + fold toggle) so switching back is always possible. * * Behaviour contract: `role="tree"` / `role="treeitem"` + `aria-level`, a * `[data-tasks-row]` focus order, ArrowUp / ArrowDown / Home / End focus moves, * Enter / Space activation, and the global fold aggregate (a fold row toggles * the whole page's fold state). * * Readability: rows sit on the page's 32px rhythm (a 2px accent bar marks the * current session, hover is the native fill, settled rows recede), and every * line that can be ellipsised carries its full text as `title` — the label, the * workflow name and the fold previews. */ import { useCallback, useMemo, useRef, type KeyboardEvent, type ReactNode } from 'react' import clsx from 'clsx' import { IconLoadingOutlineRegular, StateDot } from '@deepseek-ai/dsh-client-ui-primitives' import type { TasksAgentNode, TasksNode, TasksWorkflowNode } from './tasks-model.ts' import { AgentGlyph, agentMeta, FoldGlyph, foldPreviews, foldTally, LiveLine, nodeDotState, TaskLine, WorkflowGlyph, workflowMeta, } from './tasks-shared.tsx' import { FoldToggleButton, ViewModeToggle } from './TasksGraph.tsx' import { t } from './locales.ts' import css from './tasks-graph.module.css' export interface TasksTreeProps { nodes: readonly TasksNode[] folded: boolean onNodeInfo(node: TasksAgentNode, anchor: HTMLElement): void onWorkflowInfo(node: TasksWorkflowNode, anchor: HTMLElement): void /** Open the shared task window for one task id. */ onOpenTask(taskId: string, anchor: HTMLElement): void onToggleFold(): void mode: 'graph' | 'tree' onModeChange(mode: 'graph' | 'tree'): void /** Fallback loading row while the root catalog hydrates. */ loading?: boolean } export function TasksTree(props: TasksTreeProps): ReactNode { const { nodes, folded, onNodeInfo, onWorkflowInfo, onOpenTask, onToggleFold, mode, onModeChange, loading, } = props const bodyRef = useRef(null) /** children by parent id (model order preserved). */ const childrenOf = useMemo(() => { const map = new Map() const ids = new Set(nodes.map(node => node.id)) for (const node of nodes) { const parent = node.parentId !== undefined && ids.has(node.parentId) ? node.parentId : '' const list = map.get(parent) if (list === undefined) map.set(parent, [node]) else list.push(node) } return map }, [nodes]) /** Arrow-key navigation over the visible rows (official catalog recipe). */ const focusAt = useCallback((index: number): void => { const items = bodyRef.current?.querySelectorAll('[data-tasks-row]') ?? [] if (items.length === 0) return items[(index + items.length) % items.length]?.focus() }, []) const onTreeKeyDown = useCallback((event: KeyboardEvent): void => { const items = bodyRef.current?.querySelectorAll('[data-tasks-row]') ?? [] const index = Array.prototype.indexOf.call(items, document.activeElement) if (event.key === 'ArrowDown') { event.preventDefault() focusAt(index + 1) } else if (event.key === 'ArrowUp') { event.preventDefault() focusAt(index < 0 ? items.length - 1 : index - 1) } else if (event.key === 'Home') { event.preventDefault() focusAt(0) } else if (event.key === 'End') { event.preventDefault() focusAt(items.length - 1) } }, [focusAt]) const activateOnKey = (event: KeyboardEvent, action: () => void): void => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault() event.stopPropagation() action() } } /** One row plus its nested children (the connector line is drawn by * `.treeKids` in tasks-graph.module.css). */ const renderNode = (node: TasksNode, depth: number): ReactNode => { const children = childrenOf.get(node.id) ?? [] const row = node.kind === 'fold' ? (
{ activateOnKey(event, onToggleFold) }} > {foldTally(node)} {t(folded ? 'tasksFoldExpand' : 'tasksFoldCollapse')} {foldPreviews(node.previews, node.count)}
) : node.kind === 'workflow' ? (
{ onWorkflowInfo(node, event.currentTarget) }} onKeyDown={(event) => { activateOnKey(event, () => { onWorkflowInfo(node, event.currentTarget as HTMLElement) }) }} > {node.run.name} {workflowMeta(node)}
) : (
{ onNodeInfo(node, event.currentTarget) }} onKeyDown={(event) => { activateOnKey(event, () => { onNodeInfo(node, event.currentTarget as HTMLElement) }) }} > {node.label} {agentMeta(node)} {node.state === 'running' && }
) if (children.length === 0) return
{row}
return (
{row}
{children.map(child => renderNode(child, depth + 1))}
) } const roots = childrenOf.get('') ?? [] return (
{loading === true && roots.length === 0 && (
{t('loading')}
)} {roots.map(root => renderNode(root, 0))}
) }