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 className={cn(
165 "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",
166 props.isActive && "bg-accent! text-accent-foreground!",
167 )}
168 onMouseMove={() => {
169 if (!props.isActive) props.onHighlight(props.item.id);
170 }}
171 onMouseDown={(event) => {
172 event.preventDefault();
173 }}
174 onClick={() => {
175 props.onSelect(props.item);
176 }}
177 >
178 {props.item.type === "path" ? (
179 <PierreEntryIcon
180 pathValue={props.item.path}
181 kind={props.item.pathKind}
182 theme={props.resolvedTheme}
183 />
184 ) : null}
185 {pullRequestPresentation ? (
186 <pullRequestPresentation.Icon
187 role="img"
188 aria-label={pullRequestPresentation.label}
189 className={cn("size-4 shrink-0", pullRequestPresentation.toneClassName)}
190 />
191 ) : null}
192 <span className="flex min-w-0 flex-1 items-center gap-2">
193 <span className="min-w-0 max-w-[45%] shrink-0 truncate font-sans text-xs font-medium">
194 {isSlashSkill ? (
195 <>
196 <span className="text-secondary-label">/skill:</span>
197 {formatProviderSkillDisplayName(isSlashSkill)}
198 </>
199 ) : (
200 props.item.label
201 )}
202 </span>
203 <span className="min-w-0 max-w-[48ch] flex-1 truncate text-left text-secondary-label text-xs">
204 {props.item.description}
205 </span>
206 {skillSourceKind ? (
207 <SkillSourceBadge
208 kind={skillSourceKind}
209 showSkillSuffix={props.triggerKind === "skill"}
210 />
211 ) : null}
212 </span>
213 </CommandItem>
214 );
215});
216
217const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = {
218 app: BlocksIcon,
219 repo: FolderIcon,
220 project: FolderIcon,
221 personal: UserRoundIcon,
222 system: SettingsIcon,
223 other: PackageIcon,
224};
225
226const SKILL_SOURCE_LABEL_BY_KIND: Record<ProviderSkillSourceKind, string> = {
227 app: "App",
228 repo: "Repo",
229 project: "Project",
230 personal: "Personal",
231 system: "System",
232 other: "Provider",
233};
234
235function SkillSourceBadge(props: { kind: ProviderSkillSourceKind; showSkillSuffix: boolean }) {
236 const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind];
237 return (
238 <Badge className="ms-auto" variant="secondary">
239 <Icon aria-hidden="true" className="text-current" />
240 {SKILL_SOURCE_LABEL_BY_KIND[props.kind]}
241 {props.showSkillSuffix ? " Skill" : null}
242 </Badge>
243 );
244}