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

ComposerCommandMenu.tsx

261 lines8.6 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 { PierreEntryIcon } from "./PierreEntryIcon";
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="dropdown-glass relative w-full overflow-hidden rounded-[20px] shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] **:data-[slot=scroll-area-scrollbar]:data-[orientation=vertical]:my-4 dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]"
145 >
146 {props.items.length > 0 ? (
147 <CommandList className="max-h-72 not-empty:py-3">
148 {groups.map((group, groupIndex) => (
149 <div key={group.id}>
150 {groupIndex > 0 ? <CommandSeparator className="my-0.5" /> : null}
151 <CommandGroup>
152 {group.label ? (
153 <CommandGroupLabel className="px-3 pt-2 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-secondary-label">
154 {group.label}
155 </CommandGroupLabel>
156 ) : null}
157 {group.items.map((item) => (
158 <ComposerCommandMenuItem
159 key={item.id}
160 item={item}
161 resolvedTheme={props.resolvedTheme}
162 isActive={props.activeItemId === item.id}
163 onHighlight={props.onHighlightedItemChange}
164 onSelect={props.onSelect}
165 />
166 ))}
167 </CommandGroup>
168 </div>
169 ))}
170 </CommandList>
171 ) : (
172 <div className="px-5 py-3.5">
173 {props.triggerKind === "skill" ? (
174 <CommandGroup>
175 <CommandGroupLabel className="px-0 pt-0 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-secondary-label">
176 Skills
177 </CommandGroupLabel>
178 <p className="text-secondary-label text-xs">
179 {props.isLoading
180 ? "Searching workspace skills..."
181 : (props.emptyStateText ??
182 "No skills found. Try / to browse provider commands.")}
183 </p>
184 </CommandGroup>
185 ) : (
186 <p className="text-secondary-label text-xs">
187 {props.isLoading
188 ? "Searching workspace files..."
189 : (props.emptyStateText ??
190 (props.triggerKind === "path"
191 ? "No matching files or folders."
192 : "No matching command."))}
193 </p>
194 )}
195 </div>
196 )}
197 </div>
198 </Command>
199 );
200});
201​
202const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
203 item: ComposerCommandItem;
204 resolvedTheme: "light" | "dark";
205 isActive: boolean;
206 onHighlight: (itemId: string | null) => void;
207 onSelect: (item: ComposerCommandItem) => void;
208}) {
209 const skillSourceLabel =
210 props.item.type === "skill" ? formatProviderSkillInstallSource(props.item.skill) : null;
211​
212 return (
213 <CommandItem
214 value={props.item.id}
215 data-composer-item-id={props.item.id}
216 className={cn(
217 "cursor-pointer select-none gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
218 props.isActive && "bg-accent! text-accent-foreground!",
219 )}
220 onMouseMove={() => {
221 if (!props.isActive) props.onHighlight(props.item.id);
222 }}
223 onMouseDown={(event) => {
224 event.preventDefault();
225 }}
226 onClick={() => {
227 props.onSelect(props.item);
228 }}
229 >
230 {props.item.type === "path" ? (
231 <PierreEntryIcon
232 pathValue={props.item.path}
233 kind={props.item.pathKind}
234 theme={props.resolvedTheme}
235 />
236 ) : null}
237 {props.item.type === "slash-command" ? (
238 <BotIcon className="size-4 shrink-0 text-icon-muted" />
239 ) : null}
240 {props.item.type === "provider-slash-command" ? (
241 <span className="inline-flex size-4 shrink-0 items-center justify-center text-icon-muted">
242 <SkillGlyph className="size-3.5" />
243 </span>
244 ) : null}
245 {props.item.type === "skill" ? (
246 <span className="inline-flex size-4 shrink-0 items-center justify-center text-icon-muted">
247 <SkillGlyph className="size-3.5" />
248 </span>
249 ) : null}
250 <span className="flex min-w-0 flex-1 items-center gap-2">
251 <span className="shrink-0">{props.item.label}</span>
252 <span className="min-w-0 flex-1 truncate text-secondary-label text-xs">
253 {props.item.description}
254 </span>
255 </span>
256 {skillSourceLabel ? (
257 <span className="shrink-0 pl-2 text-secondary-label text-xs">{skillSourceLabel}</span>
258 ) : null}
259 </CommandItem>
260 );
261});