import { type ResolvedKeybindingsConfig } from "@t3tools/contracts"; import { ChevronRightIcon } from "lucide-react"; import { shortcutLabelForCommand } from "../keybindings"; import { type CommandPaletteActionItem, type CommandPaletteGroup, type CommandPaletteSubmenuItem, } from "./CommandPalette.logic"; import { CommandCollection, CommandGroup, CommandGroupLabel, CommandItem, CommandList, CommandShortcut, } from "./ui/command"; import { cn } from "~/lib/utils"; function foldAsciiCase(value: string): string { return value.replace(/[A-Z]/g, (character) => character.toLowerCase()); } function HighlightedSearchText(props: { text: string; query: string }) { const query = props.query.trim(); if (query.length === 0) return props.text; const normalizedText = foldAsciiCase(props.text); const normalizedQuery = foldAsciiCase(query); const parts: Array<{ readonly text: string; readonly highlighted: boolean; readonly start: number; }> = []; let cursor = 0; while (cursor < props.text.length) { const matchIndex = normalizedText.indexOf(normalizedQuery, cursor); if (matchIndex === -1) { parts.push({ text: props.text.slice(cursor), highlighted: false, start: cursor }); break; } if (matchIndex > cursor) { parts.push({ text: props.text.slice(cursor, matchIndex), highlighted: false, start: cursor, }); } parts.push({ text: props.text.slice(matchIndex, matchIndex + query.length), highlighted: true, start: matchIndex, }); cursor = matchIndex + query.length; } return parts.map((part) => part.highlighted ? ( {part.text} ) : ( part.text ), ); } function ThreadContentMatch(props: { match: NonNullable; }) { const isUser = props.match.source === "user"; return ( {isUser ? "You:" : "Agent:"} {" "} ); } interface CommandPaletteResultsProps { emptyStateMessage?: string; groups: ReadonlyArray; highlightedItemValue?: string | null; isActionsOnly: boolean; keybindings: ResolvedKeybindingsConfig; onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void; } export function CommandPaletteResults(props: CommandPaletteResultsProps) { if (props.groups.length === 0) { return (
{props.emptyStateMessage ?? (props.isActionsOnly ? "No matching actions." : "No matching commands, projects, or threads.")}
); } return ( {props.groups.map((group) => ( {group.label} {(item) => item.disabled ? ( ) : ( ) } ))} ); } function DisabledCommandPaletteResultRow(props: { item: CommandPaletteActionItem | CommandPaletteSubmenuItem; }) { return (
{props.item.icon} {props.item.description || props.item.threadContentMatch ? ( {props.item.titleLeadingContent} {props.item.title} {props.item.threadContentMatch ? ( ) : null} {props.item.description ? ( {props.item.description} ) : null} ) : ( {props.item.titleLeadingContent} {props.item.title} )} {props.item.titleTrailingContent}
); } function CommandPaletteResultRow(props: { item: CommandPaletteActionItem | CommandPaletteSubmenuItem; isActive: boolean; keybindings: ResolvedKeybindingsConfig; onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void; }) { const shortcutLabel = props.item.shortcutCommand ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand) : null; return ( { event.preventDefault(); }} onClick={() => { props.onExecuteItem(props.item); }} > {props.item.icon} {props.item.description || props.item.threadContentMatch ? ( {props.item.titleLeadingContent} {props.item.title} {props.item.threadContentMatch ? ( ) : null} {props.item.description ? ( {props.item.description} ) : null} ) : ( {props.item.titleLeadingContent} {props.item.title} )} {props.item.titleTrailingContent} {props.item.timestamp ? ( {props.item.timestamp} ) : null} {shortcutLabel ? {shortcutLabel} : null} {props.item.kind === "submenu" ? ( ) : null} ); }