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

ComposerCommandMenu.tsx

253 lines7.8 KB

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