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

ComposerCommandMenu.tsx

274 lines8.8 KB

1import {
2 type ProjectEntry,
3 type ProviderKind,
4 type ServerProviderSkill,
5 type ServerProviderSlashCommand,
6} from "@t3tools/contracts";
7import { BotIcon } from "lucide-react";
8import { memo, useLayoutEffect, useMemo, useRef } from "react";
9​
10import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
11import { formatProviderSkillInstallSource } from "~/providerSkillPresentation";
12import { cn } from "~/lib/utils";
13import { Badge } from "../ui/badge";
14import {
15 Command,
16 CommandGroup,
17 CommandGroupLabel,
18 CommandItem,
19 CommandList,
20 CommandSeparator,
21} from "../ui/command";
22import { VscodeEntryIcon } from "./VscodeEntryIcon";
23​
24export type ComposerCommandItem =
25 | {
26 id: string;
27 type: "path";
28 path: string;
29 pathKind: ProjectEntry["kind"];
30 label: string;
31 description: string;
32 }
33 | {
34 id: string;
35 type: "slash-command";
36 command: ComposerSlashCommand;
37 label: string;
38 description: string;
39 }
40 | {
41 id: string;
42 type: "provider-slash-command";
43 provider: ProviderKind;
44 command: ServerProviderSlashCommand;
45 label: string;
46 description: string;
47 }
48 | {
49 id: string;
50 type: "model";
51 provider: ProviderKind;
52 model: string;
53 label: string;
54 description: string;
55 }
56 | {
57 id: string;
58 type: "skill";
59 provider: ProviderKind;
60 skill: ServerProviderSkill;
61 label: string;
62 description: string;
63 };
64​
65type ComposerCommandGroup = {
66 id: string;
67 label: string | null;
68 items: ComposerCommandItem[];
69};
70​
71function SkillGlyph(props: { className?: string }) {
72 return (
73 <svg
74 viewBox="0 0 24 24"
75 fill="none"
76 stroke="currentColor"
77 strokeWidth="1.85"
78 strokeLinecap="round"
79 strokeLinejoin="round"
80 className={props.className}
81 aria-hidden="true"
82 >
83 <path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z" />
84 <path d="m3.3 7 8.7 5 8.7-5" />
85 <path d="M12 22V12" />
86 </svg>
87 );
89​
90function groupCommandItems(
91 items: ComposerCommandItem[],
92 triggerKind: ComposerTriggerKind | null,
93 groupSlashCommandSections: boolean,
94): ComposerCommandGroup[] {
95 if (triggerKind === "skill") {
96 return items.length > 0 ? [{ id: "skills", label: "Skills", items }] : [];
97 }
98 if (triggerKind !== "slash-command" || !groupSlashCommandSections) {
99 return [{ id: "default", label: null, items }];
100 }
101​
102 const builtInItems = items.filter((item) => item.type === "slash-command");
103 const providerItems = items.filter((item) => item.type === "provider-slash-command");
104​
105 const groups: ComposerCommandGroup[] = [];
106 if (builtInItems.length > 0) {
107 groups.push({ id: "built-in", label: "Built-in", items: builtInItems });
108 }
109 if (providerItems.length > 0) {
110 groups.push({ id: "provider", label: "Provider", items: providerItems });
111 }
112 return groups;
114​
115export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
116 items: ComposerCommandItem[];
117 resolvedTheme: "light" | "dark";
118 isLoading: boolean;
119 triggerKind: ComposerTriggerKind | null;
120 groupSlashCommandSections?: boolean;
121 emptyStateText?: string;
122 activeItemId: string | null;
123 onHighlightedItemChange: (itemId: string | null) => void;
124 onSelect: (item: ComposerCommandItem) => void;
125}) {
126 const listRef = useRef<HTMLDivElement>(null);
127 const groups = useMemo(
128 () =>
129 groupCommandItems(props.items, props.triggerKind, props.groupSlashCommandSections ?? true),
130 [props.groupSlashCommandSections, props.items, props.triggerKind],
131 );
132​
133 useLayoutEffect(() => {
134 if (!props.activeItemId || !listRef.current) return;
135 const el = listRef.current.querySelector<HTMLElement>(
136 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
137 );
138 el?.scrollIntoView({ block: "nearest" });
139 }, [props.activeItemId]);
140​
141 return (
142 <Command
143 autoHighlight={false}
144 mode="none"
145 onItemHighlighted={(highlightedValue) => {
146 props.onHighlightedItemChange(
147 typeof highlightedValue === "string" ? highlightedValue : null,
148 );
149 }}
150 >
151 <div
152 ref={listRef}
153 className="relative overflow-hidden rounded-xl border border-border/80 bg-popover/96 shadow-lg/8 backdrop-blur-xs"
154 >
155 <CommandList className="max-h-72">
156 {groups.map((group, groupIndex) => (
157 <div key={group.id}>
158 {groupIndex > 0 ? <CommandSeparator className="my-0.5" /> : null}
159 <CommandGroup>
160 {group.label ? (
161 <CommandGroupLabel className="px-3 pt-2 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-muted-foreground/55">
162 {group.label}
163 </CommandGroupLabel>
164 ) : null}
165 {group.items.map((item) => (
166 <ComposerCommandMenuItem
167 key={item.id}
168 item={item}
169 resolvedTheme={props.resolvedTheme}
170 isActive={props.activeItemId === item.id}
171 onHighlight={props.onHighlightedItemChange}
172 onSelect={props.onSelect}
173 />
174 ))}
175 </CommandGroup>
176 </div>
177 ))}
178 </CommandList>
179 {props.items.length === 0 ? (
180 <div className="px-3 py-2">
181 {props.triggerKind === "skill" ? (
182 <CommandGroup>
183 <CommandGroupLabel className="px-0 pt-0 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-muted-foreground/55">
184 Skills
185 </CommandGroupLabel>
186 <p className="text-muted-foreground/70 text-xs">
187 {props.isLoading
188 ? "Searching workspace skills..."
189 : (props.emptyStateText ??
190 "No skills found. Try / to browse provider commands.")}
191 </p>
192 </CommandGroup>
193 ) : (
194 <p className="text-muted-foreground/70 text-xs">
195 {props.isLoading
196 ? "Searching workspace files..."
197 : (props.emptyStateText ??
198 (props.triggerKind === "path"
199 ? "No matching files or folders."
200 : "No matching command."))}
201 </p>
202 )}
203 </div>
204 ) : null}
205 </div>
206 </Command>
207 );
208});
209​
210const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
211 item: ComposerCommandItem;
212 resolvedTheme: "light" | "dark";
213 isActive: boolean;
214 onHighlight: (itemId: string | null) => void;
215 onSelect: (item: ComposerCommandItem) => void;
216}) {
217 const skillSourceLabel =
218 props.item.type === "skill" ? formatProviderSkillInstallSource(props.item.skill) : null;
219​
220 return (
221 <CommandItem
222 value={props.item.id}
223 data-composer-item-id={props.item.id}
224 className={cn(
225 "cursor-pointer select-none gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
226 props.isActive && "bg-accent! text-accent-foreground!",
227 )}
228 onMouseMove={() => {
229 if (!props.isActive) props.onHighlight(props.item.id);
230 }}
231 onMouseDown={(event) => {
232 event.preventDefault();
233 }}
234 onClick={() => {
235 props.onSelect(props.item);
236 }}
237 >
238 {props.item.type === "path" ? (
239 <VscodeEntryIcon
240 pathValue={props.item.path}
241 kind={props.item.pathKind}
242 theme={props.resolvedTheme}
243 />
244 ) : null}
245 {props.item.type === "slash-command" ? (
246 <BotIcon className="size-4 shrink-0 text-muted-foreground/80" />
247 ) : null}
248 {props.item.type === "provider-slash-command" ? (
249 <span className="inline-flex size-4 shrink-0 items-center justify-center text-muted-foreground/80">
250 <SkillGlyph className="size-3.5" />
251 </span>
252 ) : null}
253 {props.item.type === "skill" ? (
254 <span className="inline-flex size-4 shrink-0 items-center justify-center text-muted-foreground/80">
255 <SkillGlyph className="size-3.5" />
256 </span>
257 ) : null}
258 {props.item.type === "model" ? (
259 <Badge variant="outline" className="px-1.5 py-0 text-[10px]">
260 model
261 </Badge>
262 ) : null}
263 <span className="flex min-w-0 flex-1 items-center gap-2">
264 <span className="shrink-0">{props.item.label}</span>
265 <span className="min-w-0 flex-1 truncate text-muted-foreground/70 text-xs">
266 {props.item.description}
267 </span>
268 </span>
269 {skillSourceLabel ? (
270 <span className="shrink-0 pl-2 text-muted-foreground/70 text-xs">{skillSourceLabel}</span>
271 ) : null}
272 </CommandItem>
273 );
274});