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

ComposerCommandMenu.tsx

274 lines8.6 KB

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