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

ComposerCommandMenu.tsx

260 lines8.4 KB

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