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