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

ComposerCommandMenu.tsx

204 lines6.2 KB

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