Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CommandPaletteResults.tsx

110 lines3.7 KB

1import { type ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { ChevronRightIcon } from "lucide-react";
3import { shortcutLabelForCommand } from "../keybindings";
4import {
5 type CommandPaletteActionItem,
6 type CommandPaletteGroup,
7 type CommandPaletteSubmenuItem,
8} from "./CommandPalette.logic";
9import {
10 CommandCollection,
11 CommandGroup,
12 CommandGroupLabel,
13 CommandItem,
14 CommandList,
15 CommandShortcut,
16} from "./ui/command";
17import { cn } from "~/lib/utils";
18​
19interface CommandPaletteResultsProps {
20 emptyStateMessage?: string;
21 groups: ReadonlyArray<CommandPaletteGroup>;
22 highlightedItemValue?: string | null;
23 isActionsOnly: boolean;
24 keybindings: ResolvedKeybindingsConfig;
25 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
27​
28export function CommandPaletteResults(props: CommandPaletteResultsProps) {
29 if (props.groups.length === 0) {
30 return (
31 <div className="py-10 text-center text-sm text-muted-foreground">
32 {props.emptyStateMessage ??
33 (props.isActionsOnly
34 ? "No matching actions."
35 : "No matching commands, projects, or threads.")}
36 </div>
37 );
38 }
39​
40 return (
41 <CommandList>
42 {props.groups.map((group) => (
43 <CommandGroup items={group.items} key={group.value}>
44 <CommandGroupLabel>{group.label}</CommandGroupLabel>
45 <CommandCollection>
46 {(item) => (
47 <CommandPaletteResultRow
48 item={item}
49 key={item.value}
50 keybindings={props.keybindings}
51 isActive={props.highlightedItemValue === item.value}
52 onExecuteItem={props.onExecuteItem}
53 />
54 )}
55 </CommandCollection>
56 </CommandGroup>
57 ))}
58 </CommandList>
59 );
61​
62function CommandPaletteResultRow(props: {
63 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
64 isActive: boolean;
65 keybindings: ResolvedKeybindingsConfig;
66 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
67}) {
68 const shortcutLabel = props.item.shortcutCommand
69 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
70 : null;
71​
72 return (
73 <CommandItem
74 value={props.item.value}
75 className={cn(
76 "cursor-pointer gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit data-selected:bg-transparent data-selected:text-inherit [&[data-highlighted][data-selected]]:bg-transparent [&[data-highlighted][data-selected]]:text-inherit",
77 props.isActive && "bg-accent! text-accent-foreground!",
78 )}
79 onMouseDown={(event) => {
80 event.preventDefault();
81 }}
82 onClick={() => {
83 props.onExecuteItem(props.item);
84 }}
85 >
86 {props.item.icon}
87 {props.item.description ? (
88 <span className="flex min-w-0 flex-1 flex-col">
89 <span className="truncate text-sm text-foreground">{props.item.title}</span>
90 <span className="truncate text-muted-foreground/70 text-xs">
91 {props.item.description}
92 </span>
93 </span>
94 ) : (
95 <span className="flex min-w-0 items-center gap-1.5 truncate text-sm text-foreground">
96 <span className="truncate">{props.item.title}</span>
97 </span>
98 )}
99 {props.item.timestamp ? (
100 <span className="min-w-12 shrink-0 text-right text-[10px] tabular-nums text-muted-foreground/70">
101 {props.item.timestamp}
102 </span>
103 ) : null}
104 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
105 {props.item.kind === "submenu" ? (
106 <ChevronRightIcon className="ml-auto size-4 shrink-0 text-muted-foreground/50" />
107 ) : null}
108 </CommandItem>
109 );