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

CommandPaletteResults.tsx

153 lines5.3 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 { ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
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 {group.label ? <CommandGroupLabel>{group.label}</CommandGroupLabel> : null}
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 );
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 || props.item.threadContentMatch ? (
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 {props.item.threadContentMatch ? (
79 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
80 ) : null}
81 {props.item.description ? (
82 <span className="min-w-0 text-muted-foreground/70 text-xs">
83 {props.item.description}
84 </span>
85 ) : null}
86 </span>
87 ) : (
88 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
89 {props.item.titleLeadingContent}
90 <span className="truncate">{props.item.title}</span>
91 </span>
92 )}
93 {props.item.titleTrailingContent}
94 </div>
95 );
97​
98function CommandPaletteResultRow(props: {
99 item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
100 isActive: boolean;
101 keybindings: ResolvedKeybindingsConfig;
102 onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
103}) {
104 const shortcutLabel = props.item.shortcutCommand
105 ? shortcutLabelForCommand(props.keybindings, props.item.shortcutCommand)
106 : null;
107​
108 return (
109 <CommandItem
110 value={props.item.value}
111 active={props.isActive}
112 onMouseDown={(event) => {
113 event.preventDefault();
114 }}
115 onClick={() => {
116 props.onExecuteItem(props.item);
117 }}
118 >
119 {props.item.icon}
120 {props.item.description || props.item.threadContentMatch ? (
121 <span className="flex min-w-0 flex-1 flex-col">
122 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
123 {props.item.titleLeadingContent}
124 <span className="truncate">{props.item.title}</span>
125 </span>
126 {props.item.threadContentMatch ? (
127 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
128 ) : null}
129 {props.item.description ? (
130 <span className="min-w-0 text-muted-foreground/70 text-xs">
131 {props.item.description}
132 </span>
133 ) : null}
134 </span>
135 ) : (
136 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
137 {props.item.titleLeadingContent}
138 <span className="truncate">{props.item.title}</span>
139 </span>
140 )}
141 {props.item.titleTrailingContent}
142 {props.item.timestamp ? (
143 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
144 {props.item.timestamp}
145 </span>
146 ) : null}
147 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
148 {props.item.kind === "submenu" ? (
149 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
150 ) : null}
151 </CommandItem>
152 );