diff --git a/apps/web/components/command-panel-content-search.test.tsx b/apps/web/components/command-panel-content-search.test.tsx index 0519189806..286132944e 100644 --- a/apps/web/components/command-panel-content-search.test.tsx +++ b/apps/web/components/command-panel-content-search.test.tsx @@ -2,6 +2,8 @@ import { cleanup, fireEvent, render, screen } from "@testing-library/react"; import type { ReactNode } from "react"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { WorkspaceContentSearchResult } from "@/lib/types/backend"; +import type { Task } from "@/lib/types/http"; +import { taskId, workflowId, workspaceId } from "@/lib/types/ids"; vi.mock("@kandev/ui/command", () => ({ Command: ({ children, shouldFilter }: { children: ReactNode; shouldFilter?: boolean }) => ( @@ -66,7 +68,13 @@ vi.mock("@kandev/ui/command", () => ({ /> ), - CommandItem: ({ children }: { children: ReactNode }) =>
{children}
, + // Forwards onSelect: without it the mock silently swallows every selection + // handler and any assertion about picking a row would prove nothing. + CommandItem: ({ children, onSelect }: { children: ReactNode; onSelect?: () => void }) => ( +
+ {children} +
+ ), CommandList: ({ children }: { children: ReactNode }) =>
{children}
, CommandShortcut: ({ children }: { children: ReactNode }) => {children}, })); @@ -109,6 +117,7 @@ import { CommandPanelView, MODE_COMMANDS, MODE_SEARCH_CONTENT, + MODE_SEARCH_TASKS, type CommandPanelViewProps, } from "./command-panel-footer"; @@ -185,14 +194,21 @@ describe("CommandPanelView task content search mode", () => { fireEvent.click(screen.getByTestId("mock-content-search")); expect(props.handleContentSelect).toHaveBeenCalledWith(result); }); +}); +describe("CommandPanelView scope switcher", () => { it("makes all palette scopes visible and switches without clearing the query", () => { const onScopeChange = vi.fn(); const props = viewProps({ onScopeChange }); render(); const tabs = screen.getAllByRole("tab"); - expect(tabs).toHaveLength(3); + expect(tabs.map((tab) => tab.getAttribute("aria-label"))).toEqual([ + "Commands", + "Tasks", + "Files", + "Contents", + ]); expect( screen.getByRole("tab", { name: "Commands" }).getAttribute(ARIA_SELECTED_ATTRIBUTE), ).toBe("false"); @@ -262,7 +278,7 @@ describe("CommandPanelView task content search mode", () => { expect(document.activeElement).toBe(input); }); - it("hides workspace modes and leaves Tab alone outside a task workbench", () => { + it("hides workspace modes but keeps commands and tasks outside a task workbench", () => { const onScopeChange = vi.fn(); render( { ); const input = screen.getByRole("combobox"); - expect(screen.queryByRole("tablist", { name: "Command palette mode" })).toBeNull(); - expect(screen.queryByText("Switch mode")).toBeNull(); - expect(fireEvent.keyDown(input, { key: "Tab" })).toBe(true); - expect(onScopeChange).not.toHaveBeenCalled(); + expect(screen.getAllByRole("tab").map((tab) => tab.getAttribute("aria-label"))).toEqual([ + "Commands", + "Tasks", + ]); + // Tab still cycles, but only between the two scopes that need no worktree. + expect(fireEvent.keyDown(input, { key: "Tab" })).toBe(false); + expect(onScopeChange).toHaveBeenLastCalledWith("search-tasks"); + expect(fireEvent.keyDown(input, { key: "Tab", shiftKey: true })).toBe(false); + expect(onScopeChange).toHaveBeenLastCalledWith("search-tasks"); }); }); @@ -496,3 +517,103 @@ describe("CommandPanelView confirmations", () => { expect(setOpen).toHaveBeenCalledWith(false); }); }); + +function task(id: string, title: string): Task { + return { + id: taskId(id), + workspace_id: workspaceId("workspace-1"), + workflow_id: workflowId("workflow-1"), + workflow_step_id: "step-1", + position: 0, + title, + description: "", + state: "IN_PROGRESS", + priority: "medium", + created_at: "2026-08-24T09:00:00Z", + updated_at: "2026-08-24T09:00:00Z", + }; +} + +describe("CommandPanelView commands scope ordering", () => { + const archiveCommand = { + id: "task-archive", + label: "Archive task", + group: "Task", + action: vi.fn(), + }; + + function groupHeadings() { + return Array.from(document.querySelectorAll(CMDK_GROUP_HEADING_SELECTOR)).map( + (heading) => heading.textContent, + ); + } + + it("puts matching commands above the task preview once the user types", () => { + render( + , + ); + + expect(groupHeadings()).toEqual(["Commands", "Tasks"]); + }); + + it("keeps the active task list on top while nothing is typed", () => { + render( + , + ); + + expect(groupHeadings()).toEqual(["Active Tasks", "Task"]); + }); +}); + +describe("CommandPanelView tasks scope", () => { + it("lists task results and opens the one that is picked", () => { + const target = task("task-1", "Palette task"); + const props = viewProps({ + mode: MODE_SEARCH_TASKS, + search: "palette", + taskResults: [target], + }); + render(); + + expect(screen.getByPlaceholderText("Search for tasks...")).toBeTruthy(); + expect(screen.getByText("Palette task")).toBeTruthy(); + expect(screen.getByRole("tab", { name: "Tasks" }).getAttribute(ARIA_SELECTED_ATTRIBUTE)).toBe( + "true", + ); + + fireEvent.click(screen.getByText("Palette task")); + expect(props.handleTaskSelect).toHaveBeenCalledWith(target); + }); + + it("reports an empty task search instead of falling back to commands", () => { + render( + , + ); + + expect(screen.getByText("No tasks found.")).toBeTruthy(); + expect(screen.queryByText("Go Home")).toBeNull(); + }); +}); diff --git a/apps/web/components/command-panel-footer.tsx b/apps/web/components/command-panel-footer.tsx index 1b39bd69b8..cb575c233c 100644 --- a/apps/web/components/command-panel-footer.tsx +++ b/apps/web/components/command-panel-footer.tsx @@ -3,33 +3,18 @@ import { useEffect, type Dispatch, type SetStateAction } from "react"; import { useTranslation } from "react-i18next"; import type { TFunction } from "i18next"; -import { formatTimeDistance, useDateLocale } from "@/lib/i18n/date-locale"; -import { IconArchive, IconArrowRight, IconHammer, IconLoader2 } from "@tabler/icons-react"; import { Command, CommandDialog, CommandEmpty, - CommandGroup, CommandInput, - CommandItem, CommandList, - CommandShortcut, } from "@kandev/ui/command"; import { Kbd, KbdGroup } from "@kandev/ui/kbd"; -import { Badge } from "@kandev/ui/badge"; import type { CommandPanelMode, CommandItem as CommandItemType } from "@/lib/commands/types"; -import { - getCommandSearchTerms, - scoreCommandSearch, - sortCommandsForSearch, -} from "@/lib/commands/search"; -import { formatShortcut } from "@/lib/keyboard/utils"; import type { Task } from "@/lib/types/http"; import type { FileSearchResult } from "@/lib/types/backend"; -import { FileIcon } from "@/components/ui/file-icon"; import { WorkspaceContentSearch } from "@/components/workspace-content-search"; -import { useRepoDisplayName } from "@/hooks/domains/session/use-repo-display-name"; -import { groupByRepositoryName, isSingleRepoGroup } from "@/lib/group-by-repo"; import { CommandPanelScopeSwitcher, getAdjacentCommandPanelScope, @@ -43,312 +28,25 @@ import { dismissCommandConfirmation, getCommandConfirmationState, } from "@/components/command-panel-confirmation"; +import { + CommandsListContent, + FileSearchContent, + MODE_COMMANDS, + MODE_SEARCH_CONTENT, + MODE_SEARCH_FILES, + MODE_SEARCH_TASKS, + TaskSearchContent, + type StepMap, +} from "@/components/command-panel-results"; -const ARCHIVED_STATES = new Set(["COMPLETED", "CANCELLED", "FAILED"]); -export const MODE_COMMANDS: CommandPanelMode = "commands"; -export const MODE_SEARCH_FILES: CommandPanelMode = "search-files"; -export const MODE_SEARCH_CONTENT: CommandPanelMode = "search-content"; - -const STEP_COLOR_MAP: Record = { - "bg-slate-500": "#64748b", - "bg-red-500": "#ef4444", - "bg-orange-500": "#f97316", - "bg-yellow-500": "#eab308", - "bg-green-500": "#22c55e", - "bg-cyan-500": "#06b6d4", - "bg-blue-500": "#3b82f6", - "bg-indigo-500": "#6366f1", - "bg-purple-500": "#a855f7", -}; - -function getFileName(filePath: string) { - return filePath.split("/").pop() ?? filePath; -} - -export function getTaskResultValue(task: Task) { - return `__task:${task.id} ${task.title}`; -} - -export function getFileResultValue(filePath: string) { - return `__file:${filePath}`; -} - -function CommandItemRow({ - cmd, - onSelect, -}: { - cmd: CommandItemType; - onSelect: (cmd: CommandItemType) => void; -}) { - return ( - onSelect(cmd)} - > - {cmd.icon && ( - - {cmd.icon} - - )} - - {cmd.label} - {cmd.context && ( - - {cmd.context} - - )} - - {cmd.shortcut && {formatShortcut(cmd.shortcut)}} - {cmd.enterMode && ( - - - - )} - - ); -} - -type TaskResultItemProps = { - task: Task; - stepMap: Map; - repoMap: Map; - onSelect: (task: Task) => void; -}; - -function TaskResultItem({ task, stepMap, repoMap, onSelect }: TaskResultItemProps) { - const locale = useDateLocale(); - const isArchived = ARCHIVED_STATES.has(task.state); - const step = stepMap.get(task.workflow_step_id); - const stepHex = step ? STEP_COLOR_MAP[step.color] : undefined; - const rawPath = - task.primary_working_directory ?? - (task.repositories?.[0] ? repoMap.get(task.repositories[0].repository_id) : undefined); - const workDir = rawPath ? getFileName(rawPath) : undefined; - const details: string[] = []; - if (workDir) details.push(workDir); - if (task.primary_agent_name) details.push(task.primary_agent_name); - if (task.updated_at) { - details.push(formatTimeDistance(task.updated_at, locale)); - } - return ( - onSelect(task)} - className={isArchived ? "opacity-60" : ""} - forceMount - > -
- {isArchived ? ( - - ) : ( - - )} - {task.title} - {step && ( - - {step.name} - - )} - {details.length > 0 && ( - - {details.join(" · ")} - - )} -
-
- ); -} - -type CommandsListContentProps = { - commands: CommandItemType[]; - grouped: [string, CommandItemType[]][]; - search: string; - onSelect: (cmd: CommandItemType) => void; - taskResults: Task[]; - isSearching: boolean; - stepMap: Map; - repoMap: Map; - onTaskSelect: (task: Task) => void; -}; - -function SearchCommandGroups({ - commands, - search, - onSelect, -}: Pick) { - const { t } = useTranslation(); - const matches = sortCommandsForSearch(commands, search).filter( - (cmd) => scoreCommandSearch(cmd.id, search, getCommandSearchTerms(cmd)) > 0, - ); - const regularMatches = matches.filter((cmd) => !cmd.searchOnly); - const searchOnlyGroups = new Map(); - - for (const cmd of matches) { - if (!cmd.searchOnly) continue; - const groupItems = searchOnlyGroups.get(cmd.group) ?? []; - groupItems.push(cmd); - searchOnlyGroups.set(cmd.group, groupItems); - } - - return ( - <> - {regularMatches.length > 0 && ( - - {regularMatches.map((cmd) => ( - - ))} - - )} - {[...searchOnlyGroups].map(([group, items]) => ( - - {items.map((cmd) => ( - - ))} - - ))} - - ); -} - -function CommandsListContent({ - commands, - grouped, - search, - onSelect, - taskResults, - isSearching, - stepMap, - repoMap, - onTaskSelect, -}: CommandsListContentProps) { - const { t } = useTranslation(); - const hasInlineResults = taskResults.length > 0 || isSearching; - return ( - <> - {!hasInlineResults && !isSearching && ( - {t("common:noCommandsFound")} - )} - {isSearching && taskResults.length === 0 && ( - -
- -
-
- )} - {taskResults.length > 0 && ( - - {taskResults.map((task) => ( - - ))} - - )} - {search.trim() ? ( - - ) : ( - grouped.map(([group, items]) => { - const visibleItems = items.filter((cmd) => !cmd.searchOnly); - if (visibleItems.length === 0) return null; - return ( - - {visibleItems.map((cmd) => ( - - ))} - - ); - }) - )} - - ); -} - -type FileSearchContentProps = { - files: FileSearchResult[]; - isSearching: boolean; - search: string; - sessionId: string | null; - onSelect: (path: string) => void; -}; - -function FileSearchContent({ - files, - isSearching, - search, - sessionId, - onSelect, -}: FileSearchContentProps) { - const { t } = useTranslation(); - const getRepoDisplayName = useRepoDisplayName(sessionId); - if (isSearching && files.length === 0) { - return ( -
- -
- ); - } - if (search.trim() && files.length === 0) - return {t("common:noFilesFound")}; - if (!search.trim()) return {t("common:typeToSearchFiles")}; - - const groups = groupByRepositoryName(files, (file) => file.repository_name); - const singleRepo = isSingleRepoGroup(groups); - return groups.map((group) => ( - - {group.items.map((file) => { - const repositoryPrefix = file.repository_name ? `${file.repository_name}/` : ""; - const displayPath = file.path.startsWith(repositoryPrefix) - ? file.path.slice(repositoryPrefix.length) - : file.path; - const fileName = getFileName(displayPath); - const lastSlash = displayPath.lastIndexOf("/"); - const dir = lastSlash > 0 ? displayPath.slice(0, lastSlash) : ""; - return ( - onSelect(file.path)} - forceMount - > - - {fileName} - {dir && {dir}} - - ); - })} - - )); -} +export { + MODE_COMMANDS, + MODE_SEARCH_CONTENT, + MODE_SEARCH_FILES, + MODE_SEARCH_TASKS, + getFileResultValue, + getTaskResultValue, +} from "@/components/command-panel-results"; // `mode` stays an untranslated discriminant — it is compared with `===` // throughout. Only the labels it selects are copy. @@ -358,7 +56,7 @@ function getInputPlaceholder( inputCommand: CommandItemType | null, ) { if (mode === "input") return inputCommand?.inputPlaceholder ?? t("common:enterValue"); - if (mode === "search-tasks") return t("common:searchForTasks"); + if (mode === MODE_SEARCH_TASKS) return t("common:searchForTasks"); if (mode === MODE_SEARCH_FILES) return t("common:searchForFiles"); if (mode === MODE_SEARCH_CONTENT) return t("common:searchTaskContents"); return t("common:typeACommand"); @@ -366,7 +64,7 @@ function getInputPlaceholder( function getEnterLabel(t: TFunction, mode: CommandPanelMode) { if (mode === "input") return t("common:confirm"); - if (mode === "search-tasks" || mode === MODE_SEARCH_FILES || mode === MODE_SEARCH_CONTENT) { + if (mode === MODE_SEARCH_TASKS || mode === MODE_SEARCH_FILES || mode === MODE_SEARCH_CONTENT) { return t("common:open"); } return t("common:select"); @@ -374,22 +72,15 @@ function getEnterLabel(t: TFunction, mode: CommandPanelMode) { function getModeLabel(t: TFunction, mode: CommandPanelMode, inputCommand: CommandItemType | null) { if (mode === "input") return inputCommand?.label; - if (mode === "search-tasks") return t("common:tasks"); + if (mode === MODE_SEARCH_TASKS) return t("common:tasks"); if (mode === MODE_SEARCH_FILES) return t("common:files"); if (mode === MODE_SEARCH_CONTENT) return t("common:contents"); return null; } -function CommandPanelFooter({ - mode, - workspaceSearchAvailable, -}: { - mode: CommandPanelMode; - workspaceSearchAvailable: boolean; -}) { +function CommandPanelFooter({ mode }: { mode: CommandPanelMode }) { const { t } = useTranslation(); const isScopeMode = isCommandPanelScopeMode(mode); - const canSwitchScope = workspaceSearchAvailable && isScopeMode; return (
{isScopeMode && ( @@ -399,12 +90,10 @@ function CommandPanelFooter({ ↓ {t("common:navigate")} - {canSwitchScope && ( - - Tab - {t("common:switchMode")} - - )} + + Tab + {t("common:switchMode")} + )} @@ -452,7 +141,7 @@ export type CommandPanelViewProps = { handleSelect: (cmd: CommandItemType) => void; isSearching: boolean; taskResults: Task[]; - stepMap: Map; + stepMap: StepMap; repoMap: Map; handleTaskSelect: (task: Task) => void; }; @@ -469,24 +158,27 @@ function CommandPanelInputHeader({ }: CommandPanelViewProps) { const { t } = useTranslation(); const isTopLevelMode = isCommandPanelScopeMode(mode); - const canSwitchScope = workspaceSearchAvailable && isTopLevelMode; const modeLabel = getModeLabel(t, mode, inputCommand); const onInputKeyDown = (event: React.KeyboardEvent) => { if ( - canSwitchScope && + isTopLevelMode && event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey ) { event.preventDefault(); - onScopeChange(getAdjacentCommandPanelScope(mode, event.shiftKey)); + onScopeChange(getAdjacentCommandPanelScope(mode, event.shiftKey, workspaceSearchAvailable)); return; } handleKeyDown(event); }; return ( -
+ // Four scope tabs do not fit beside the input on a phone, and the tablist + // does not shrink, so the input would collapse to a few characters. The + // min-width forces the tablist onto its own row instead of squeezing the + // query out of view. +
{!isTopLevelMode && (
); } @@ -552,6 +250,16 @@ function CommandPanelResultList(props: CommandPanelViewProps) { onTaskSelect={handleTaskSelect} /> )} + {mode === MODE_SEARCH_TASKS && ( + + )} {mode === MODE_SEARCH_FILES && ( - + ); diff --git a/apps/web/components/command-panel-results.tsx b/apps/web/components/command-panel-results.tsx new file mode 100644 index 0000000000..4343a2190c --- /dev/null +++ b/apps/web/components/command-panel-results.tsx @@ -0,0 +1,429 @@ +"use client"; + +import { useTranslation } from "react-i18next"; +import { formatTimeDistance, useDateLocale } from "@/lib/i18n/date-locale"; +import { IconArchive, IconArrowRight, IconHammer, IconLoader2 } from "@tabler/icons-react"; +import { CommandEmpty, CommandGroup, CommandItem, CommandShortcut } from "@kandev/ui/command"; +import { Badge } from "@kandev/ui/badge"; +import type { CommandPanelMode, CommandItem as CommandItemType } from "@/lib/commands/types"; +import { + getCommandSearchTerms, + scoreCommandSearch, + sortCommandsForSearch, +} from "@/lib/commands/search"; +import { formatShortcut } from "@/lib/keyboard/utils"; +import type { Task } from "@/lib/types/http"; +import type { FileSearchResult } from "@/lib/types/backend"; +import { FileIcon } from "@/components/ui/file-icon"; +import { useRepoDisplayName } from "@/hooks/domains/session/use-repo-display-name"; +import { groupByRepositoryName, isSingleRepoGroup } from "@/lib/group-by-repo"; + +const ARCHIVED_STATES = new Set(["COMPLETED", "CANCELLED", "FAILED"]); + +export const MODE_COMMANDS: CommandPanelMode = "commands"; +export const MODE_SEARCH_TASKS: CommandPanelMode = "search-tasks"; +export const MODE_SEARCH_FILES: CommandPanelMode = "search-files"; +export const MODE_SEARCH_CONTENT: CommandPanelMode = "search-content"; + +const STEP_COLOR_MAP: Record = { + "bg-slate-500": "#64748b", + "bg-red-500": "#ef4444", + "bg-orange-500": "#f97316", + "bg-yellow-500": "#eab308", + "bg-green-500": "#22c55e", + "bg-cyan-500": "#06b6d4", + "bg-blue-500": "#3b82f6", + "bg-indigo-500": "#6366f1", + "bg-purple-500": "#a855f7", +}; + +function getFileName(filePath: string) { + return filePath.split("/").pop() ?? filePath; +} + +export function getTaskResultValue(task: Task) { + return `__task:${task.id} ${task.title}`; +} + +export function getFileResultValue(filePath: string) { + return `__file:${filePath}`; +} + +export type StepMap = Map; + +function CommandItemRow({ + cmd, + onSelect, +}: { + cmd: CommandItemType; + onSelect: (cmd: CommandItemType) => void; +}) { + return ( + onSelect(cmd)} + > + {cmd.icon && ( + + {cmd.icon} + + )} + + {cmd.label} + {cmd.context && ( + + {cmd.context} + + )} + + {cmd.shortcut && {formatShortcut(cmd.shortcut)}} + {cmd.enterMode && ( + + + + )} + + ); +} + +type TaskResultItemProps = { + task: Task; + stepMap: StepMap; + repoMap: Map; + onSelect: (task: Task) => void; +}; + +function TaskResultItem({ task, stepMap, repoMap, onSelect }: TaskResultItemProps) { + const locale = useDateLocale(); + const isArchived = ARCHIVED_STATES.has(task.state); + const step = stepMap.get(task.workflow_step_id); + const stepHex = step ? STEP_COLOR_MAP[step.color] : undefined; + const rawPath = + task.primary_working_directory ?? + (task.repositories?.[0] ? repoMap.get(task.repositories[0].repository_id) : undefined); + const workDir = rawPath ? getFileName(rawPath) : undefined; + const details: string[] = []; + if (workDir) details.push(workDir); + if (task.primary_agent_name) details.push(task.primary_agent_name); + if (task.updated_at) { + details.push(formatTimeDistance(task.updated_at, locale)); + } + return ( + onSelect(task)} + className={isArchived ? "opacity-60" : ""} + forceMount + > +
+ {isArchived ? ( + + ) : ( + + )} + {/* The title identifies the row, so it takes the free space and the + non-shrinking badge and metadata cannot squeeze it away. Metadata is + secondary and steps aside entirely on a phone. */} + {task.title} + {step && ( + + {step.name} + + )} + {details.length > 0 && ( + + {details.join(" · ")} + + )} +
+
+ ); +} + +type TaskResultGroupProps = { + tasks: Task[]; + search: string; + stepMap: StepMap; + repoMap: Map; + onSelect: (task: Task) => void; + testId?: string; +}; + +function TaskResultGroup({ + tasks, + search, + stepMap, + repoMap, + onSelect, + testId, +}: TaskResultGroupProps) { + const { t } = useTranslation(); + if (tasks.length === 0) return null; + return ( + + {tasks.map((task) => ( + + ))} + + ); +} + +function TaskResultsLoader() { + const { t } = useTranslation(); + return ( + +
+ +
+
+ ); +} + +type CommandsListContentProps = { + commands: CommandItemType[]; + grouped: [string, CommandItemType[]][]; + search: string; + onSelect: (cmd: CommandItemType) => void; + taskResults: Task[]; + isSearching: boolean; + stepMap: StepMap; + repoMap: Map; + onTaskSelect: (task: Task) => void; +}; + +function SearchCommandGroups({ + commands, + search, + onSelect, +}: Pick) { + const { t } = useTranslation(); + const matches = sortCommandsForSearch(commands, search).filter( + (cmd) => scoreCommandSearch(cmd.id, search, getCommandSearchTerms(cmd)) > 0, + ); + const regularMatches = matches.filter((cmd) => !cmd.searchOnly); + const searchOnlyGroups = new Map(); + + for (const cmd of matches) { + if (!cmd.searchOnly) continue; + const groupItems = searchOnlyGroups.get(cmd.group) ?? []; + groupItems.push(cmd); + searchOnlyGroups.set(cmd.group, groupItems); + } + + return ( + <> + {regularMatches.length > 0 && ( + + {regularMatches.map((cmd) => ( + + ))} + + )} + {[...searchOnlyGroups].map(([group, items]) => ( + + {items.map((cmd) => ( + + ))} + + ))} + + ); +} + +function IdleCommandGroups({ + grouped, + onSelect, +}: Pick) { + return ( + <> + {grouped.map(([group, items]) => { + const visibleItems = items.filter((cmd) => !cmd.searchOnly); + if (visibleItems.length === 0) return null; + return ( + + {visibleItems.map((cmd) => ( + + ))} + + ); + })} + + ); +} + +/** + * The commands scope mixes commands with task rows. Idle, the Active Tasks + * group leads: reopening the palette is usually a jump back to work, and there + * is no query to rank commands against. Once the user types, the ordering + * flips — a typed query is a command query first, so matching commands lead and + * the fuzzy task matches trail as a preview. The Tasks scope owns the full + * task search. + */ +export function CommandsListContent({ + commands, + grouped, + search, + onSelect, + taskResults, + isSearching, + stepMap, + repoMap, + onTaskSelect, +}: CommandsListContentProps) { + const { t } = useTranslation(); + const hasQuery = Boolean(search.trim()); + const hasInlineResults = taskResults.length > 0 || isSearching; + const taskRows = ( + <> + {isSearching && taskResults.length === 0 && } + + + ); + const commandRows = hasQuery ? ( + + ) : ( + + ); + return ( + <> + {!hasInlineResults && {t("common:noCommandsFound")}} + {hasQuery ? commandRows : taskRows} + {hasQuery ? taskRows : commandRows} + + ); +} + +type TaskSearchContentProps = { + tasks: Task[]; + isSearching: boolean; + search: string; + stepMap: StepMap; + repoMap: Map; + onSelect: (task: Task) => void; +}; + +export function TaskSearchContent({ + tasks, + isSearching, + search, + stepMap, + repoMap, + onSelect, +}: TaskSearchContentProps) { + const { t } = useTranslation(); + if (isSearching && tasks.length === 0) { + return ( +
+ +
+ ); + } + if (tasks.length === 0) return {t("common:noTasksFound")}; + return ( + + ); +} + +type FileSearchContentProps = { + files: FileSearchResult[]; + isSearching: boolean; + search: string; + sessionId: string | null; + onSelect: (path: string) => void; +}; + +export function FileSearchContent({ + files, + isSearching, + search, + sessionId, + onSelect, +}: FileSearchContentProps) { + const { t } = useTranslation(); + const getRepoDisplayName = useRepoDisplayName(sessionId); + if (isSearching && files.length === 0) { + return ( +
+ +
+ ); + } + if (search.trim() && files.length === 0) + return {t("common:noFilesFound")}; + if (!search.trim()) return {t("common:typeToSearchFiles")}; + + const groups = groupByRepositoryName(files, (file) => file.repository_name); + const singleRepo = isSingleRepoGroup(groups); + return groups.map((group) => ( + + {group.items.map((file) => { + const repositoryPrefix = file.repository_name ? `${file.repository_name}/` : ""; + const displayPath = file.path.startsWith(repositoryPrefix) + ? file.path.slice(repositoryPrefix.length) + : file.path; + const fileName = getFileName(displayPath); + const lastSlash = displayPath.lastIndexOf("/"); + const dir = lastSlash > 0 ? displayPath.slice(0, lastSlash) : ""; + return ( + onSelect(file.path)} + forceMount + > + + {fileName} + {dir && {dir}} + + ); + })} + + )); +} diff --git a/apps/web/components/command-panel-scope-switcher.tsx b/apps/web/components/command-panel-scope-switcher.tsx index 47f94dfd16..76a7f06e85 100644 --- a/apps/web/components/command-panel-scope-switcher.tsx +++ b/apps/web/components/command-panel-scope-switcher.tsx @@ -8,7 +8,7 @@ import { formatShortcut } from "@/lib/keyboard/utils"; import { cn } from "@/lib/utils"; import { useTranslation } from "react-i18next"; -export type CommandPanelScopeMode = "commands" | "search-files" | "search-content"; +export type CommandPanelScopeMode = "commands" | "search-tasks" | "search-files" | "search-content"; type ScopeOption = { mode: CommandPanelScopeMode; @@ -18,16 +18,26 @@ type ScopeOption = { * threaded through the command-panel state. */ labelKey: string; - shortcutId: ConfigurableShortcutId; + /** Omitted for scopes that are reachable by click and Tab but unbound. */ + shortcutId?: ConfigurableShortcutId; + /** Scopes that read a checked-out worktree, so they need an open session. */ + requiresWorkspace?: boolean; }; const SCOPE_OPTIONS: ScopeOption[] = [ { mode: "commands", labelKey: "common:scopeCommands", shortcutId: "SEARCH" }, - { mode: "search-files", labelKey: "common:scopeFiles", shortcutId: "FILE_SEARCH" }, + { mode: "search-tasks", labelKey: "common:scopeTasks" }, + { + mode: "search-files", + labelKey: "common:scopeFiles", + shortcutId: "FILE_SEARCH", + requiresWorkspace: true, + }, { mode: "search-content", labelKey: "common:scopeContents", shortcutId: "CONTENT_SEARCH", + requiresWorkspace: true, }, ]; @@ -35,22 +45,37 @@ export function isCommandPanelScopeMode(mode: CommandPanelMode): mode is Command return SCOPE_OPTIONS.some((scope) => scope.mode === mode); } +function availableScopes(workspaceSearchAvailable: boolean): ScopeOption[] { + return SCOPE_OPTIONS.filter((scope) => workspaceSearchAvailable || !scope.requiresWorkspace); +} + +/** Commands and Tasks are always reachable, so the switcher always has tabs. */ +export function getAvailableCommandPanelScopes( + workspaceSearchAvailable: boolean, +): CommandPanelScopeMode[] { + return availableScopes(workspaceSearchAvailable).map((scope) => scope.mode); +} + export function getAdjacentCommandPanelScope( mode: CommandPanelScopeMode, reverse = false, + workspaceSearchAvailable = true, ): CommandPanelScopeMode { - const currentIndex = SCOPE_OPTIONS.findIndex((scope) => scope.mode === mode); + const scopes = availableScopes(workspaceSearchAvailable); + const currentIndex = scopes.findIndex((scope) => scope.mode === mode); const offset = reverse ? -1 : 1; - const nextIndex = (currentIndex + offset + SCOPE_OPTIONS.length) % SCOPE_OPTIONS.length; - return SCOPE_OPTIONS[nextIndex].mode; + const nextIndex = (currentIndex + offset + scopes.length) % scopes.length; + return scopes[nextIndex].mode; } export function CommandPanelScopeSwitcher({ mode, onScopeChange, + workspaceSearchAvailable, }: { mode: CommandPanelScopeMode; onScopeChange: (mode: CommandPanelScopeMode) => void; + workspaceSearchAvailable: boolean; }) { const { t } = useTranslation(); const keyboardShortcuts = useAppStore((state) => state.userSettings.keyboardShortcuts); @@ -59,11 +84,13 @@ export function CommandPanelScopeSwitcher({
- {SCOPE_OPTIONS.map((scope) => { + {availableScopes(workspaceSearchAvailable).map((scope) => { const active = mode === scope.mode; - const shortcut = formatShortcut(getShortcut(scope.shortcutId, keyboardShortcuts)); + const shortcut = scope.shortcutId + ? formatShortcut(getShortcut(scope.shortcutId, keyboardShortcuts)) + : null; const label = t(scope.labelKey); return (