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

ComposerCommandMenu.tsx

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