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