Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 200fa82

Browse files
t3dotggclaude
andauthored
feat(web): prompt stash — cmd+S saves the composer to a per-provider queue (pingdotgg#4453)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent f4c3943 commit 200fa82

11 files changed

Lines changed: 1784 additions & 1 deletion

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 448 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { BookmarkIcon } from "lucide-react";
2+
import { memo } from "react";
3+
4+
import { cn } from "~/lib/utils";
5+
6+
/**
7+
* Bookmark pill perched on the composer's top-right shoulder. Shows the
8+
* current method's stash count and doubles as the click target for opening
9+
* the stash menu.
10+
*
11+
* On save the badge gives one quiet acknowledgement: it lifts to full
12+
* opacity and the count ticks over. `pulseKey` changes per stash, remounting
13+
* the count so the transition replays without a continuous animation.
14+
*/
15+
export const ComposerStashBadge = memo(function ComposerStashBadge(props: {
16+
count: number;
17+
pulseKey: number;
18+
pulsing: boolean;
19+
menuOpen: boolean;
20+
onToggleMenu: () => void;
21+
}) {
22+
if (props.count === 0) return null;
23+
24+
return (
25+
<button
26+
type="button"
27+
data-prompt-stash-badge="true"
28+
aria-label={`Stashed prompts: ${props.count}. Open stash.`}
29+
aria-expanded={props.menuOpen}
30+
className={cn(
31+
"absolute -top-3 right-4 z-10 inline-flex cursor-pointer items-center gap-1.5 rounded-full border bg-popover px-2.5 py-0.5 text-xs shadow-sm",
32+
"transition-[color,border-color,opacity] duration-200",
33+
props.menuOpen || props.pulsing
34+
? "border-border text-foreground opacity-100"
35+
: "border-border/70 text-muted-foreground opacity-70 hover:opacity-100 hover:text-foreground",
36+
)}
37+
onPointerDown={(event) => {
38+
// Keep composer focus so Escape/typing flows stay intact.
39+
event.preventDefault();
40+
}}
41+
onClick={props.onToggleMenu}
42+
>
43+
<BookmarkIcon className="size-3" aria-hidden="true" />
44+
Stash
45+
<span
46+
key={props.pulseKey}
47+
className={cn(
48+
"rounded-full px-1.5 text-[10px] font-medium tabular-nums",
49+
props.pulsing
50+
? "prompt-stash-count-enter bg-primary text-primary-foreground"
51+
: "bg-muted text-muted-foreground",
52+
)}
53+
>
54+
{props.count}
55+
</span>
56+
</button>
57+
);
58+
});
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
});

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ interface ComposerDraftStoreState {
495495
attachments: PersistedComposerImageAttachment[],
496496
) => void;
497497
clearComposerContent: (threadRef: ComposerThreadTarget) => void;
498+
/**
499+
* Clears only the prompt text and image attachments, preserving terminal /
500+
* element contexts, preview annotations, and review comments. Used by the
501+
* prompt stash, which can only round-trip text + images: clearing the
502+
* session-bound contexts would destroy state nothing can restore.
503+
*/
504+
clearComposerPromptAndImages: (threadRef: ComposerThreadTarget) => void;
498505
}
499506

500507
export interface EffectiveComposerModelState {
@@ -2091,7 +2098,7 @@ function hydratePersistedComposerImageAttachment(
20912098
}
20922099
}
20932100

2094-
function hydrateImagesFromPersisted(
2101+
export function hydrateImagesFromPersisted(
20952102
attachments: ReadonlyArray<PersistedComposerImageAttachment>,
20962103
): ComposerImageAttachment[] {
20972104
return attachments.flatMap((attachment) => {
@@ -3347,6 +3354,35 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
33473354
return { draftsByThreadKey: nextDraftsByThreadKey };
33483355
});
33493356
},
3357+
clearComposerPromptAndImages: (threadRef) => {
3358+
const threadKey = resolveComposerDraftKey(get(), threadRef) ?? "";
3359+
if (threadKey.length === 0) {
3360+
return;
3361+
}
3362+
set((state) => {
3363+
const current = state.draftsByThreadKey[threadKey];
3364+
if (!current) {
3365+
return state;
3366+
}
3367+
for (const image of current.images) {
3368+
revokeObjectPreviewUrl(image.previewUrl);
3369+
}
3370+
const nextDraft: ComposerThreadDraftState = {
3371+
...current,
3372+
prompt: ensureInlineTerminalContextPlaceholders("", current.terminalContexts.length),
3373+
images: [],
3374+
nonPersistedImageIds: [],
3375+
persistedAttachments: [],
3376+
};
3377+
const nextDraftsByThreadKey = { ...state.draftsByThreadKey };
3378+
if (shouldRemoveDraft(nextDraft)) {
3379+
delete nextDraftsByThreadKey[threadKey];
3380+
} else {
3381+
nextDraftsByThreadKey[threadKey] = nextDraft;
3382+
}
3383+
return { draftsByThreadKey: nextDraftsByThreadKey };
3384+
});
3385+
},
33503386
};
33513387
},
33523388
{

‎apps/web/src/index.css‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,30 @@ label:has(> select#reasoning-effort) select {
14321432
margin-top: 0.125rem;
14331433
}
14341434

1435+
/* Prompt-stash save acknowledgement: the new count fades up from just below
1436+
its resting position, once, then stops. One-shot and event-driven (React
1437+
remounts the element by key on each stash) — no continuous animation. */
1438+
@keyframes prompt-stash-count-enter {
1439+
from {
1440+
opacity: 0;
1441+
transform: translateY(2px);
1442+
}
1443+
to {
1444+
opacity: 1;
1445+
transform: translateY(0);
1446+
}
1447+
}
1448+
1449+
.prompt-stash-count-enter {
1450+
animation: prompt-stash-count-enter 180ms ease-out both;
1451+
}
1452+
1453+
@media (prefers-reduced-motion: reduce) {
1454+
.prompt-stash-count-enter {
1455+
animation: none;
1456+
}
1457+
}
1458+
14351459
@keyframes provider-update-pill-countdown {
14361460
from {
14371461
transform: scaleX(1);

0 commit comments

Comments
 (0)