Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerCommandMenu.tsx

224 lines6.9 KB

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