From 50dab4743fdd95fb6551ce604c2b38f10e0317be Mon Sep 17 00:00:00 2001 From: delta <528491526@qq.com> Date: Sun, 22 Nov 2026 21:21:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E9=A1=B9=E7=9B=AE=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E6=94=AF=E6=8C=81=E8=84=9A=E6=9C=AC=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=98=BE=E5=BC=8F=E6=8E=92=E5=BA=8F=20(Closes=20#138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/desktop/e2e/features/sorting.spec.ts | 62 ++++++++++++++ .../src/features/projects/project-detail.tsx | 19 ++++- .../src/features/projects/project-subnav.tsx | 52 +++++++----- .../src/features/scripts/script-list.tsx | 46 ++++++---- .../src/features/scripts/script-sort-menu.tsx | 50 +++++++++++ .../src/features/scripts/script-sort.ts | 36 ++++++++ packages/shared/src/index.ts | 1 + packages/shared/src/scripts.test.ts | 81 ++++++++++++++++++ packages/shared/src/scripts.ts | 85 +++++++++++++++++++ 9 files changed, 397 insertions(+), 35 deletions(-) create mode 100644 apps/desktop/src/renderer/src/features/scripts/script-sort-menu.tsx create mode 100644 apps/desktop/src/renderer/src/features/scripts/script-sort.ts create mode 100644 packages/shared/src/scripts.test.ts create mode 100644 packages/shared/src/scripts.ts diff --git a/apps/desktop/e2e/features/sorting.spec.ts b/apps/desktop/e2e/features/sorting.spec.ts index d8e7891..6fb5822 100644 --- a/apps/desktop/e2e/features/sorting.spec.ts +++ b/apps/desktop/e2e/features/sorting.spec.ts @@ -138,3 +138,65 @@ test('sorts batch tasks by name and persists selection after restart', async ({ await expect(restartedGroupItems.nth(1)).toContainText('A-Task') await expect(page.getByRole('button', { name: '批量任务排序' })).toContainText('名称降序') }) + +test('sorts scripts by name and retains sort order after restart', async ({ + launchDevhub, + createProject +}) => { + const dir = await createProject('script-sort-app', { + 'package.json': packageJson({ + echo: 'node -v', + build: 'node -v', + test: 'node -v' + }) + }) + + const first = await launchDevhub() + const { app } = first + let { page } = first + await addProjectViaApi(page, dir) + + // Navigate to project detail by clicking project in list + await projectList(page) + .getByRole('button', { name: /^script-sort-app/ }) + .click() + + const projectContent = page.getByRole('region', { name: '项目内容' }) + const scriptItems = projectContent.locator('section ul li') + + // Default order is declaration order: echo, build, test + await expect(scriptItems.nth(0)).toContainText('echo') + await expect(scriptItems.nth(1)).toContainText('build') + await expect(scriptItems.nth(2)).toContainText('test') + + // Switch to Name Ascending (A-Z) -> build, echo, test + await projectContent.getByRole('button', { name: '脚本排序' }).click() + await page.getByRole('menuitemradio', { name: '名称升序 (A-Z)' }).click() + + await expect(scriptItems.nth(0)).toContainText('build') + await expect(scriptItems.nth(1)).toContainText('echo') + await expect(scriptItems.nth(2)).toContainText('test') + + // Switch to Name Descending (Z-A) -> test, echo, build + await projectContent.getByRole('button', { name: '脚本排序' }).click() + await page.getByRole('menuitemradio', { name: '名称降序 (Z-A)' }).click() + + await expect(scriptItems.nth(0)).toContainText('test') + await expect(scriptItems.nth(1)).toContainText('echo') + await expect(scriptItems.nth(2)).toContainText('build') + + // Restart app and verify sort order persists + await app.close() + page = (await launchDevhub()).page + + await projectList(page) + .getByRole('button', { name: /^script-sort-app/ }) + .click() + const restartedContent = page.getByRole('region', { name: '项目内容' }) + const restartedScriptItems = restartedContent.locator('section ul li') + + await expect(restartedScriptItems.nth(0)).toContainText('test') + await expect(restartedScriptItems.nth(1)).toContainText('echo') + await expect(restartedScriptItems.nth(2)).toContainText('build') + await expect(restartedContent.getByRole('button', { name: '脚本排序' })).toContainText('名称降序') +}) diff --git a/apps/desktop/src/renderer/src/features/projects/project-detail.tsx b/apps/desktop/src/renderer/src/features/projects/project-detail.tsx index 5324844..958805a 100644 --- a/apps/desktop/src/renderer/src/features/projects/project-detail.tsx +++ b/apps/desktop/src/renderer/src/features/projects/project-detail.tsx @@ -1,11 +1,15 @@ import { useEffect, useState } from 'react' import { TriangleAlert, Workflow, X } from 'lucide-react' -import type { Project } from '@devhub/shared' +import type { Project, ScriptSortOrder } from '@devhub/shared' import { Alert, AlertAction, AlertDescription, AlertTitle } from '@renderer/components/ui/alert' import { Button } from '@renderer/components/ui/button' import { useGroups } from '@renderer/features/groups/use-groups' import { useRuns, useStartScript } from '@renderer/features/runs/use-runs' import { ScriptList } from '@renderer/features/scripts/script-list' +import { + SCRIPT_SORT_STORAGE_KEY, + getStoredScriptSortOrder +} from '@renderer/features/scripts/script-sort' import { useProjectScripts } from '@renderer/features/scripts/use-scripts' import { TerminalPanel } from '@renderer/features/terminal/terminal-panel' import { ProjectHeader } from './project-header' @@ -38,6 +42,16 @@ export function ProjectDetail({ const [filterQuery, setFilterQuery] = useState('') const [activeRunId, setActiveRunId] = useState(initialRunId) const [editorError, setEditorError] = useState(null) + const [scriptSortOrder, setScriptSortOrder] = useState(getStoredScriptSortOrder) + + const handleScriptSortOrderChange = (order: ScriptSortOrder): void => { + setScriptSortOrder(order) + try { + localStorage.setItem(SCRIPT_SORT_STORAGE_KEY, order) + } catch { + // ignore + } + } useEffect(() => { onTerminalViewChange?.(currentTab === 'terminals') @@ -86,6 +100,8 @@ export function ProjectDetail({ tasksCount={projectBatchTasks.length} filterQuery={filterQuery} onFilterChange={setFilterQuery} + scriptSortOrder={scriptSortOrder} + onScriptSortOrderChange={handleScriptSortOrderChange} onQuickStart={scriptItems.length > 0 ? handleStartAll : undefined} isStarting={startScript.isPending} /> @@ -114,6 +130,7 @@ export function ProjectDetail({ setActiveRunId(run.id)} onViewTerminal={(runId) => { setActiveRunId(runId) diff --git a/apps/desktop/src/renderer/src/features/projects/project-subnav.tsx b/apps/desktop/src/renderer/src/features/projects/project-subnav.tsx index 849c915..61fb0b2 100644 --- a/apps/desktop/src/renderer/src/features/projects/project-subnav.tsx +++ b/apps/desktop/src/renderer/src/features/projects/project-subnav.tsx @@ -8,7 +8,9 @@ import { Workflow, X } from 'lucide-react' +import type { ScriptSortOrder } from '@devhub/shared' import { Button } from '@renderer/components/ui/button' +import { ScriptSortMenu } from '@renderer/features/scripts/script-sort-menu' import { cn } from '@renderer/lib/utils' export type ProjectViewTab = 'scripts' | 'terminals' | 'history' | 'tasks' | 'settings' @@ -22,6 +24,8 @@ interface ProjectSubnavProps { tasksCount?: number filterQuery: string onFilterChange: (query: string) => void + scriptSortOrder?: ScriptSortOrder + onScriptSortOrderChange?: (order: ScriptSortOrder) => void onQuickStart?: () => void isStarting?: boolean } @@ -35,6 +39,8 @@ export function ProjectSubnav({ tasksCount = 0, filterQuery, onFilterChange, + scriptSortOrder, + onScriptSortOrderChange, onQuickStart, isStarting }: ProjectSubnavProps): React.JSX.Element { @@ -176,26 +182,34 @@ export function ProjectSubnav({ {/* Right Controls: Filter & Quick Action */}
{currentTab === 'scripts' && ( -
- - onFilterChange(e.target.value)} - placeholder="筛选脚本..." - className="h-7 w-36 rounded-md border border-border bg-secondary/60 pl-6.5 pr-6 text-xs text-foreground placeholder:text-muted-foreground/70 focus:border-ring focus:bg-background focus:outline-none focus:ring-1 focus:ring-ring sm:w-44" - /> - {filterQuery && ( - + <> +
+ + onFilterChange(e.target.value)} + placeholder="筛选脚本..." + className="h-7 w-36 rounded-md border border-border bg-secondary/60 pl-6.5 pr-6 text-xs text-foreground placeholder:text-muted-foreground/70 focus:border-ring focus:bg-background focus:outline-none focus:ring-1 focus:ring-ring sm:w-44" + /> + {filterQuery && ( + + )} +
+ {scriptSortOrder && onScriptSortOrderChange && ( + )} -
+ )} {onQuickStart && ( diff --git a/apps/desktop/src/renderer/src/features/scripts/script-list.tsx b/apps/desktop/src/renderer/src/features/scripts/script-list.tsx index 978289f..4bd12bc 100644 --- a/apps/desktop/src/renderer/src/features/scripts/script-list.tsx +++ b/apps/desktop/src/renderer/src/features/scripts/script-list.tsx @@ -1,6 +1,13 @@ import { useState } from 'react' import { ChevronRight, FileCode, FolderX, TriangleAlert, X } from 'lucide-react' -import type { Project, Run, Script, ScriptSource } from '@devhub/shared' +import { + sortScripts, + type Project, + type Run, + type Script, + type ScriptSortOrder, + type ScriptSource +} from '@devhub/shared' import { Alert, AlertAction, AlertDescription, AlertTitle } from '@renderer/components/ui/alert' import { Button } from '@renderer/components/ui/button' import { Skeleton } from '@renderer/components/ui/skeleton' @@ -13,6 +20,7 @@ import { useProjectScripts } from './use-scripts' interface ScriptListProps { project: Project filterQuery?: string + sortOrder?: ScriptSortOrder /** Called with a freshly started run so the terminal panel can switch to it. */ onRunStarted: (run: Run) => void onViewTerminal?: (runId: string) => void @@ -22,6 +30,7 @@ interface ScriptListProps { export function ScriptList({ project, filterQuery = '', + sortOrder = 'default', onRunStarted, onViewTerminal, onOpenSettings @@ -141,20 +150,27 @@ export function ScriptList({

)} - {groupBySource(displayItems).map(([source, group]) => ( - setRunError({ error, retry })} - onRunStarted={onRunStarted} - onViewTerminal={onViewTerminal} - onOpenSettings={onOpenSettings} - /> - ))} + {groupBySource(displayItems).map(([source, group]) => { + const sortedScripts = sortScripts(group, { + order: sortOrder, + runs: runs.data, + projectId: project.id + }) + return ( + setRunError({ error, retry })} + onRunStarted={onRunStarted} + onViewTerminal={onViewTerminal} + onOpenSettings={onOpenSettings} + /> + ) + })} {/* 自定义脚本提示条 (对齐 pen/devhub.pen) */}
diff --git a/apps/desktop/src/renderer/src/features/scripts/script-sort-menu.tsx b/apps/desktop/src/renderer/src/features/scripts/script-sort-menu.tsx new file mode 100644 index 0000000..c9f273f --- /dev/null +++ b/apps/desktop/src/renderer/src/features/scripts/script-sort-menu.tsx @@ -0,0 +1,50 @@ +import { ArrowUpDown } from 'lucide-react' +import type { ScriptSortOrder } from '@devhub/shared' +import { Button } from '@renderer/components/ui/button' +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuRadioGroup, + DropdownMenuRadioItem, + DropdownMenuTrigger +} from '@renderer/components/ui/dropdown-menu' +import { scriptSortLabels, scriptSortOptions, scriptSortShortLabels } from './script-sort' + +interface ScriptSortMenuProps { + sortOrder: ScriptSortOrder + onSortOrderChange: (order: ScriptSortOrder) => void +} + +export function ScriptSortMenu({ + sortOrder, + onSortOrderChange +}: ScriptSortMenuProps): React.JSX.Element { + return ( + + + + + + onSortOrderChange(val as ScriptSortOrder)} + > + {scriptSortOptions.map((opt) => ( + + {opt.label} + + ))} + + + + ) +} diff --git a/apps/desktop/src/renderer/src/features/scripts/script-sort.ts b/apps/desktop/src/renderer/src/features/scripts/script-sort.ts new file mode 100644 index 0000000..d4ef2a0 --- /dev/null +++ b/apps/desktop/src/renderer/src/features/scripts/script-sort.ts @@ -0,0 +1,36 @@ +import type { ScriptSortOrder } from '@devhub/shared' + +export const scriptSortLabels: Record = { + default: '默认排序', + 'name-asc': '名称升序 (A-Z)', + 'name-desc': '名称降序 (Z-A)', + 'active-first': '运行中优先' +} + +export const scriptSortShortLabels: Record = { + default: '默认排序', + 'name-asc': '名称升序', + 'name-desc': '名称降序', + 'active-first': '运行中优先' +} + +export const scriptSortOptions: Array<{ value: ScriptSortOrder; label: string }> = [ + { value: 'default', label: '默认排序' }, + { value: 'name-asc', label: '名称升序 (A-Z)' }, + { value: 'name-desc', label: '名称降序 (Z-A)' }, + { value: 'active-first', label: '运行中优先' } +] + +export const SCRIPT_SORT_STORAGE_KEY = 'devhub.scripts.sort-order' + +export function getStoredScriptSortOrder(): ScriptSortOrder { + try { + const saved = localStorage.getItem(SCRIPT_SORT_STORAGE_KEY) + if (saved && saved in scriptSortLabels) { + return saved as ScriptSortOrder + } + } catch { + // ignore + } + return 'default' +} diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 4845fac..b006b0a 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -14,6 +14,7 @@ export * from './ports' export * from './projects' export * from './remote' export * from './remote-client' +export * from './scripts' export * from './settings' export * from './schedules' export * from './start-script' diff --git a/packages/shared/src/scripts.test.ts b/packages/shared/src/scripts.test.ts new file mode 100644 index 0000000..9f28642 --- /dev/null +++ b/packages/shared/src/scripts.test.ts @@ -0,0 +1,81 @@ +import { describe, expect, it } from 'vitest' +import type { Run, Script } from './domain' +import { sortScripts } from './scripts' + +const makeScript = (id: string, name: string, command = 'echo test'): Script => ({ + id, + name, + command, + source: 'npm' +}) + +const makeRun = ( + id: string, + projectId: string, + scriptId: string, + status: Run['status'], + startedAt = '2026-01-01T00:00:00.000Z' +): Run => ({ + id, + projectId, + title: scriptId, + kind: 'script', + scriptId, + command: 'echo test', + status, + pid: 1234, + exitCode: status === 'exited' ? 0 : null, + stopped: false, + startedAt +}) + +describe('sortScripts', () => { + const sA = makeScript('s1', 'build', 'vite build') + const sB = makeScript('s2', 'dev', 'vite dev') + const sC = makeScript('s3', 'test', 'vitest') + + it('keeps detector/configuration order by default', () => { + const original = [sB, sC, sA] + const sorted = sortScripts(original) + expect(sorted.map((s) => s.id)).toEqual(['s2', 's3', 's1']) + // does not mutate + expect(sorted).not.toBe(original) + }) + + it('sorts alphabetically ascending by name', () => { + const sorted = sortScripts([sB, sC, sA], { order: 'name-asc' }) + expect(sorted.map((s) => s.name)).toEqual(['build', 'dev', 'test']) + }) + + it('sorts alphabetically descending by name', () => { + const sorted = sortScripts([sB, sA, sC], { order: 'name-desc' }) + expect(sorted.map((s) => s.name)).toEqual(['test', 'dev', 'build']) + }) + + it('sorts active scripts first when order is active-first', () => { + const runs: Run[] = [ + makeRun('r1', 'p1', 's2', 'running', '2026-01-01T10:00:00.000Z'), + makeRun('r2', 'p1', 's3', 'running', '2026-01-01T12:00:00.000Z'), + makeRun('r3', 'p1', 's1', 'exited', '2026-01-01T14:00:00.000Z') + ] + + const sorted = sortScripts([sA, sB, sC], { + order: 'active-first', + runs, + projectId: 'p1' + }) + + // s3 started at 12:00 (active), s2 started at 10:00 (active), s1 is exited (inactive) + expect(sorted.map((s) => s.id)).toEqual(['s3', 's2', 's1']) + }) + + it('breaks equal active status ties by name', () => { + const sorted = sortScripts([sC, sB, sA], { + order: 'active-first', + runs: [], + projectId: 'p1' + }) + // No active runs -> sorted by name + expect(sorted.map((s) => s.name)).toEqual(['build', 'dev', 'test']) + }) +}) diff --git a/packages/shared/src/scripts.ts b/packages/shared/src/scripts.ts new file mode 100644 index 0000000..62e6f7c --- /dev/null +++ b/packages/shared/src/scripts.ts @@ -0,0 +1,85 @@ +import type { Run, Script } from './domain' + +export type ScriptSortOrder = 'default' | 'name-asc' | 'name-desc' | 'active-first' + +export interface SortScriptsOptions { + /** + * Sort order for scripts: + * - 'default': keep original detector/configuration order. + * - 'name-asc': sort by script name alphabetically ascending. + * - 'name-desc': sort by script name alphabetically descending. + * - 'active-first': scripts currently running appear first (latest started first). + */ + order?: ScriptSortOrder + /** + * Active runs for determining which scripts are currently running. + */ + runs?: readonly Run[] + /** + * Project ID to match against runs when using 'active-first'. + */ + projectId?: string +} + +/** + * Sorts scripts according to the specified sort order. + * Returns a new array without mutating the input. + */ +export function sortScripts(scripts: readonly Script[], options?: SortScriptsOptions): Script[] { + const order = options?.order ?? 'default' + if (order === 'default') { + return [...scripts] + } + + if (order === 'name-asc') { + return [...scripts].sort((a, b) => { + const byName = a.name.localeCompare(b.name) + if (byName !== 0) return byName + return a.command.localeCompare(b.command) + }) + } + + if (order === 'name-desc') { + return [...scripts].sort((a, b) => { + const byName = b.name.localeCompare(a.name) + if (byName !== 0) return byName + return b.command.localeCompare(a.command) + }) + } + + // 'active-first' + const runs = options?.runs ?? [] + const projectId = options?.projectId + + // Map scriptId -> latest startedAt timestamp + const activeScriptTimes = new Map() + for (const run of runs) { + if (run.status === 'exited') continue + if (run.kind === 'script' && (!projectId || run.projectId === projectId)) { + const time = Date.parse(run.startedAt) || 0 + const existing = activeScriptTimes.get(run.scriptId) + if (existing === undefined || time > existing) { + activeScriptTimes.set(run.scriptId, time) + } + } + } + + return [...scripts].sort((a, b) => { + const aActiveTime = activeScriptTimes.get(a.id) + const bActiveTime = activeScriptTimes.get(b.id) + const aActive = aActiveTime !== undefined + const bActive = bActiveTime !== undefined + + if (aActive && !bActive) return -1 + if (!aActive && bActive) return 1 + if (aActive && bActive && aActiveTime !== undefined && bActiveTime !== undefined) { + if (bActiveTime !== aActiveTime) { + return bActiveTime - aActiveTime + } + } + + const byName = a.name.localeCompare(b.name) + if (byName !== 0) return byName + return a.command.localeCompare(b.command) + }) +} From c8985f308a63b1529a04c8e4f0e6dd241999fd56 Mon Sep 17 00:00:00 2001 From: delta <528491526@qq.com> Date: Sun, 22 Nov 2026 21:52:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(groups):=20=E4=BF=AE=E5=A4=8D=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E4=BB=BB=E5=8A=A1=E5=AD=90=E4=BB=BB=E5=8A=A1=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=80=81=E4=BB=8D=E6=98=BE=E7=A4=BA=E7=81=B0=E8=89=B2?= =?UTF-8?q?=E5=B7=B2=E5=AE=8C=E6=88=90=E4=B8=8E=E7=BC=96=E8=BE=91=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E8=BE=93=E5=85=A5=E6=A1=86=E5=B7=A6=E8=BE=B9=E6=A1=86?= =?UTF-8?q?=E9=81=AE=E6=8C=A1=20(Closes=20#137)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 resolveGroupStatus 纯函数与单元测试,将批量任务状态与子脚本活跃长任务联动 - 批量任务卡片在子脚本存活时显示高亮运行中状态与停止操作,不提前置灰已完成 - 修复 GroupEditor 编辑弹窗左侧内边距缺失导致的输入框边框裁切 - 更新相关 Playwright E2E 运行态断言 --- apps/desktop/e2e/features/groups.spec.ts | 6 +- apps/desktop/e2e/process/ports.spec.ts | 2 +- .../src/features/groups/batch-task-list.tsx | 71 +++---- .../src/features/groups/group-detail.tsx | 57 +++-- .../src/features/groups/group-editor.tsx | 2 +- .../src/features/groups/group-list.tsx | 50 ++--- .../src/features/groups/group-status.test.ts | 198 ++++++++++++++++++ .../src/features/groups/group-status.ts | 180 ++++++++++++++++ docs/PROGRESS.md | 1 + 9 files changed, 458 insertions(+), 109 deletions(-) create mode 100644 apps/desktop/src/renderer/src/features/groups/group-status.test.ts create mode 100644 apps/desktop/src/renderer/src/features/groups/group-status.ts diff --git a/apps/desktop/e2e/features/groups.spec.ts b/apps/desktop/e2e/features/groups.spec.ts index bb22417..d19373c 100644 --- a/apps/desktop/e2e/features/groups.spec.ts +++ b/apps/desktop/e2e/features/groups.spec.ts @@ -83,7 +83,7 @@ test('a serial group across projects waits for each step, then stops everything' await expect(bar).toContainText(/front1启动中 \d\/2/) await expect(titleBar).toHaveCount(0) await page.getByRole('button', { name: '批量', exact: true }).click() - await expect(item).toContainText('已完成', { timeout: 30_000 }) + await expect(item).toContainText('运行中 2/2', { timeout: 30_000 }) // The navigation badge and bar remain while the servers keep running after the execution ended. await expect(running).toHaveText(/\d+/) await page.getByRole('button', { name: '运行中', exact: true }).click() @@ -91,7 +91,7 @@ test('a serial group across projects waits for each step, then stops everything' await expect(titleBar).toHaveCount(0) await page.getByRole('button', { name: '批量', exact: true }).click() await expect(detail).toContainText('2/2 个步骤完成') - await expect(detail).toContainText('服务进程可能仍在运行') + await expect(detail).toContainText('服务进程正在运行中') await expect(detail.getByRole('button', { name: '停止全部' })).toBeEnabled() await expect(detail.getByRole('listitem', { name: '运行步骤 1' })).toContainText('本次启动') await detail @@ -194,7 +194,7 @@ test('waits for a regex in the output and remembers the result after a restart', const item = () => page.getByRole('listitem', { name: '批量任务 backend' }) await item().getByRole('button', { name: '执行 backend' }).click() - await expect(item()).toContainText(/已完成 · \d{2}:\d{2}/) + await expect(item()).toContainText('运行中 1/1', { timeout: 30_000 }) await item().getByRole('button', { name: '查看 backend' }).click() const detail = () => page.getByRole('region', { name: '任务运行 backend' }) await expect(detail()).toContainText('本次启动') diff --git a/apps/desktop/e2e/process/ports.spec.ts b/apps/desktop/e2e/process/ports.spec.ts index 1d0d830..35ee9ec 100644 --- a/apps/desktop/e2e/process/ports.spec.ts +++ b/apps/desktop/e2e/process/ports.spec.ts @@ -257,7 +257,7 @@ for (const mode of ['serial', 'parallel'] as const) { await item.getByRole('button', { name: '执行 replace listeners' }).click() // Windows performs verified CIM snapshots for each sequential port takeover. - await expect(item).toContainText('已完成', { timeout: 35_000 }) + await expect(item).toContainText('运行中 2/2', { timeout: 35_000 }) await expect.poll(() => httpBody(first.port)).toBe('group-first') await expect.poll(() => httpBody(second.port)).toBe('group-second') await expect.poll(() => [hasExited(first), hasExited(second)]).toEqual([true, true]) diff --git a/apps/desktop/src/renderer/src/features/groups/batch-task-list.tsx b/apps/desktop/src/renderer/src/features/groups/batch-task-list.tsx index 0a75586..55f70a8 100644 --- a/apps/desktop/src/renderer/src/features/groups/batch-task-list.tsx +++ b/apps/desktop/src/renderer/src/features/groups/batch-task-list.tsx @@ -23,6 +23,7 @@ import { } from '@renderer/components/ui/dropdown-menu' import { useRuns } from '@renderer/features/runs/use-runs' import { cn } from '@renderer/lib/utils' +import { resolveGroupStatus } from './group-status' import { GROUP_SORT_STORAGE_KEY, getStoredGroupSortOrder } from './group-sort' import { GroupSortMenu } from './group-sort-menu' import { @@ -67,7 +68,8 @@ export function BatchTaskList({ } } - const activeIds = new Set(activeGroups(states.data ?? [], runs).map((group) => group.groupId)) + const activeList = activeGroups(states.data ?? [], runs) + const activeIds = new Set(activeList.map((group) => group.groupId)) const sortedGroups = sortGroups(groups.data ?? [], activeIds, { order: sortOrder, states: states.data @@ -114,10 +116,9 @@ export function BatchTaskList({
    {sortedGroups.map((group) => { const state = states.data?.find((s) => s.groupId === group.id) + const active = activeList.find((a) => a.groupId === group.id) + const presentation = resolveGroupStatus({ group, state, active }) const isSelected = group.id === selectedGroupId - const running = state?.status === 'running' - const failed = state?.status === 'failed' - const done = state?.status === 'done' const busy = (start.isPending && start.variables === group.id) || (stop.isPending && stop.variables === group.id) @@ -140,10 +141,11 @@ export function BatchTaskList({ @@ -168,21 +170,15 @@ export function BatchTaskList({ variant="secondary" className={cn( 'h-4 shrink-0 px-1 font-mono text-[9px] font-normal border-0', - running && 'bg-primary/15 text-primary font-medium', - failed && 'bg-destructive/15 text-destructive font-medium', - done && 'bg-secondary text-muted-foreground', - !state && 'bg-secondary/60 text-muted-foreground/60' + presentation.isActive && 'bg-primary/15 text-primary font-medium', + presentation.phase === 'failed' && + 'bg-destructive/15 text-destructive font-medium', + (presentation.phase === 'done' || presentation.phase === 'stopped') && + 'bg-secondary text-muted-foreground', + presentation.phase === 'idle' && 'bg-secondary/60 text-muted-foreground/60' )} > - {running - ? '运行中' - : failed - ? '失败' - : done - ? '已完成' - : group.mode === 'parallel' - ? '并行' - : '串行'} + {presentation.badgeText}
@@ -191,14 +187,8 @@ export function BatchTaskList({ {group.steps.length} 步 · {group.mode === 'parallel' ? '并行' : '串行'} - - {running - ? '运行中' - : failed - ? (state?.steps?.find((s) => s.state === 'failed')?.message ?? '失败') - : state?.finishedAt - ? `已完成 · ${formatFinished(state.finishedAt)}` - : '未运行'} + + {presentation.summaryText}
@@ -208,12 +198,12 @@ export function BatchTaskList({ variant="ghost" size="icon-xs" onClick={() => start.mutate(group.id, { onError: (e) => setError(e.message) })} - disabled={busy || running} + disabled={busy || presentation.phase === 'starting'} aria-label={`执行 ${group.name}`} - title="执行任务" + title={presentation.isActive ? '重新执行任务' : '执行任务'} className="size-6 text-muted-foreground hover:text-foreground" > - {busy && !running ? ( + {busy && presentation.phase === 'starting' ? ( ) : ( @@ -224,10 +214,15 @@ export function BatchTaskList({ variant="ghost" size="icon-xs" onClick={() => stop.mutate(group.id, { onError: (e) => setError(e.message) })} - disabled={busy} + disabled={busy || !presentation.isActive} aria-label={`停止 ${group.name}`} title="停止全部" - className="size-6 text-muted-foreground hover:text-foreground" + className={cn( + 'size-6 transition-colors', + presentation.isActive + ? 'text-destructive hover:bg-destructive/15 hover:text-destructive' + : 'text-muted-foreground/40 hover:text-muted-foreground' + )} > @@ -309,11 +304,3 @@ export function BatchTaskList({ ) } - -function formatFinished(iso: string): string { - const date = new Date(iso) - const time = date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }) - return date.toDateString() === new Date().toDateString() - ? time - : `${date.getMonth() + 1}月${date.getDate()}日 ${time}` -} diff --git a/apps/desktop/src/renderer/src/features/groups/group-detail.tsx b/apps/desktop/src/renderer/src/features/groups/group-detail.tsx index 61757b4..c973057 100644 --- a/apps/desktop/src/renderer/src/features/groups/group-detail.tsx +++ b/apps/desktop/src/renderer/src/features/groups/group-detail.tsx @@ -1,6 +1,6 @@ import { useState } from 'react' import { Pencil, Play, RotateCcw, Square } from 'lucide-react' -import { formatDuration, type Group, type GroupRunState, type Run } from '@devhub/shared' +import { activeGroups, formatDuration, type Group, type Run } from '@devhub/shared' import { access } from '@renderer/api' import { Badge } from '@renderer/components/ui/badge' import { Button } from '@renderer/components/ui/button' @@ -12,16 +12,10 @@ import { cn } from '@renderer/lib/utils' import { GroupEditor } from './group-editor' import { GroupOutputPanel } from './group-output-panel' import { GroupProgressBar } from './group-progress-bar' +import { resolveGroupStatus } from './group-status' import { GroupStepCard } from './group-step-card' import { useGroupStates, useStartGroup, useStopGroup } from './use-groups' -const statusLabels: Record = { - running: '执行中', - done: '已完成', - failed: '失败', - stopped: '已停止' -} - export function GroupDetail({ group, onOpenRun @@ -29,11 +23,14 @@ export function GroupDetail({ group: Group onOpenRun: (run: Run) => void }): React.JSX.Element { - const state = useGroupStates().data?.find((candidate) => candidate.groupId === group.id) + const allStates = useGroupStates().data ?? [] + const state = allStates.find((candidate) => candidate.groupId === group.id) const execution = state?.group ?? group const projects = useProjects().data ?? [] const runs = useRuns().data ?? [] const health = useRunHealth().data ?? [] + const active = activeGroups(allStates, runs).find((candidate) => candidate.groupId === group.id) + const presentation = resolveGroupStatus({ group, state, active }) const start = useStartGroup() const stop = useStopGroup() const [editing, setEditing] = useState(false) @@ -74,7 +71,6 @@ export function GroupDetail({ ) const isParallel = execution.mode === 'parallel' - const currentStepNum = Math.min(completed + 1, execution.steps.length) return (
- {running - ? `第 ${currentStepNum} / ${execution.steps.length} 步 · 运行中` - : state - ? statusLabels[state.status] - : '尚未执行'} + {presentation.detailBadgeText} @@ -205,15 +198,7 @@ export function GroupDetail({ totalCount={execution.steps.length} stepStates={stepStates} durationText={groupDurationMs !== null ? formatDuration(groupDurationMs) : null} - statusHint={ - running - ? `正在执行第 ${currentStepNum} 步` - : state?.status === 'done' - ? '全部步骤就绪' - : state?.status === 'failed' - ? '执行中断' - : '待执行' - } + statusHint={presentation.progressHint} /> {/* Main body scrollable area */} @@ -237,9 +222,15 @@ export function GroupDetail({

)} - {state?.status === 'done' && ( + {presentation.phase === 'running' && (

- 步骤已完成,服务进程可能仍在运行。当前有 {activeRuns.length} 个活动脚本。 + 步骤已就绪,服务进程正在运行中。当前有 {activeRuns.length} 个活动脚本。 +

+ )} + + {presentation.phase === 'done' && ( +

+ 上次执行步骤已完成,相关服务均已停止。

)} diff --git a/apps/desktop/src/renderer/src/features/groups/group-editor.tsx b/apps/desktop/src/renderer/src/features/groups/group-editor.tsx index 9c87a5b..22a9706 100644 --- a/apps/desktop/src/renderer/src/features/groups/group-editor.tsx +++ b/apps/desktop/src/renderer/src/features/groups/group-editor.tsx @@ -88,7 +88,7 @@ export function GroupEditor({ 把多个项目的脚本组合在一起,一键启动或停止。 -
+
group.groupId)) + const activeList = activeGroups(states.data ?? [], runs) + const activeIds = new Set(activeList.map((group) => group.groupId)) const sortedGroups = sortGroups(groups.data ?? [], activeIds, { order: sortOrder, states: states.data @@ -142,6 +145,7 @@ export function GroupList({ selected={group.id === selectedGroupId} onSelect={() => onSelectGroup(group.id)} state={states.data?.find((state) => state.groupId === group.id)} + active={activeList.find((a) => a.groupId === group.id)} projects={projects} busy={ (start.isPending && start.variables === group.id) || @@ -178,6 +182,7 @@ interface GroupItemProps { onSelect: () => void group: Group state: GroupRunState | undefined + active: ActiveGroup | undefined projects: Project[] busy: boolean onStart: () => void @@ -189,18 +194,12 @@ interface GroupItemProps { onSelectProject: (projectId: string) => void } -const statusLabels: Record = { - running: '执行中', - done: '已完成', - failed: '失败', - stopped: '已停止' -} - function GroupItem({ selected, onSelect, group, state, + active, projects, busy, onStart, @@ -211,9 +210,11 @@ function GroupItem({ onSelectProject }: GroupItemProps): React.JSX.Element { const deleteGroup = useDeleteGroup() - const running = state?.status === 'running' + const presentation = resolveGroupStatus({ group, state, active }) // Show the steps while something is happening or went wrong; finished groups stay compact. - const showSteps = state && (state.status === 'running' || state.status === 'failed') + const showSteps = + state && + (state.status === 'running' || state.status === 'failed' || presentation.phase === 'running') return (
  • - {state - ? [statusLabels[state.status], state.finishedAt && formatFinished(state.finishedAt)] - .filter(Boolean) - .join(' · ') - : `${group.steps.length} 个脚本`} + {state ? presentation.summaryText : `${group.steps.length} 个脚本`}