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

ComposerCommandMenu.tsx

120 lines3.6 KB

1import { type ProjectEntry, type ModelSlug, type ProviderKind } from "@t3tools/contracts";
2import { memo } from "react";
3import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
4import { BotIcon } from "lucide-react";
5import { cn } from "~/lib/utils";
6import { Badge } from "../ui/badge";
7import { Command, CommandItem, CommandList } from "../ui/command";
8import { VscodeEntryIcon } from "./VscodeEntryIcon";
9​
10export type ComposerCommandItem =
11 | {
12 id: string;
13 type: "path";
14 path: string;
15 pathKind: ProjectEntry["kind"];
16 label: string;
17 description: string;
18 }
19 | {
20 id: string;
21 type: "slash-command";
22 command: ComposerSlashCommand;
23 label: string;
24 description: string;
25 }
26 | {
27 id: string;
28 type: "model";
29 provider: ProviderKind;
30 model: ModelSlug;
31 label: string;
32 description: string;
33 };
34​
35export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
36 items: ComposerCommandItem[];
37 resolvedTheme: "light" | "dark";
38 isLoading: boolean;
39 triggerKind: ComposerTriggerKind | null;
40 activeItemId: string | null;
41 onHighlightedItemChange: (itemId: string | null) => void;
42 onSelect: (item: ComposerCommandItem) => void;
43}) {
44 return (
45 <Command
46 mode="none"
47 onItemHighlighted={(highlightedValue) => {
48 props.onHighlightedItemChange(
49 typeof highlightedValue === "string" ? highlightedValue : null,
50 );
51 }}
52 >
53 <div className="relative overflow-hidden rounded-xl border border-border/80 bg-popover/96 shadow-lg/8 backdrop-blur-xs">
54 <CommandList className="max-h-64">
55 {props.items.map((item) => (
56 <ComposerCommandMenuItem
57 key={item.id}
58 item={item}
59 resolvedTheme={props.resolvedTheme}
60 isActive={props.activeItemId === item.id}
61 onSelect={props.onSelect}
62 />
63 ))}
64 </CommandList>
65 {props.items.length === 0 && (
66 <p className="px-3 py-2 text-muted-foreground/70 text-xs">
67 {props.isLoading
68 ? "Searching workspace files..."
69 : props.triggerKind === "path"
70 ? "No matching files or folders."
71 : "No matching command."}
72 </p>
73 )}
74 </div>
75 </Command>
76 );
77});
78​
79const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
80 item: ComposerCommandItem;
81 resolvedTheme: "light" | "dark";
82 isActive: boolean;
83 onSelect: (item: ComposerCommandItem) => void;
84}) {
85 return (
86 <CommandItem
87 value={props.item.id}
88 className={cn(
89 "cursor-pointer select-none gap-2",
90 props.isActive && "bg-accent text-accent-foreground",
91 )}
92 onMouseDown={(event) => {
93 event.preventDefault();
94 }}
95 onClick={() => {
96 props.onSelect(props.item);
97 }}
98 >
99 {props.item.type === "path" ? (
100 <VscodeEntryIcon
101 pathValue={props.item.path}
102 kind={props.item.pathKind}
103 theme={props.resolvedTheme}
104 />
105 ) : null}
106 {props.item.type === "slash-command" ? (
107 <BotIcon className="size-4 text-muted-foreground/80" />
108 ) : null}
109 {props.item.type === "model" ? (
110 <Badge variant="outline" className="px-1.5 py-0 text-[10px]">
111 model
112 </Badge>
113 ) : null}
114 <span className="flex min-w-0 items-center gap-1.5 truncate">
115 <span className="truncate">{props.item.label}</span>
116 </span>
117 <span className="truncate text-muted-foreground/70 text-xs">{props.item.description}</span>
118 </CommandItem>
119 );
120});