The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep the image lightbox open across transcript remounts
Brooklyn Nicholson6013f62b6d33
diff --git a/apps/desktop/src/components/chat/zoomable-image.test.tsx b/apps/desktop/src/components/chat/zoomable-image.test.tsxindex 6cf1e834845..852b2285a1b 100644--- a/apps/desktop/src/components/chat/zoomable-image.test.tsx+++ b/apps/desktop/src/components/chat/zoomable-image.test.tsx@@ -2,6 +2,7 @@ import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-libraimport { afterEach, describe, expect, it } from 'vitest'import { I18nProvider } from '@/i18n/context'+import { resetTranscriptLightbox } from '@/store/transcript-lightbox'import { ZoomableImage } from './zoomable-image'@@ -28,6 +29,7 @@ async function renderWithI18n(ui: React.ReactNode) {describe('ZoomableImage zoomSrc', () => {afterEach(() => {cleanup()+ resetTranscriptLightbox()})it('paints the bounded src inline but enlarges the full-resolution zoomSrc (#93204)', async () => {@@ -59,4 +61,50 @@ describe('ZoomableImage zoomSrc', () => {expect(screen.getAllByAltText('shot').every(img => img.getAttribute('src') === THUMB)).toBe(true)})})++ // #123018: transcript rows remount routinely while a turn streams (render-+ // budget slice recycling, markdown AST re-parse). The open flag lives in a+ // store keyed by source identity, so a remounted row re-presents its own+ // open lightbox instead of dropping it.+ it('re-presents an open lightbox after the row unmounts and remounts (#123018)', async () => {+ const { unmount } = await renderWithI18n(<ZoomableImage alt="shot" src={THUMB} zoomSrc={FULL} />)++ fireEvent.click(screen.getByAltText('shot'))++ await waitFor(() => {+ expect(screen.getAllByAltText('shot').some(img => img.getAttribute('src') === FULL)).toBe(true)+ })++ // The row recycles — component-local state dies with it.+ unmount()+ await renderWithI18n(<ZoomableImage alt="shot" src={THUMB} zoomSrc={FULL} />)++ await waitFor(() => {+ expect(screen.getAllByAltText('shot').some(img => img.getAttribute('src') === FULL)).toBe(true)+ })+ })++ it('keeps only one preview open at a time and only the owning row can close it', async () => {+ await renderWithI18n(+ <>+ <ZoomableImage alt="one" src={THUMB} zoomSrc={FULL} />+ <ZoomableImage alt="two" src={THUMB} zoomSrc={THUMB} />+ </>+ )++ fireEvent.click(screen.getAllByAltText('one')[0]!)++ await waitFor(() => {+ expect(screen.getAllByAltText('one').some(img => img.getAttribute('src') === FULL)).toBe(true)+ })++ // Opening the second preview replaces the first — same one-at-a-time UX+ // as the component-local flag.+ fireEvent.click(screen.getAllByAltText('two')[0]!)++ await waitFor(() => {+ expect(screen.getAllByAltText('two').some(img => img.getAttribute('src') === THUMB)).toBe(true)+ expect(screen.queryAllByAltText('one').every(img => img.getAttribute('src') !== FULL)).toBe(true)+ })+ })})diff --git a/apps/desktop/src/components/chat/zoomable-image.tsx b/apps/desktop/src/components/chat/zoomable-image.tsxindex d837ccb7008..ca260298f69 100644--- a/apps/desktop/src/components/chat/zoomable-image.tsx+++ b/apps/desktop/src/components/chat/zoomable-image.tsx@@ -1,12 +1,14 @@'use client'-import { type ComponentProps, useState } from 'react'+import { useStore } from '@nanostores/react'+import { type ComponentProps } from 'react'import { Dialog, DialogContent } from '@/components/ui/dialog'import { useImageDownload } from '@/hooks/use-image-download'import { useI18n } from '@/i18n'import { Download } from '@/lib/icons'import { cn } from '@/lib/utils'+import { $transcriptLightbox, closeTranscriptLightbox, openTranscriptLightbox } from '@/store/transcript-lightbox'export interface ZoomableImageProps extends ComponentProps<'img'> {containerClassName?: string@@ -38,7 +40,14 @@ export function ZoomableImage({// thumbnail (`src`) stays the cheap paint.const fullSrc = zoomSrc || src || ''const { download, saving } = useImageDownload(fullSrc)- const [lightboxOpen, setLightboxOpen] = useState(false)+ // The open flag lives in a store keyed by source identity, not local state:+ // transcript rows (and the markdown leaves inside them) remount routinely+ // while a turn streams — the render-budget slice can drop the row, and the+ // streaming re-parse re-mounts AST leaves — which used to close a+ // user-opened lightbox with no gesture (#123018). A remounted row reads the+ // same store and re-presents its own open dialog.+ const openSrc = useStore($transcriptLightbox)+ const lightboxOpen = openSrc !== null && openSrc === fullSrcconst canOpen = Boolean(src)return (@@ -51,7 +60,7 @@ export function ZoomableImage({aria-label={canOpen ? copy.openImage : undefined}className="contents"disabled={!canOpen}- onClick={() => canOpen && setLightboxOpen(true)}+ onClick={() => canOpen && openTranscriptLightbox(fullSrc)}type="button"><img alt={alt ?? ''} className={className} src={src} {...props} />@@ -65,7 +74,7 @@ export function ZoomableImage({alt={alt}copy={copy}onClick={download}- onOpenChange={setLightboxOpen}+ onOpenChange={open => (open ? openTranscriptLightbox(fullSrc) : closeTranscriptLightbox(fullSrc))}open={lightboxOpen}saving={saving}src={fullSrc}diff --git a/apps/desktop/src/store/transcript-lightbox.ts b/apps/desktop/src/store/transcript-lightbox.tsnew file mode 100644index 00000000000..b93ac1b41ed--- /dev/null+++ b/apps/desktop/src/store/transcript-lightbox.ts@@ -0,0 +1,43 @@+import { atom } from 'nanostores'++import { $activeSessionId } from '@/store/session'++/**+ * The transcript lightbox's open source, hoisted out of `ZoomableImage`.+ *+ * Transcript rows — and the markdown leaves inside them — legitimately remount+ * while a turn streams (the render-budget slice drops older rows; the markdown+ * AST re-parses as text grows), and the dialog's open flag used to be+ * component-local `useState`. A user-opened preview therefore closed itself+ * with no gesture the moment its row recycled (#123018). Keying the flag by+ * source identity here lets a remounted row re-present its own open dialog.+ *+ * One preview at a time (same as the component-local flag ever allowed),+ * process-memory only, cleared by an explicit close (Escape, backdrop, or+ * click) — never by a remount — and on a session switch, so a new transcript+ * never inherits the previous chat's open preview.+ */+export const $transcriptLightbox = atom<string | null>(null)++// A stale open entry must not follow the user into another chat: the same+// media path can legitimately appear in two sessions, and a lightbox opening+// with no gesture there would be the bug again, inverted.+$activeSessionId.subscribe(() => resetTranscriptLightbox())++export function openTranscriptLightbox(src: string): void {+ if (src) {+ $transcriptLightbox.set(src)+ }+}++/** Identity-guarded: only the row owning the currently-open source clears it. */+export function closeTranscriptLightbox(src: string): void {+ if ($transcriptLightbox.get() === src) {+ $transcriptLightbox.set(null)+ }+}++/** Mount/test isolation: drop any open entry without a gesture. */+export function resetTranscriptLightbox(): void {+ $transcriptLightbox.set(null)+}