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

ComposerStashMenu.tsx

176 lines7.4 KB

1import { BookmarkIcon, XIcon } from "lucide-react";
2import { memo, useEffect, useState } from "react";
3​
4import { formatRelativeTimeLabel } from "../../timestampFormat";
5import { cn } from "~/lib/utils";
6import { type PromptStashEntry } from "../../promptStashStore";
7import { Command, CommandGroup, CommandGroupLabel, CommandItem, CommandList } from "../ui/command";
8import { Button } from "../ui/button";
9​
10const SNIPPET_MAX_CHARS = 90;
11​
12/** Images that did not make it into the entry, whatever the reason. */
13function missingImageCount(entry: PromptStashEntry): number {
14 return entry.droppedImageNames.length + (entry.unreadableImageNames?.length ?? 0);
16​
17function stashEntrySnippet(entry: PromptStashEntry): string {
18 const trimmed = entry.prompt.trim().replace(/\s+/g, " ");
19 if (trimmed.length > 0) {
20 return trimmed.length > SNIPPET_MAX_CHARS ? `${trimmed.slice(0, SNIPPET_MAX_CHARS)}…` : trimmed;
21 }
22 const imageCount = entry.attachments.length + entry.droppedImageNames.length;
23 return imageCount > 0 ? `(${imageCount} image${imageCount === 1 ? "" : "s"})` : "(empty)";
25​
26/**
27 * Popover listing the stashed prompts. Keyboard-first: opened by ⌘S on an
28 * empty composer, navigated with arrows, restored with Enter, dismissed
29 * with Escape. The listener runs capture-phase on window so it wins over
30 * the Lexical editor's handlers while the menu is open.
31 */
32export const ComposerStashMenu = memo(function ComposerStashMenu(props: {
33 entries: ReadonlyArray<PromptStashEntry>;
34 onRestore: (entry: PromptStashEntry) => void;
35 onDelete: (entry: PromptStashEntry) => void;
36 onClose: () => void;
37}) {
38 const { entries, onRestore, onDelete, onClose } = props;
39 const [highlightedId, setHighlightedId] = useState<string | null>(entries[0]?.id ?? null);
40​
41 const highlightedEntry = entries.find((entry) => entry.id === highlightedId) ?? entries[0];
42​
43 useEffect(() => {
44 if (entries.length === 0) return;
45 if (!entries.some((entry) => entry.id === highlightedId)) {
46 setHighlightedId(entries[0]?.id ?? null);
47 }
48 }, [entries, highlightedId]);
49​
50 useEffect(() => {
51 const handler = (event: KeyboardEvent) => {
52 if (event.key === "Escape") {
53 event.preventDefault();
54 event.stopPropagation();
55 onClose();
56 return;
57 }
58 if (event.key === "ArrowDown" || event.key === "ArrowUp") {
59 if (entries.length === 0) return;
60 event.preventDefault();
61 event.stopPropagation();
62 const currentIndex = entries.findIndex((entry) => entry.id === highlightedId);
63 const offset = event.key === "ArrowDown" ? 1 : -1;
64 const normalizedIndex = currentIndex >= 0 ? currentIndex : offset === 1 ? -1 : 0;
65 const nextIndex = (normalizedIndex + offset + entries.length) % entries.length;
66 setHighlightedId(entries[nextIndex]?.id ?? null);
67 return;
68 }
69 if (event.key === "Enter") {
70 // A focused control inside the row (the delete button) owns its own
71 // activation; swallowing Enter here would restore instead of delete.
72 if (event.target instanceof HTMLElement && event.target.closest("button[aria-label]")) {
73 return;
74 }
75 if (!highlightedEntry) return;
76 event.preventDefault();
77 event.stopPropagation();
78 onRestore(highlightedEntry);
79 return;
80 }
81 if (event.key === "Backspace" && (event.metaKey || event.ctrlKey)) {
82 if (!highlightedEntry) return;
83 event.preventDefault();
84 event.stopPropagation();
85 onDelete(highlightedEntry);
86 }
87 };
88 window.addEventListener("keydown", handler, true);
89 return () => window.removeEventListener("keydown", handler, true);
90 }, [entries, highlightedEntry, highlightedId, onClose, onDelete, onRestore]);
91​
92 return (
93 <Command autoHighlight={false} mode="none">
94 <div className="dropdown-glass relative w-full overflow-hidden rounded-[20px]">
95 <CommandList className="max-h-72">
96 <CommandGroup>
97 <CommandGroupLabel className="flex items-center gap-1.5 px-3 pt-2 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-muted-foreground/55">
98 <BookmarkIcon className="size-3" aria-hidden="true" />
99 Stashed prompts
100 </CommandGroupLabel>
101 {entries.length === 0 ? (
102 <p className="px-3 pb-3 pt-1 text-muted-foreground/70 text-xs">
103 Nothing stashed yet. Press ⌘S with a prompt in the composer to stash it.
104 </p>
105 ) : (
106 entries.map((entry) => (
107 <CommandItem
108 key={entry.id}
109 value={entry.id}
110 className={cn(
111 "group/stash cursor-pointer select-none gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
112 highlightedId === entry.id && "bg-accent! text-accent-foreground!",
113 )}
114 onMouseMove={() => {
115 if (highlightedId !== entry.id) setHighlightedId(entry.id);
116 }}
117 onMouseDown={(event) => {
118 event.preventDefault();
119 }}
120 onClick={() => {
121 onRestore(entry);
122 }}
123 >
124 {entry.attachments.length > 0 ? (
125 <span className="flex shrink-0 items-center -space-x-1.5">
126 {entry.attachments.slice(0, 3).map((attachment) => (
127 <img
128 key={attachment.id}
129 src={attachment.dataUrl}
130 alt=""
131 aria-hidden="true"
132 className="size-5 rounded border border-border/70 object-cover"
133 />
134 ))}
135 </span>
136 ) : (
137 <BookmarkIcon className="size-4 shrink-0 text-muted-foreground/60" />
138 )}
139 <span className="min-w-0 flex-1 truncate text-sm">
140 {stashEntrySnippet(entry)}
141 </span>
142 {entry.pendingImageCount ? (
143 <span className="shrink-0 text-[10px] text-muted-foreground/60">
144 saving {entry.pendingImageCount} image
145 {entry.pendingImageCount === 1 ? "" : "s"}…
146 </span>
147 ) : missingImageCount(entry) > 0 ? (
148 <span className="shrink-0 text-[10px] text-amber-600">
149 {missingImageCount(entry)} image
150 {missingImageCount(entry) === 1 ? "" : "s"} dropped
151 </span>
152 ) : null}
153 <span className="shrink-0 text-muted-foreground/60 text-xs">
154 {formatRelativeTimeLabel(entry.createdAt)}
155 </span>
156 <Button
157 variant="ghost"
158 size="icon-xs"
159 className="shrink-0 opacity-0 transition-opacity group-hover/stash:opacity-100"
160 aria-label="Delete stashed prompt"
161 onClick={(event) => {
162 event.stopPropagation();
163 onDelete(entry);
164 }}
165 >
166 <XIcon />
167 </Button>
168 </CommandItem>
169 ))
170 )}
171 </CommandGroup>
172 </CommandList>
173 </div>
174 </Command>
175 );
176});