"use client"; import { formatDistanceToNow } from "date-fns"; import { IconArrowRight, IconLoader2, IconSearch } 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 { formatShortcut } from "@/lib/keyboard/utils"; import type { Task } from "@/lib/types/http"; import { FileIcon } from "@/components/ui/file-icon"; const ARCHIVED_STATES = new Set(["COMPLETED", "CANCELLED", "FAILED"]); const MODE_SEARCH_TASKS: CommandPanelMode = "search-tasks"; function getFileName(filePath: string) { return filePath.split("/").pop() ?? filePath; } function CommandItemRow({ cmd, onSelect, }: { cmd: CommandItemType; onSelect: (cmd: CommandItemType) => void; }) { return ( onSelect(cmd)} > {cmd.icon && {cmd.icon}} {cmd.label} {cmd.shortcut && {formatShortcut(cmd.shortcut)}} {cmd.enterMode && ( )} ); } type TaskResultItemProps = { task: Task; stepMap: Map; onSelect: (task: Task) => void; }; function TaskResultItem({ task, stepMap, onSelect }: TaskResultItemProps) { const isArchived = ARCHIVED_STATES.has(task.state); const step = stepMap.get(task.workflow_step_id); return ( onSelect(task)} className={isArchived ? "opacity-60" : ""} >
{task.title} {step && ( {step.name} )} {isArchived && ( {task.state} )}
{task.updated_at && (
{formatDistanceToNow(new Date(task.updated_at), { addSuffix: true })}
)}
); } type CommandsListContentProps = { commands: CommandItemType[]; grouped: [string, CommandItemType[]][]; search: string; onSelect: (cmd: CommandItemType) => void; hasFileResults: boolean; }; function CommandsListContent({ commands, grouped, search, onSelect, hasFileResults, }: CommandsListContentProps) { return ( <> {!hasFileResults && No commands found.} {search.trim() ? ( {commands.map((cmd) => ( ))} ) : ( grouped.map(([group, items]) => ( {items.map((cmd) => ( ))} )) )} ); } type SearchTasksContentProps = { isSearching: boolean; search: string; taskResults: Task[]; stepMap: Map; onTaskSelect: (task: Task) => void; }; function SearchTasksContent({ isSearching, search, taskResults, stepMap, onTaskSelect, }: SearchTasksContentProps) { if (isSearching) return (
); if (search.trim() && taskResults.length === 0) return No tasks found.; if (!search.trim()) return Type to search tasks...; return ( {taskResults.map((task) => ( ))} ); } type FileSearchResultsProps = { files: string[]; isSearching: boolean; onSelect: (path: string) => void; }; function FileSearchResults({ files, isSearching, onSelect }: FileSearchResultsProps) { if (isSearching && files.length === 0) { return (
); } if (files.length === 0) return null; return ( {files.map((filePath) => { const fileName = getFileName(filePath); const lastSlash = filePath.lastIndexOf("/"); const dir = lastSlash > 0 ? filePath.slice(0, lastSlash) : ""; return ( onSelect(filePath)} forceMount > {fileName} {dir && {dir}} ); })} ); } function getInputPlaceholder(mode: CommandPanelMode, inputCommand: CommandItemType | null) { if (mode === "input") return inputCommand?.inputPlaceholder ?? "Enter value..."; if (mode === MODE_SEARCH_TASKS) return "Search for tasks..."; return "Type a command..."; } function getEnterLabel(mode: CommandPanelMode) { if (mode === "input") return "Confirm"; if (mode === MODE_SEARCH_TASKS) return "Open"; return "Select"; } function CommandPanelFooter({ mode }: { mode: CommandPanelMode }) { return (
{mode === "commands" && ( ↑ ↓ Navigate )} ↵ {getEnterLabel(mode)} {mode !== "commands" && ( ⌫ Back )} esc Close
); } export type CommandPanelViewProps = { open: boolean; setOpen: (open: boolean) => void; mode: CommandPanelMode; inputCommand: CommandItemType | null; selectedValue: string; setSelectedValue: (value: string) => void; search: string; setSearch: (value: string) => void; handleKeyDown: (e: React.KeyboardEvent) => void; goBack: () => void; fileResults: string[]; isSearchingFiles: boolean; handleFileSelect: (filePath: string) => void; commands: CommandItemType[]; grouped: Array<[string, CommandItemType[]]>; handleSelect: (cmd: CommandItemType) => void; isSearching: boolean; taskResults: Task[]; stepMap: Map; handleTaskSelect: (task: Task) => void; }; export function CommandPanelView({ open, setOpen, mode, inputCommand, selectedValue, setSelectedValue, search, setSearch, handleKeyDown, goBack, fileResults, isSearchingFiles, handleFileSelect, commands, grouped, handleSelect, isSearching, taskResults, stepMap, handleTaskSelect, }: CommandPanelViewProps) { return (
{mode !== "commands" && ( )}
{mode === "commands" && ( <> {search.trim() && ( )} 0 || isSearchingFiles} /> )} {mode === MODE_SEARCH_TASKS && ( )} {mode === "input" && (!search.trim() ? ( {inputCommand?.inputPlaceholder ?? "Enter a value..."} ) : ( Press Enter to confirm ))}
); }