Open History to see the last commit
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;
26}
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 item.disabled ? (
48 <DisabledCommandPaletteResultRow item={item} key={item.value} />
49 ) : (
50 <CommandPaletteResultRow
51 item={item}
52 key={item.value}
53 keybindings={props.keybindings}
54 isActive={props.highlightedItemValue === item.value}
55 onExecuteItem={props.onExecuteItem}
56 />
57 )
58 }
59 </CommandCollection>
60 </CommandGroup>
61 ))}
62 </CommandList>
63 );
64}
65
66function DisabledCommandPaletteResultRow(props: {
67 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
68}) {
69 return (
70 <div className="flex min-h-8 select-none items-center gap-2 rounded-sm px-2 py-1.5 text-base opacity-64 sm:min-h-7 sm:text-sm">
71 {props.item.icon}
72 {props.item.description ? (
73 <span className="flex min-w-0 flex-1 flex-col">
74 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
75 {props.item.titleLeadingContent}
76 <span className="truncate">{props.item.title}</span>
77 </span>
78 <span className="truncate text-muted-foreground/70 text-xs">
79 {props.item.description}
80 </span>
81 </span>
82 ) : (
83 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
84 {props.item.titleLeadingContent}
85 <span className="truncate">{props.item.title}</span>
86 </span>
87 )}
88 {props.item.titleTrailingContent}
89 </div>
90 );
91}
92
93function CommandPaletteResultRow(props: {
94 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
95 isActive: boolean;
96 keybindings: ResolvedKeybindingsConfig;
97 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
98}) {
99 const shortcutLabel = props.item.shortcutCommand
100 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
101 : null;
102
103 return (
104 <CommandItem
105 value={props.item.value}
106 className={cn(
107 "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",
108 props.isActive && "bg-accent! text-accent-foreground!",
109 )}
110 onMouseDown={(event) => {
111 event.preventDefault();
112 }}
113 onClick={() => {
114 props.onExecuteItem(props.item);
115 }}
116 >
117 {props.item.icon}
118 {props.item.description ? (
119 <span className="flex min-w-0 flex-1 flex-col">
120 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
121 {props.item.titleLeadingContent}
122 <span className="truncate">{props.item.title}</span>
123 </span>
124 <span className="truncate text-muted-foreground/70 text-xs">
125 {props.item.description}
126 </span>
127 </span>
128 ) : (
129 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
130 {props.item.titleLeadingContent}
131 <span className="truncate">{props.item.title}</span>
132 </span>
133 )}
134 {props.item.titleTrailingContent}
135 {props.item.timestamp ? (
136 <span className="min-w-12 shrink-0 text-right text-[10px] tabular-nums text-muted-foreground/70">
137 {props.item.timestamp}
138 </span>
139 ) : null}
140 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
141 {props.item.kind === "submenu" ? (
142 <ChevronRightIcon className="ml-auto size-4 shrink-0 text-muted-foreground/50" />
143 ) : null}
144 </CommandItem>
145 );
146}