From d514e7f980a20417cd43379aa0fd9335b75463a6 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 12:16:34 -0700 Subject: [PATCH] Resolve smart workspace command value during render (#3192) --- .../new-workspace/SmartWorkspaceNameField.tsx | 41 ++------- .../smart-workspace-command-value.test.ts | 86 +++++++++++++++++++ .../smart-workspace-command-value.ts | 49 +++++++++++ 3 files changed, 144 insertions(+), 32 deletions(-) create mode 100644 src/renderer/src/components/new-workspace/smart-workspace-command-value.test.ts create mode 100644 src/renderer/src/components/new-workspace/smart-workspace-command-value.ts diff --git a/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx b/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx index 19e2696b6..14a66888b 100644 --- a/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx +++ b/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx @@ -58,6 +58,7 @@ import type { GitLabWorkItem, LinearIssue } from '../../../../shared/types' +import { resolveSmartWorkspaceCommandValue } from './smart-workspace-command-value' // Why: GitLab MR list filter — Open / Merged / Closed / All — replaces // GitHub's search-DSL on the GitLab tab per the agreed scope. @@ -773,36 +774,12 @@ export default function SmartWorkspaceNameField({ return null }, [linearAvailable, value]) - useEffect(() => { - if (rows.length === 0) { - return - } - if (isQueryStale) { - const typedTextRow = rows.find( - (row) => row.kind === 'use-name' || row.kind === 'create-branch' - ) - // No typed-text fallback in this mode (GitHub/Linear): clear the - // highlight so cmdk doesn't auto-select a stale source on Enter. - setCommandValue(typedTextRow ? typedTextRow.value : '') - return - } - if (sourceIntent === 'github') { - const githubRow = rows.find((row) => row.kind === 'github') - if (githubRow) { - setCommandValue(githubRow.value) - return - } - } else if (sourceIntent === 'linear') { - const linearRow = rows.find((row) => row.kind === 'linear') - if (linearRow) { - setCommandValue(linearRow.value) - return - } - } - setCommandValue((current) => - rows.some((row) => row.value === current) ? current : rows[0].value - ) - }, [isQueryStale, rows, sourceIntent]) + const resolvedCommandValue = resolveSmartWorkspaceCommandValue({ + currentValue: commandValue, + rows, + isQueryStale, + sourceIntent + }) const loading = githubLoading || gitlabLoading || branchesLoading || linearLoading const ActiveInputIcon = mode === 'text' ? CaseSensitive : loading ? LoaderCircle : Search @@ -963,7 +940,7 @@ export default function SmartWorkspaceNameField({ onOpenChange={(next) => setOpen(disabled || selectedSource ? false : next)} > 0) { - const row = rows.find((entry) => entry.value === commandValue) + const row = rows.find((entry) => entry.value === resolvedCommandValue) if (row) { event.preventDefault() handleSelect(row) diff --git a/src/renderer/src/components/new-workspace/smart-workspace-command-value.test.ts b/src/renderer/src/components/new-workspace/smart-workspace-command-value.test.ts new file mode 100644 index 000000000..6fa37b7c7 --- /dev/null +++ b/src/renderer/src/components/new-workspace/smart-workspace-command-value.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, it } from 'vitest' +import { + resolveSmartWorkspaceCommandValue, + type SmartWorkspaceCommandRow +} from './smart-workspace-command-value' + +function row(kind: SmartWorkspaceCommandRow['kind'], value: string): SmartWorkspaceCommandRow { + return { kind, value } +} + +describe('resolveSmartWorkspaceCommandValue', () => { + it('keeps the current command value when the row still exists', () => { + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'github-12', + rows: [row('use-name', 'use-name-fix'), row('github', 'github-12')], + isQueryStale: false, + sourceIntent: null + }) + ).toBe('github-12') + }) + + it('falls back to the first row when the current value is no longer rendered', () => { + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'github-12', + rows: [row('use-name', 'use-name-fix'), row('branch', 'branch-main')], + isQueryStale: false, + sourceIntent: null + }) + ).toBe('use-name-fix') + }) + + it('uses typed-text rows while source results are stale', () => { + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'github-12', + rows: [row('use-name', 'use-name-fix'), row('github', 'github-12')], + isQueryStale: true, + sourceIntent: null + }) + ).toBe('use-name-fix') + }) + + it('clears selection while stale source-only rows have no typed fallback', () => { + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'github-12', + rows: [row('github', 'github-12')], + isQueryStale: true, + sourceIntent: null + }) + ).toBe('') + }) + + it('prefers matching source-intent rows once fresh results arrive', () => { + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'use-name-123', + rows: [row('use-name', 'use-name-123'), row('github', 'github-123')], + isQueryStale: false, + sourceIntent: 'github' + }) + ).toBe('github-123') + + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'use-name-eng-123', + rows: [row('use-name', 'use-name-eng-123'), row('linear', 'linear-ENG-123')], + isQueryStale: false, + sourceIntent: 'linear' + }) + ).toBe('linear-ENG-123') + }) + + it('leaves the current value alone when no rows are rendered', () => { + expect( + resolveSmartWorkspaceCommandValue({ + currentValue: 'github-12', + rows: [], + isQueryStale: false, + sourceIntent: null + }) + ).toBe('github-12') + }) +}) diff --git a/src/renderer/src/components/new-workspace/smart-workspace-command-value.ts b/src/renderer/src/components/new-workspace/smart-workspace-command-value.ts new file mode 100644 index 000000000..930b79b13 --- /dev/null +++ b/src/renderer/src/components/new-workspace/smart-workspace-command-value.ts @@ -0,0 +1,49 @@ +export type SmartWorkspaceCommandRowKind = + | 'use-name' + | 'create-branch' + | 'github' + | 'gitlab' + | 'branch' + | 'linear' + +export type SmartWorkspaceCommandRow = { + kind: SmartWorkspaceCommandRowKind + value: string +} + +export type SmartWorkspaceSourceIntent = 'github' | 'linear' | null + +export function resolveSmartWorkspaceCommandValue({ + currentValue, + rows, + isQueryStale, + sourceIntent +}: { + currentValue: string + rows: readonly SmartWorkspaceCommandRow[] + isQueryStale: boolean + sourceIntent: SmartWorkspaceSourceIntent +}): string { + if (rows.length === 0) { + return currentValue + } + + if (isQueryStale) { + const typedTextRow = rows.find((row) => row.kind === 'use-name' || row.kind === 'create-branch') + return typedTextRow?.value ?? '' + } + + if (sourceIntent === 'github') { + const githubRow = rows.find((row) => row.kind === 'github') + if (githubRow) { + return githubRow.value + } + } else if (sourceIntent === 'linear') { + const linearRow = rows.find((row) => row.kind === 'linear') + if (linearRow) { + return linearRow.value + } + } + + return rows.some((row) => row.value === currentValue) ? currentValue : rows[0].value +}