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 { ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
18import { cn } from "~/lib/utils";
19
20interface CommandPaletteResultsProps {
21 emptyStateMessage?: string;
22 groups: ReadonlyArray<CommandPaletteGroup>;
23 highlightedItemValue?: string | null;
24 isActionsOnly: boolean;
25 keybindings: ResolvedKeybindingsConfig;
26 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
27}
28
29export function CommandPaletteResults(props: CommandPaletteResultsProps) {
30 if (props.groups.length === 0) {
31 return (
32 <div className="py-10 text-center text-sm text-muted-foreground">
33 {props.emptyStateMessage ??
34 (props.isActionsOnly
35 ? "No matching actions."
36 : "No matching commands, projects, or threads.")}
37 </div>
38 );
39 }
40
41 return (
42 <CommandList>
43 {props.groups.map((group) => (
44 <CommandGroup items={group.items} key={group.value}>
45 <CommandGroupLabel className="ps-[9px]">{group.label}</CommandGroupLabel>
46 <CommandCollection>
47 {(item) =>
48 item.disabled ? (
49 <DisabledCommandPaletteResultRow item={item} key={item.value} />
50 ) : (
51 <CommandPaletteResultRow
52 item={item}
53 key={item.value}
54 keybindings={props.keybindings}
55 isActive={props.highlightedItemValue === item.value}
56 onExecuteItem={props.onExecuteItem}
57 />
58 )
59 }
60 </CommandCollection>
61 </CommandGroup>
62 ))}
63 </CommandList>
64 );
65}
66
67function DisabledCommandPaletteResultRow(props: {
68 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
69}) {
70 return (
71 <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">
72 {props.item.icon}
73 {props.item.description || props.item.threadContentMatch ? (
74 <span className="flex min-w-0 flex-1 flex-col">
75 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
76 {props.item.titleLeadingContent}
77 <span className="truncate">{props.item.title}</span>
78 </span>
79 {props.item.threadContentMatch ? (
80 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
81 ) : null}
82 {props.item.description ? (
83 <span className="min-w-0 text-muted-foreground/70 text-xs">
84 {props.item.description}
85 </span>
86 ) : null}
87 </span>
88 ) : (
89 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
90 {props.item.titleLeadingContent}
91 <span className="truncate">{props.item.title}</span>
92 </span>
93 )}
94 {props.item.titleTrailingContent}
95 </div>
96 );
97}
98
99function CommandPaletteResultRow(props: {
100 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
101 isActive: boolean;
102 keybindings: ResolvedKeybindingsConfig;
103 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
104}) {
105 const shortcutLabel = props.item.shortcutCommand
106 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
107 : null;
108
109 return (
110 <CommandItem
111 value={props.item.value}
112 className={cn(
113 "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",
114 props.isActive && "bg-accent! text-accent-foreground!",
115 )}
116 onMouseDown={(event) => {
117 event.preventDefault();
118 }}
119 onClick={() => {
120 props.onExecuteItem(props.item);
121 }}
122 >
123 {props.item.icon}
124 {props.item.description || props.item.threadContentMatch ? (
125 <span className="flex min-w-0 flex-1 flex-col">
126 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
127 {props.item.titleLeadingContent}
128 <span className="truncate">{props.item.title}</span>
129 </span>
130 {props.item.threadContentMatch ? (
131 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
132 ) : null}
133 {props.item.description ? (
134 <span className="min-w-0 text-muted-foreground/70 text-xs">
135 {props.item.description}
136 </span>
137 ) : null}
138 </span>
139 ) : (
140 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
141 {props.item.titleLeadingContent}
142 <span className="truncate">{props.item.title}</span>
143 </span>
144 )}
145 {props.item.titleTrailingContent}
146 {props.item.timestamp ? (
147 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
148 {props.item.timestamp}
149 </span>
150 ) : null}
151 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
152 {props.item.kind === "submenu" ? (
153 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
154 ) : null}
155 </CommandItem>
156 );
157}