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

ComposerCommandMenu.tsx

295 lines9.4 KB

1import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
2import type { ComposerPathSearchTarget } from "@t3tools/client-runtime/state/threads";
3import {
4 resolveProviderSkillSourceKind,
5 type ProviderSkillSourceKind,
6} from "@t3tools/client-runtime/providerSkills";
7import {
8 type EnvironmentId,
9 type EnvironmentMachineKind,
10 type ProjectEntry,
11 type ProviderDriverKind,
12 type PullRequestContextMetadata,
13 type ScopedThreadRef,
14 type ServerProviderSkill,
15 type ServerProviderSlashCommand,
16} from "@t3tools/contracts";
17import {
18 BlocksIcon,
19 FolderIcon,
20 MessagesSquareIcon,
21 PackageIcon,
22 SettingsIcon,
23 UserRoundIcon,
24 type LucideIcon,
25} from "lucide-react";
26import { memo, useLayoutEffect, useRef } from "react";
27​
28import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
29import { cn } from "~/lib/utils";
30import { Badge } from "../ui/badge";
31import { Command, CommandGroup, CommandItem, CommandList } from "../ui/command";
32import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
33import { PierreEntryIcon } from "./PierreEntryIcon";
34import { ComposerBanner } from "./ComposerBanner";
35import { resolvePullRequestState } from "../pullRequest/pullRequestPresentation";
36​
37export type ComposerCommandItem =
38 | {
39 id: string;
40 type: "path";
41 path: string;
42 pathKind: ProjectEntry["kind"];
43 searchTarget: ComposerPathSearchTarget;
44 label: string;
45 description: string;
46 }
47 | {
48 id: string;
49 type: "slash-command";
50 command: ComposerSlashCommand;
51 label: string;
52 description: string;
53 }
54 | {
55 id: string;
56 type: "provider-slash-command";
57 provider: ProviderDriverKind;
58 command: ServerProviderSlashCommand;
59 label: string;
60 description: string;
61 }
62 | {
63 id: string;
64 type: "skill";
65 provider: ProviderDriverKind;
66 skill: ServerProviderSkill;
67 label: string;
68 description: string;
69 }
70 | {
71 id: string;
72 type: "pull-request";
73 pullRequest: PullRequestContextMetadata;
74 label: string;
75 description: string;
76 }
77 | {
78 id: string;
79 type: "thread";
80 thread: ScopedThreadRef;
81 label: string;
82 description: string;
83 }
84 | {
85 id: string;
86 type: "environment";
87 environmentId: EnvironmentId;
88 machine: EnvironmentMachineKind;
89 label: string;
90 description: string;
91 };
92​
93export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
94 listId: string;
95 items: ComposerCommandItem[];
96 resolvedTheme: "light" | "dark";
97 isLoading: boolean;
98 triggerKind: ComposerTriggerKind | null;
99 emptyStateText?: string;
100 activeItemId: string | null;
101 onHighlightedItemChange: (itemId: string | null) => void;
102 onSelect: (item: ComposerCommandItem) => void;
103}) {
104 const listRef = useRef<HTMLDivElement>(null);
105​
106 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the highlighted menu item's DOM node into view
107 useLayoutEffect(() => {
108 if (!props.activeItemId || !listRef.current) return;
109 const el = listRef.current.querySelector<HTMLElement>(
110 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
111 );
112 el?.scrollIntoView({ block: "nearest" });
113 }, [props.activeItemId]);
114​
115 return (
116 <Command
117 autoHighlight={false}
118 mode="none"
119 onItemHighlighted={(highlightedValue) => {
120 props.onHighlightedItemChange(
121 typeof highlightedValue === "string" ? highlightedValue : null,
122 );
123 }}
124 >
125 <ComposerBanner.Surface
126 ref={listRef}
127 className="flex min-h-0 w-full flex-col overflow-hidden pb-(--chat-composer-attachment-overlap) **:data-[slot=command-item]:rounded-lg **:data-[slot=scroll-area-scrollbar]:data-[orientation=vertical]:my-4"
128 data-composer-command-drawer="true"
129 >
130 {props.items.length > 0 ? (
131 <CommandList
132 id={props.listId}
133 aria-label={props.triggerKind ? LISTBOX_LABEL_BY_TRIGGER[props.triggerKind] : undefined}
134 className="max-h-72 min-h-0 scroll-pb-6"
135 >
136 <CommandGroup>
137 {props.items.map((item) => (
138 <ComposerCommandMenuItem
139 key={item.id}
140 optionId={composerSuggestionOptionId(props.listId, item.id)}
141 item={item}
142 triggerKind={props.triggerKind}
143 resolvedTheme={props.resolvedTheme}
144 isActive={props.activeItemId === item.id}
145 onHighlight={props.onHighlightedItemChange}
146 onSelect={props.onSelect}
147 />
148 ))}
149 </CommandGroup>
150 </CommandList>
151 ) : (
152 <div className="px-5 pt-3.5 pb-7">
153 <p className="text-secondary-label text-xs">
154 {props.isLoading
155 ? props.triggerKind === "skill" || props.triggerKind === "slash-command"
156 ? "Searching workspace skills..."
157 : props.triggerKind === "pull-request"
158 ? "Finding pull request..."
159 : "Searching workspace files..."
160 : (props.emptyStateText ??
161 (props.triggerKind === "skill"
162 ? "No skills found. Try / to browse provider commands."
163 : props.triggerKind === "path"
164 ? "No matching files or folders."
165 : "No matching command."))}
166 </p>
167 </div>
168 )}
169 </ComposerBanner.Surface>
170 </Command>
171 );
172});
173​
174const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
175 optionId: string;
176 item: ComposerCommandItem;
177 triggerKind: ComposerTriggerKind | null;
178 resolvedTheme: "light" | "dark";
179 isActive: boolean;
180 onHighlight: (itemId: string | null) => void;
181 onSelect: (item: ComposerCommandItem) => void;
182}) {
183 const skillSourceKind =
184 props.item.type === "skill" ? resolveProviderSkillSourceKind(props.item.skill) : null;
185 const isSlashSkill =
186 props.triggerKind === "slash-command" && props.item.type === "skill" ? props.item.skill : null;
187 const pullRequestPresentation =
188 props.item.type === "pull-request" ? resolvePullRequestState(props.item.pullRequest) : null;
189​
190 return (
191 <CommandItem
192 render={<div id={props.optionId} />}
193 aria-selected={props.isActive}
194 value={props.item.id}
195 data-composer-item-id={props.item.id}
196 active={props.isActive}
197 onMouseMove={() => {
198 if (!props.isActive) props.onHighlight(props.item.id);
199 }}
200 onMouseDown={(event) => {
201 event.preventDefault();
202 }}
203 onClick={() => {
204 props.onSelect(props.item);
205 }}
206 >
207 {props.item.type === "path" ? (
208 <PierreEntryIcon
209 pathValue={props.item.path}
210 kind={props.item.pathKind}
211 theme={props.resolvedTheme}
212 />
213 ) : null}
214 {props.item.type === "thread" ? (
215 <MessagesSquareIcon aria-hidden="true" className="size-4 shrink-0 text-secondary-label" />
216 ) : null}
217 {props.item.type === "environment" ? (
218 <EnvironmentMachineIcon
219 kind={props.item.machine}
220 aria-hidden="true"
221 className="size-4 shrink-0 text-secondary-label"
222 />
223 ) : null}
224 {pullRequestPresentation ? (
225 <pullRequestPresentation.Icon
226 role="img"
227 aria-label={pullRequestPresentation.label}
228 className={cn("size-4 shrink-0", pullRequestPresentation.toneClassName)}
229 />
230 ) : null}
231 <span className="flex min-w-0 flex-1 items-center gap-2">
232 <span className="min-w-0 max-w-[45%] shrink-0 truncate font-sans text-xs font-medium">
233 {isSlashSkill ? (
234 <>
235 <span className="text-secondary-label">/skill:</span>
236 {formatProviderSkillDisplayName(isSlashSkill)}
237 </>
238 ) : (
239 props.item.label
240 )}
241 </span>
242 <span className="min-w-0 flex-1 truncate text-left text-secondary-label text-xs">
243 {props.item.description}
244 </span>
245 {skillSourceKind ? (
246 <SkillSourceBadge
247 kind={skillSourceKind}
248 showSkillSuffix={props.triggerKind === "skill"}
249 />
250 ) : null}
251 </span>
252 </CommandItem>
253 );
254});
255​
256export function composerSuggestionOptionId(listId: string, itemId: string): string {
257 // JSON escapes lone UTF-16 surrogates before URI encoding without losing identity.
258 return `${listId}-${encodeURIComponent(JSON.stringify(itemId))}`;
260​
261const LISTBOX_LABEL_BY_TRIGGER: Record<ComposerTriggerKind, string> = {
262 path: "Files and folders",
263 "pull-request": "Pull requests",
264 "slash-command": "Commands",
265 skill: "Skills",
266};
267​
268const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = {
269 app: BlocksIcon,
270 repo: FolderIcon,
271 project: FolderIcon,
272 personal: UserRoundIcon,
273 system: SettingsIcon,
274 other: PackageIcon,
275};
276​
277const SKILL_SOURCE_LABEL_BY_KIND: Record<ProviderSkillSourceKind, string> = {
278 app: "App",
279 repo: "Repo",
280 project: "Project",
281 personal: "Personal",
282 system: "System",
283 other: "Provider",
284};
285​
286function SkillSourceBadge(props: { kind: ProviderSkillSourceKind; showSkillSuffix: boolean }) {
287 const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind];
288 return (
289 <Badge className="ms-auto" variant="secondary">
290 <Icon aria-hidden="true" className="text-current" />
291 {SKILL_SOURCE_LABEL_BY_KIND[props.kind]}
292 {props.showSkillSuffix ? " Skill" : null}
293 </Badge>
294 );