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 ServerProviderSkill,
10 type ServerProviderSlashCommand,
11} from "@t3tools/contracts";
12import {
13 BlocksIcon,
14 FolderIcon,
15 PackageIcon,
16 SettingsIcon,
17 UserRoundIcon,
18 type LucideIcon,
19} from "lucide-react";
20import { memo, useLayoutEffect, useRef } from "react";
21
22import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
23import { cn } from "~/lib/utils";
24import { Badge } from "../ui/badge";
25import { Command, CommandGroup, CommandItem, CommandList } from "../ui/command";
26import { PierreEntryIcon } from "./PierreEntryIcon";
27
28export type ComposerCommandItem =
29 | {
30 id: string;
31 type: "path";
32 path: string;
33 pathKind: ProjectEntry["kind"];
34 label: string;
35 description: string;
36 }
37 | {
38 id: string;
39 type: "slash-command";
40 command: ComposerSlashCommand;
41 label: string;
42 description: string;
43 }
44 | {
45 id: string;
46 type: "provider-slash-command";
47 provider: ProviderDriverKind;
48 command: ServerProviderSlashCommand;
49 label: string;
50 description: string;
51 }
52 | {
53 id: string;
54 type: "skill";
55 provider: ProviderDriverKind;
56 skill: ServerProviderSkill;
57 label: string;
58 description: string;
59 };
60
61export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
62 items: ComposerCommandItem[];
63 resolvedTheme: "light" | "dark";
64 isLoading: boolean;
65 triggerKind: ComposerTriggerKind | null;
66 emptyStateText?: string;
67 activeItemId: string | null;
68 onHighlightedItemChange: (itemId: string | null) => void;
69 onSelect: (item: ComposerCommandItem) => void;
70}) {
71 const listRef = useRef<HTMLDivElement>(null);
72
73 useLayoutEffect(() => {
74 if (!props.activeItemId || !listRef.current) return;
75 const el = listRef.current.querySelector<HTMLElement>(
76 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
77 );
78 el?.scrollIntoView({ block: "nearest" });
79 }, [props.activeItemId]);
80
81 return (
82 <Command
83 autoHighlight={false}
84 mode="none"
85 onItemHighlighted={(highlightedValue) => {
86 props.onHighlightedItemChange(
87 typeof highlightedValue === "string" ? highlightedValue : null,
88 );
89 }}
90 >
91 <div
92 ref={listRef}
93 className="chat-composer-drawer-surface chat-composer-drawer-attached relative w-full overflow-hidden **:data-[slot=scroll-area-scrollbar]:data-[orientation=vertical]:my-4"
94 data-composer-command-drawer="true"
95 >
96 {props.items.length > 0 ? (
97 <CommandList className="max-h-72 scroll-pb-6">
98 <CommandGroup>
99 {props.items.map((item) => (
100 <ComposerCommandMenuItem
101 key={item.id}
102 item={item}
103 triggerKind={props.triggerKind}
104 resolvedTheme={props.resolvedTheme}
105 isActive={props.activeItemId === item.id}
106 onHighlight={props.onHighlightedItemChange}
107 onSelect={props.onSelect}
108 />
109 ))}
110 </CommandGroup>
111 </CommandList>
112 ) : (
113 <div className="px-5 pt-3.5 pb-7">
114 <p className="text-secondary-label text-xs">
115 {props.isLoading
116 ? props.triggerKind === "skill"
117 ? "Searching workspace skills..."
118 : "Searching workspace files..."
119 : (props.emptyStateText ??
120 (props.triggerKind === "skill"
121 ? "No skills found. Try / to browse provider commands."
122 : props.triggerKind === "path"
123 ? "No matching files or folders."
124 : "No matching command."))}
125 </p>
126 </div>
127 )}
128 </div>
129 </Command>
130 );
131});
132
133const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
134 item: ComposerCommandItem;
135 triggerKind: ComposerTriggerKind | null;
136 resolvedTheme: "light" | "dark";
137 isActive: boolean;
138 onHighlight: (itemId: string | null) => void;
139 onSelect: (item: ComposerCommandItem) => void;
140}) {
141 const skillSourceKind =
142 props.item.type === "skill" ? resolveProviderSkillSourceKind(props.item.skill) : null;
143 const isSlashSkill =
144 props.triggerKind === "slash-command" && props.item.type === "skill" ? props.item.skill : null;
145
146 return (
147 <CommandItem
148 value={props.item.id}
149 data-composer-item-id={props.item.id}
150 className={cn(
151 "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",
152 props.isActive && "bg-accent! text-accent-foreground!",
153 )}
154 onMouseMove={() => {
155 if (!props.isActive) props.onHighlight(props.item.id);
156 }}
157 onMouseDown={(event) => {
158 event.preventDefault();
159 }}
160 onClick={() => {
161 props.onSelect(props.item);
162 }}
163 >
164 {props.item.type === "path" ? (
165 <PierreEntryIcon
166 pathValue={props.item.path}
167 kind={props.item.pathKind}
168 theme={props.resolvedTheme}
169 />
170 ) : null}
171 <span className="flex min-w-0 flex-1 items-center gap-2">
172 <span className="min-w-0 max-w-[45%] shrink-0 truncate font-sans text-xs font-medium">
173 {isSlashSkill ? (
174 <>
175 <span className="text-secondary-label">/skill:</span>
176 {formatProviderSkillDisplayName(isSlashSkill)}
177 </>
178 ) : (
179 props.item.label
180 )}
181 </span>
182 <span className="min-w-0 max-w-[48ch] flex-1 truncate text-left text-secondary-label text-xs">
183 {props.item.description}
184 </span>
185 {skillSourceKind ? (
186 <SkillSourceBadge
187 kind={skillSourceKind}
188 showSkillSuffix={props.triggerKind === "skill"}
189 />
190 ) : null}
191 </span>
192 </CommandItem>
193 );
194});
195
196const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = {
197 app: BlocksIcon,
198 repo: FolderIcon,
199 project: FolderIcon,
200 personal: UserRoundIcon,
201 system: SettingsIcon,
202 other: PackageIcon,
203};
204
205const SKILL_SOURCE_LABEL_BY_KIND: Record<ProviderSkillSourceKind, string> = {
206 app: "App",
207 repo: "Repo",
208 project: "Project",
209 personal: "Personal",
210 system: "System",
211 other: "Provider",
212};
213
214function SkillSourceBadge(props: { kind: ProviderSkillSourceKind; showSkillSuffix: boolean }) {
215 const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind];
216 return (
217 <Badge className="ms-auto" variant="secondary">
218 <Icon aria-hidden="true" className="text-current" />
219 {SKILL_SOURCE_LABEL_BY_KIND[props.kind]}
220 {props.showSkillSuffix ? " Skill" : null}
221 </Badge>
222 );
223}