Open History to see the last commit
1import { type ProjectEntry, type ProviderKind } from "@t3tools/contracts";
2import { memo, useLayoutEffect, useRef } 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: string;
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 const listRef = useRef<HTMLDivElement>(null);
45
46 useLayoutEffect(() => {
47 if (!props.activeItemId || !listRef.current) return;
48 const el = listRef.current.querySelector<HTMLElement>(
49 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
50 );
51 el?.scrollIntoView({ block: "nearest" });
52 }, [props.activeItemId]);
53
54 return (
55 <Command
56 autoHighlight={false}
57 mode="none"
58 onItemHighlighted={(highlightedValue) => {
59 props.onHighlightedItemChange(
60 typeof highlightedValue === "string" ? highlightedValue : null,
61 );
62 }}
63 >
64 <div
65 ref={listRef}
66 className="relative overflow-hidden rounded-xl border border-border/80 bg-popover/96 shadow-lg/8 backdrop-blur-xs"
67 >
68 <CommandList className="max-h-64">
69 {props.items.map((item) => (
70 <ComposerCommandMenuItem
71 key={item.id}
72 item={item}
73 resolvedTheme={props.resolvedTheme}
74 isActive={props.activeItemId === item.id}
75 onHighlight={props.onHighlightedItemChange}
76 onSelect={props.onSelect}
77 />
78 ))}
79 </CommandList>
80 {props.items.length === 0 && (
81 <p className="px-3 py-2 text-muted-foreground/70 text-xs">
82 {props.isLoading
83 ? "Searching workspace files..."
84 : props.triggerKind === "path"
85 ? "No matching files or folders."
86 : "No matching command."}
87 </p>
88 )}
89 </div>
90 </Command>
91 );
92});
93
94const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
95 item: ComposerCommandItem;
96 resolvedTheme: "light" | "dark";
97 isActive: boolean;
98 onHighlight: (itemId: string | null) => void;
99 onSelect: (item: ComposerCommandItem) => void;
100}) {
101 return (
102 <CommandItem
103 value={props.item.id}
104 data-composer-item-id={props.item.id}
105 className={cn(
106 "cursor-pointer select-none gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
107 props.isActive && "bg-accent! text-accent-foreground!",
108 )}
109 onMouseMove={() => {
110 if (!props.isActive) props.onHighlight(props.item.id);
111 }}
112 onMouseDown={(event) => {
113 event.preventDefault();
114 }}
115 onClick={() => {
116 props.onSelect(props.item);
117 }}
118 >
119 {props.item.type === "path" ? (
120 <VscodeEntryIcon
121 pathValue={props.item.path}
122 kind={props.item.pathKind}
123 theme={props.resolvedTheme}
124 />
125 ) : null}
126 {props.item.type === "slash-command" ? (
127 <BotIcon className="size-4 text-muted-foreground/80" />
128 ) : null}
129 {props.item.type === "model" ? (
130 <Badge variant="outline" className="px-1.5 py-0 text-[10px]">
131 model
132 </Badge>
133 ) : null}
134 <span className="flex min-w-0 items-center gap-1.5 truncate">
135 <span className="truncate">{props.item.label}</span>
136 </span>
137 <span className="truncate text-muted-foreground/70 text-xs">{props.item.description}</span>
138 </CommandItem>
139 );
140});