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