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

CommandPaletteResults.tsx

154 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 <CommandGroupLabel className="ps-[9px]">{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 );
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 className="gap-2"
113 onMouseDown={(event) => {
114 event.preventDefault();
115 }}
116 onClick={() => {
117 props.onExecuteItem(props.item);
118 }}
119 >
120 {props.item.icon}
121 {props.item.description || props.item.threadContentMatch ? (
122 <span className="flex min-w-0 flex-1 flex-col">
123 <span className="flex min-w-0 items-center gap-1.5 text-sm text-foreground">
124 {props.item.titleLeadingContent}
125 <span className="truncate">{props.item.title}</span>
126 </span>
127 {props.item.threadContentMatch ? (
128 <ThreadSearchMatchExcerpt match={props.item.threadContentMatch} />
129 ) : null}
130 {props.item.description ? (
131 <span className="min-w-0 text-muted-foreground/70 text-xs">
132 {props.item.description}
133 </span>
134 ) : null}
135 </span>
136 ) : (
137 <span className="flex min-w-0 flex-1 items-center gap-1.5 text-sm text-foreground">
138 {props.item.titleLeadingContent}
139 <span className="truncate">{props.item.title}</span>
140 </span>
141 )}
142 {props.item.titleTrailingContent}
143 {props.item.timestamp ? (
144 <span className="min-w-12 shrink-0 text-right text-xs tabular-nums text-muted-foreground/70">
145 {props.item.timestamp}
146 </span>
147 ) : null}
148 {shortcutLabel ? <CommandShortcut>{shortcutLabel}</CommandShortcut> : null}
149 {props.item.kind === "submenu" ? (
150 <ChevronRightIcon className="-me-0.5 ms-auto size-4 shrink-0 text-muted-foreground/70" />
151 ) : null}
152 </CommandItem>
153 );