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 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 items: ComposerCommandItem[];
82 resolvedTheme: "light" | "dark";
83 isLoading: boolean;
84 triggerKind: ComposerTriggerKind | null;
85 emptyStateText?: string;
86 activeItemId: string | null;
87 onHighlightedItemChange: (itemId: string | null) => void;
88 onSelect: (item: ComposerCommandItem) => void;
89}) {
90 const listRef = useRef<HTMLDivElement>(null);
91
92 useLayoutEffect(() => {
93 if (!props.activeItemId || !listRef.current) return;
94 const el = listRef.current.querySelector<HTMLElement>(
95 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
96 );
97 el?.scrollIntoView({ block: "nearest" });
98 }, [props.activeItemId]);
99
100 return (
101 <Command
102 autoHighlight={false}
103 mode="none"
104 onItemHighlighted={(highlightedValue) => {
105 props.onHighlightedItemChange(
106 typeof highlightedValue === "string" ? highlightedValue : null,
107 );
108 }}
109 >
110 <ComposerBanner.Surface
111 ref={listRef}
112 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"
113 data-composer-command-drawer="true"
114 >
115 {props.items.length > 0 ? (
116 <CommandList className="max-h-72 min-h-0 scroll-pb-6">
117 <CommandGroup>
118 {props.items.map((item) => (
119 <ComposerCommandMenuItem
120 key={item.id}
121 item={item}
122 triggerKind={props.triggerKind}
123 resolvedTheme={props.resolvedTheme}
124 isActive={props.activeItemId === item.id}
125 onHighlight={props.onHighlightedItemChange}
126 onSelect={props.onSelect}
127 />
128 ))}
129 </CommandGroup>
130 </CommandList>
131 ) : (
132 <div className="px-5 pt-3.5 pb-7">
133 <p className="text-secondary-label text-xs">
134 {props.isLoading
135 ? props.triggerKind === "skill"
136 ? "Searching workspace skills..."
137 : props.triggerKind === "pull-request"
138 ? "Finding pull request..."
139 : "Searching workspace files..."
140 : (props.emptyStateText ??
141 (props.triggerKind === "skill"
142 ? "No skills found. Try / to browse provider commands."
143 : props.triggerKind === "path"
144 ? "No matching files or folders."
145 : "No matching command."))}
146 </p>
147 </div>
148 )}
149 </ComposerBanner.Surface>
150 </Command>
151 );
152});
153
154const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
155 item: ComposerCommandItem;
156 triggerKind: ComposerTriggerKind | null;
157 resolvedTheme: "light" | "dark";
158 isActive: boolean;
159 onHighlight: (itemId: string | null) => void;
160 onSelect: (item: ComposerCommandItem) => void;
161}) {
162 const skillSourceKind =
163 props.item.type === "skill" ? resolveProviderSkillSourceKind(props.item.skill) : null;
164 const isSlashSkill =
165 props.triggerKind === "slash-command" && props.item.type === "skill" ? props.item.skill : null;
166 const pullRequestPresentation =
167 props.item.type === "pull-request" ? resolvePullRequestState(props.item.pullRequest) : null;
168
169 return (
170 <CommandItem
171 value={props.item.id}
172 data-composer-item-id={props.item.id}
173 className={cn(
174 "cursor-pointer select-none gap-3 rounded-lg px-3 py-2! hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
175 props.isActive && "bg-accent! text-accent-foreground!",
176 )}
177 onMouseMove={() => {
178 if (!props.isActive) props.onHighlight(props.item.id);
179 }}
180 onMouseDown={(event) => {
181 event.preventDefault();
182 }}
183 onClick={() => {
184 props.onSelect(props.item);
185 }}
186 >
187 {props.item.type === "path" ? (
188 <PierreEntryIcon
189 pathValue={props.item.path}
190 kind={props.item.pathKind}
191 theme={props.resolvedTheme}
192 />
193 ) : null}
194 {props.item.type === "thread" ? (
195 <MessagesSquareIcon aria-hidden="true" className="size-4 shrink-0 text-secondary-label" />
196 ) : null}
197 {pullRequestPresentation ? (
198 <pullRequestPresentation.Icon
199 role="img"
200 aria-label={pullRequestPresentation.label}
201 className={cn("size-4 shrink-0", pullRequestPresentation.toneClassName)}
202 />
203 ) : null}
204 <span className="flex min-w-0 flex-1 items-center gap-2">
205 <span className="min-w-0 max-w-[45%] shrink-0 truncate font-sans text-xs font-medium">
206 {isSlashSkill ? (
207 <>
208 <span className="text-secondary-label">/skill:</span>
209 {formatProviderSkillDisplayName(isSlashSkill)}
210 </>
211 ) : (
212 props.item.label
213 )}
214 </span>
215 <span className="min-w-0 max-w-[48ch] flex-1 truncate text-left text-secondary-label text-xs">
216 {props.item.description}
217 </span>
218 {skillSourceKind ? (
219 <SkillSourceBadge
220 kind={skillSourceKind}
221 showSkillSuffix={props.triggerKind === "skill"}
222 />
223 ) : null}
224 </span>
225 </CommandItem>
226 );
227});
228
229const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = {
230 app: BlocksIcon,
231 repo: FolderIcon,
232 project: FolderIcon,
233 personal: UserRoundIcon,
234 system: SettingsIcon,
235 other: PackageIcon,
236};
237
238const SKILL_SOURCE_LABEL_BY_KIND: Record<ProviderSkillSourceKind, string> = {
239 app: "App",
240 repo: "Repo",
241 project: "Project",
242 personal: "Personal",
243 system: "System",
244 other: "Provider",
245};
246
247function SkillSourceBadge(props: { kind: ProviderSkillSourceKind; showSkillSuffix: boolean }) {
248 const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind];
249 return (
250 <Badge className="ms-auto" variant="secondary">
251 <Icon aria-hidden="true" className="text-current" />
252 {SKILL_SOURCE_LABEL_BY_KIND[props.kind]}
253 {props.showSkillSuffix ? " Skill" : null}
254 </Badge>
255 );
256}