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

ComposerCommandMenu.tsx

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