Files
apes-sidebar/src/client/TasksTree.tsx
T
Menghuan1918 84b68e9216 feat(tasks): 任务窗内容填满高度可自然拉伸;聚合卡在成员多时报出 +N
用户两条优化:
  1. 任务详细占满上下部分,不要留空(这样能自然拉伸)
  2. 思考如何优化内容很多时的待命和完成的样式

第 1 条:描述区原本是 `max-height: 160px` 的滚动盒,窗口拉大只长空白。
FloatingWindow 新增 `bodyLayout: 'scroll' | 'fill'`(默认 scroll 不变,后台
任务输出窗仍按原样流动),fill 把 body 变成列容器、让调用方的子节点自己决定
谁吃余量;任务窗据此把余量全给描述区(编辑态给多行输入框,`flex: 1` 跟着窗
口长),头部行 / 元信息 / 负责人 / 提示行都 `flex: none`,不会跟着漂。

第 2 条:一队队友很多时,聚合卡只有一行名字却塞了十几个人,卡上看不出还有
多少。名称行现在写「前两个名字 + N」—— 用的是团队任务行已有的同一个 `+N`
约定,不是第二套;分组总数另有徽标和小条两处,所以 `+N` 只是尾巴而不是唯一
计数。图卡片与树行共用同一个 foldPreviews。

测试:新增 2 例(窗口声明 fill 布局、描述区在 body 内且编辑态换成多行框;
五个待命成员的聚合卡名称是 `m1 / m2 +3`);floating-window 既有 9 例回归通过。
pnpm test 1288 passed / 9 skipped,typecheck / lint 通过。

(cherry picked from commit 62651e731d6e5fd0378e2ecc504792cb2e3eb461)
2026-09-27 22:19:05 +08:00

216 lines
8.5 KiB
TypeScript

/**
* 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<HTMLDivElement>(null)
/** children by parent id (model order preserved). */
const childrenOf = useMemo(() => {
const map = new Map<string, TasksNode[]>()
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<HTMLElement>('[data-tasks-row]') ?? []
if (items.length === 0) return
items[(index + items.length) % items.length]?.focus()
}, [])
const onTreeKeyDown = useCallback((event: KeyboardEvent<HTMLDivElement>): void => {
const items = bodyRef.current?.querySelectorAll<HTMLElement>('[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'
? (
<div
key={node.id}
data-tasks-row
role="treeitem"
tabIndex={0}
aria-level={depth + 1}
aria-label={`${foldTally(node)} · ${t('tasksFoldExpand')}`}
className={clsx(css.treeRow, css.treeFoldRow)}
onClick={onToggleFold}
onKeyDown={(event) => { activateOnKey(event, onToggleFold) }}
>
<span className={css.treeGlyph} aria-hidden="true"><FoldGlyph /></span>
<span className={css.treeContent}>
<span className={css.treeTitle}>
{foldTally(node)}
<span className={css.treeMeta}>
{t(folded ? 'tasksFoldExpand' : 'tasksFoldCollapse')}
</span>
</span>
<span className={css.treeMeta} title={foldPreviews(node.previews, node.count)}>
{foldPreviews(node.previews, node.count)}
</span>
</span>
</div>
)
: node.kind === 'workflow'
? (
<div
key={node.id}
data-tasks-row
role="treeitem"
tabIndex={0}
aria-level={depth + 1}
aria-expanded="true"
aria-label={`${node.run.name} ${workflowMeta(node)}`}
className={clsx(css.treeRow, node.run.status !== 'running' && css.treeRowSettled)}
onClick={(event) => { onWorkflowInfo(node, event.currentTarget) }}
onKeyDown={(event) => {
activateOnKey(event, () => { onWorkflowInfo(node, event.currentTarget as HTMLElement) })
}}
>
<StateDot className={css.treeDot} state={node.run.status === 'running' ? 'ongoing' : 'done'} size={6} />
<span className={css.treeGlyph} aria-hidden="true"><WorkflowGlyph /></span>
<span className={css.treeContent}>
<span className={css.treeTitle} title={node.run.name}>{node.run.name}</span>
<span className={css.treeMeta}>{workflowMeta(node)}</span>
</span>
</div>
)
: (
<div
key={node.id}
data-tasks-row
role="treeitem"
tabIndex={0}
aria-level={depth + 1}
aria-label={`${node.label} ${agentMeta(node)}`}
aria-current={node.current ? 'true' : undefined}
className={clsx(
css.treeRow,
node.current && css.treeRowActive,
(node.state === 'done' || node.state === 'error') && !node.current && css.treeRowSettled,
)}
onClick={(event) => { onNodeInfo(node, event.currentTarget) }}
onKeyDown={(event) => {
activateOnKey(event, () => { onNodeInfo(node, event.currentTarget as HTMLElement) })
}}
>
<StateDot className={css.treeDot} state={nodeDotState(node.state)} size={6} />
<span className={css.treeGlyph} aria-hidden="true"><AgentGlyph node={node} /></span>
<span className={css.treeContent}>
<span className={css.treeTitle} title={node.label}>{node.label}</span>
<span className={css.treeMeta}>{agentMeta(node)}</span>
{node.state === 'running' && <LiveLine live={node.live} />}
<TaskLine tasks={node.tasks} onOpenTask={onOpenTask} />
</span>
</div>
)
if (children.length === 0) return <div key={node.id}>{row}</div>
return (
<div key={node.id}>
{row}
<div className={css.treeKids}>
{children.map(child => renderNode(child, depth + 1))}
</div>
</div>
)
}
const roots = childrenOf.get('') ?? []
return (
<div className={css.graphView}>
<div
ref={bodyRef}
className={css.treeView}
role="tree"
aria-label={t('subagent')}
onKeyDown={onTreeKeyDown}
>
{loading === true && roots.length === 0 && (
<div className={css.viewEmptyHint}>
<span className={css.treeGlyph} aria-hidden="true"><IconLoadingOutlineRegular size={12} /></span>
{t('loading')}
</div>
)}
{roots.map(root => renderNode(root, 0))}
</div>
<div className={css.controls} data-graph-controls>
<ViewModeToggle mode={mode} onModeChange={onModeChange} />
<FoldToggleButton folded={folded} onToggleFold={onToggleFold} />
</div>
</div>
)
}