Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Add composer image previews with draft and focus preservation

justrach6a54e394f497
diff --git a/apps/native/components/primitives/ComposerAttachments.tsx b/apps/native/components/primitives/ComposerAttachments.tsx
new file mode 100644
index 00000000..bba23a35
--- /dev/null
+++ b/apps/native/components/primitives/ComposerAttachments.tsx
@@ -0,0 +1,60 @@
+"use client";
+
+import { useLayoutEffect, useId, useRef, useState } from "react";
+import { createPortal } from "react-dom";
+import type { Attachment } from "@/lib/attachments";
+import { Icon, GLYPHS } from "./prompt-demo";
+
+export default function ComposerAttachments({ attachments, pill, onRemove }: {
+ attachments: readonly Attachment[]; pill: boolean; onRemove(id: string): void;
+}) {
+ const [previewId, setPreviewId] = useState<string | null>(null);
+ const preview = attachments.find(file => file.id === previewId && file.preview);
+ return <>
+ {attachments.length > 0 && <div className={`flex flex-wrap gap-1.5 pt-0.5 ${pill ? "px-1" : "px-0.5"}`}>
+ {attachments.map(file => <span key={file.id}
+ className={`flex h-6.5 items-center gap-1.5 bg-field py-1 pr-1 pl-1.5 text-[11.5px] text-ink-2 shadow-hairline ${pill ? "rounded-full" : "rounded-chip"}`}
+ style={{ animation: "pop-in 200ms cubic-bezier(0.23,1,0.32,1) both" }}>
+ {file.preview ? <button type="button" aria-label={`Preview ${file.name}`} aria-haspopup="dialog"
+ className="flex min-w-0 items-center gap-1.5 rounded focus-visible:outline-2 focus-visible:outline-accent"
+ onClick={() => setPreviewId(file.id)}>
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img src={file.preview} alt="" className="-my-1 size-6 rounded-[4px] object-cover" />
+ <span className="max-w-36 truncate">{file.name}</span>
+ </button> : <><Icon size={12}>{GLYPHS.file}</Icon><span className="max-w-36 truncate">{file.name}</span></>}
+ <button type="button" aria-label={`Remove ${file.name}`} onClick={() => onRemove(file.id)}
+ className={`-my-1 flex size-6 items-center justify-center text-ink-3 transition-colors duration-100 hover:bg-line/70 hover:text-ink ${pill ? "rounded-full" : "rounded-[5px]"}`}>
+ <Icon size={10} strokeWidth={2.5}><path d="M18 6L6 18M6 6l12 12" /></Icon>
+ </button>
+ </span>)}
+ </div>}
+ {preview && <AttachmentPreview key={preview.id} file={preview} onClose={() => setPreviewId(null)} />}
+ </>;
+}
+
+function AttachmentPreview({ file, onClose }: { file: Attachment; onClose(): void }) {
+ const dialog = useRef<HTMLDialogElement>(null);
+ const title = useId();
+ const [failed, setFailed] = useState(false);
+ useLayoutEffect(() => {
+ const element = dialog.current;
+ element?.showModal();
+ return () => element?.close();
+ }, []);
+ return createPortal(<dialog ref={dialog} role="dialog" aria-modal="true" aria-labelledby={title}
+ onCancel={event => { event.preventDefault(); event.stopPropagation(); onClose(); }}
+ onKeyDown={event => event.stopPropagation()}
+ onClick={event => { if (event.target === event.currentTarget) onClose(); }}
+ className="m-auto max-h-[90dvh] w-fit max-w-[calc(100vw-32px)] overflow-auto rounded-xl border border-line bg-surface p-0 text-ink shadow-overlay backdrop:bg-black/50">
+ <div className="p-3">
+ <header className="mb-3 flex items-center gap-6">
+ <h2 id={title} className="min-w-0 flex-1 truncate text-sm font-medium">{file.name}</h2>
+ <button type="button" aria-label="Close image preview" onClick={onClose}
+ className="size-7 shrink-0 rounded text-ink-3 hover:bg-hover focus-visible:outline-auto">×</button>
+ </header>
+ {failed ? <p role="alert" className="p-4 text-sm">Could not display this image. Your attachment is still in the draft.</p> :
+ /* eslint-disable-next-line @next/next/no-img-element */
+ <img src={file.preview} alt={file.name} onError={() => setFailed(true)} className="max-h-[70dvh] max-w-full rounded object-contain" />}
+ </div>
+ </dialog>, document.body);
+}
diff --git a/apps/native/components/primitives/PromptBar.tsx b/apps/native/components/primitives/PromptBar.tsx
index 7fbe1c25..80f8fbd9 100644
--- a/apps/native/components/primitives/PromptBar.tsx
+++ b/apps/native/components/primitives/PromptBar.tsx
@@ -8,6 +8,7 @@ import ModelEffortButtons from "./ModelEffortButtons";
import type { ModelChoice } from "@/lib/acp-client";
import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
import ComposerMenu from "./ComposerMenu";
+import ComposerAttachments from "./ComposerAttachments";
import { useComposerSweep } from "./useComposerSweep";
import { useComposerSize } from "./useComposerSize";
import { useComposerDraft } from "./useComposerDraft";
@@ -403,37 +404,7 @@ export default function PromptBar({
{draft}
</span>
- {attachments.length > 0 && (
- <div className={`flex flex-wrap gap-1.5 pt-0.5 ${pill ? "px-1" : "px-0.5"}`}>
- {attachments.map((file) => (
- <span
- key={file.id}
- className={`flex h-6.5 items-center gap-1.5 bg-field py-1 pr-1 pl-1.5 text-[11.5px] text-ink-2 shadow-hairline ${
- pill ? "rounded-full" : "rounded-chip"
- }`}
- style={{ animation: "pop-in 200ms cubic-bezier(0.23,1,0.32,1) both" }}
- >
- {file.preview ? (
- /* eslint-disable-next-line @next/next/no-img-element */
- <img src={file.preview} alt="" className="-my-1 size-6 rounded-[4px] object-cover" />
- ) : (
- <Icon size={12}>{GLYPHS.file}</Icon>
- )}
- <span className="max-w-36 truncate">{file.name}</span>
- <button
- type="button"
- aria-label={`Remove ${file.name}`}
- onClick={() => removeAttachment(file.id)}
- className={`-my-1 flex size-6 items-center justify-center text-ink-3 transition-colors duration-100 hover:bg-line/70 hover:text-ink ${
- pill ? "rounded-full" : "rounded-[5px]"
- }`}
- >
- <Icon size={10} strokeWidth={2.5}><path d="M18 6L6 18M6 6l12 12" /></Icon>
- </button>
- </span>
- ))}
- </div>
- )}
+ <ComposerAttachments attachments={attachments} pill={pill} onRemove={removeAttachment} />
{uploads > 0 && <p role="status" className="px-2 text-xs text-ink-3">Adding {uploads} {uploads === 1 ? "file" : "files"}…</p>}
{attachError && (
diff --git a/apps/native/electron/composer-interaction-visual.cjs b/apps/native/electron/composer-interaction-visual.cjs
index 1213362c..eb0cabd0 100644
--- a/apps/native/electron/composer-interaction-visual.cjs
+++ b/apps/native/electron/composer-interaction-visual.cjs
@@ -100,6 +100,22 @@ async function runComposerInteractions({ win, origin, output }) {
await wait(`!!document.querySelector('[aria-label="Remove composer-image.png"]')`);
await wait(`document.querySelector('[data-promptbar] img')?.naturalWidth===1`);
assert.equal(await js(`!!document.querySelector('[data-promptbar] [role="status"]')`), false, 'A hidden upload completes in its own chat');
+ const imageSource = await js(`document.querySelector('[aria-label="Preview composer-image.png"] img').src`);
+ await pointer('[aria-label="Preview composer-image.png"]');
+ await wait(`!!document.querySelector('dialog[open] [aria-label="Close image preview"]')`);
+ assert.equal(await js(`document.querySelector('dialog[open] img').src`), imageSource, 'Preview shows the selected attachment');
+ await testDesktop.testInput(wc, { type: 'keyDown', keyCode: 'Escape' });
+ await testDesktop.testInput(wc, { type: 'keyUp', keyCode: 'Escape' });
+ await wait(`!document.querySelector('dialog[open]')`);
+ assert.equal(await draft(first), 'First unsent draft', 'Closing preview preserves text');
+ assert.ok(await js(`!!document.querySelector('[aria-label="Remove composer-image.png"]')`), 'Closing preview preserves the attachment');
+ assert.ok(await js(`document.activeElement.matches('[aria-label="Preview composer-image.png"]')`), 'Closing preview restores the trigger focus');
+ await pointer('[aria-label="Preview composer-image.png"]');
+ await wait(`!!document.querySelector('dialog[open]')`);
+ await pointer('[aria-label="Close image preview"]');
+ await wait(`!document.querySelector('dialog[open]')`);
+ assert.equal(await draft(first), 'First unsent draft', 'Close button also preserves the draft');
+
await key('2', { metaKey: true });
assert.equal(await draft(second), 'Second unsent draft', 'An older upload must not reset another chat');
assert.equal(await js(`!!document.querySelector('[aria-label="Remove composer-image.png"]')`), false, 'Attachments never move between chats');