From 68bead1082183e73035937d3bb7070db48ace69b Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 4 Nov 2026 18:45:06 +0100 Subject: [PATCH] preview: the sidebar shows the file's current content after it changed on disk (follow-up to #5180) The preview kept a file's first content: openPreview dropped the freshly read item when its id was already a tab, the panel's loading effect had no content version, and the effect that re-adds a message's artifacts put the stored first copy back. All three change together, through pure rules in lib/preview-merge.ts: opening a changed file replaces its tab in place; a message's artifact only adds a missing tab or replaces an older, different copy (each copy carries the time it was read or made) and never overwrites a newer one; both return the same array when nothing changed, so no update and no loop follows. The panel reloads when (id, content version) changes: a hash of the text, or the host's mtime and size for pictures and PDFs (/api/files/preview now sends mtime). The reading view is reset by the file, not by its content. Tests: stale case, 2000 alternations without a change, one load per real change. Real Edge 156 with the real panel: the old rules keep Version One, the new ones show Version Two through 300 sync/render alternations with 0 Markdown renders. No visible text. --- apps/host/server/chat-files.mjs | 2 +- apps/host/src/components/ArtifactPanel.tsx | 16 ++- apps/host/src/lib/artifacts.ts | 6 +- apps/host/src/lib/deliverables.ts | 16 +-- apps/host/src/lib/preview-merge.ts | 74 ++++++++++ apps/host/src/screens/Chat.tsx | 9 +- apps/host/tests/preview-stale.test.mjs | 152 +++++++++++++++++++++ 7 files changed, 257 insertions(+), 18 deletions(-) create mode 100644 apps/host/src/lib/preview-merge.ts create mode 100644 apps/host/tests/preview-stale.test.mjs diff --git a/apps/host/server/chat-files.mjs b/apps/host/server/chat-files.mjs index a674546e60..6e9e116e19 100644 --- a/apps/host/server/chat-files.mjs +++ b/apps/host/server/chat-files.mjs @@ -153,7 +153,7 @@ export function parseCsv(text, delimiter = ',', maxRows = 500) { export function previewFile(root, rawPath, sessionId) { const file = resolveChatFile(root, rawPath); const st = statSync(file); const kind = fileType(file); - const base = { kind, name: basename(file), path: file, size: st.size, url: pathToFileURL(file).href }; + const base = { kind, name: basename(file), path: file, size: st.size, mtime: Math.round(st.mtimeMs), url: pathToFileURL(file).href }; const raw = `/api/files/raw?sessionId=${encodeURIComponent(sessionId)}&path=${encodeURIComponent(file)}`; if (kind === 'image' || kind === 'pdf') return { ...base, rawUrl: raw, mime: mimeOf(file) }; if (kind === 'other') return { ...base, note: 'This kind of file has no preview. Open it in its own app.' }; diff --git a/apps/host/src/components/ArtifactPanel.tsx b/apps/host/src/components/ArtifactPanel.tsx index a9d7071c66..d1d9eb3df0 100644 --- a/apps/host/src/components/ArtifactPanel.tsx +++ b/apps/host/src/components/ArtifactPanel.tsx @@ -1,6 +1,6 @@ import { tr } from '../lib/i18n'; import { PreviewControls } from './PreviewControls'; -import { lazy, Suspense, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; +import { lazy, Suspense, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { Dialog, Icon, IconBtn, Seg, type IconName } from './ui'; import { CopyBtn, CopyMenuItem } from './CopyBtn'; @@ -10,6 +10,7 @@ import { FloatingMenu } from './FloatingMenu'; import { TablePreview } from './TablePreview'; import { artifactFilename, artifactIcon, safePreviewUrl, type PreviewArtifact } from '../lib/artifacts'; import { scrollBehavior } from '../lib/responsive'; +import { previewVersion } from '../lib/preview-merge'; const PdfPreview = lazy(() => import('./PdfPreview')); export function ArtifactPanel({ items, selected, onSelect, onClose, onCloseTab, inline }: { items: PreviewArtifact[]; @@ -20,17 +21,22 @@ export function ArtifactPanel({ items, selected, onSelect, onClose, onCloseTab, inline: boolean; }) { const current = items.find(a => a.id === selected); + // What the text loading depends on: the file's identity and its content version, not the object (the chat hands over new objects often). + const version = useMemo(() => current ? previewVersion(current) : '', [current]); const [text, setText] = useState(''), [error, setError] = useState(''), [mode, setMode] = useState<'preview' | 'source'>('preview'), [revision, setRevision] = useState(0), [loading, setLoading] = useState(false), [imageSize, setImageSize] = useState('fit'); const [toolbarHost, setToolbarHost] = useState(null); const [fileMenu, setFileMenu] = useState(false); const fileMenuRef = useRef(null); useLayoutEffect(() => { setToolbarHost(inline && current ? document.getElementById('preview-file-actions') : null); }, [inline, current?.id]); + // A different file starts in the reading view; the same file with new content keeps the person's view. useEffect(() => { - setError(''); setMode('preview'); - setLoading(false); setImageSize('fit'); setFileMenu(false); + }, [current?.id]); + useEffect(() => { + setError(''); + setLoading(false); if (!current || !['markdown', 'code', 'table'].includes(current.kind)) return; if (current.content !== undefined) { @@ -61,7 +67,7 @@ export function ArtifactPanel({ items, selected, onSelect, onClose, onCloseTab, setLoading(false); }); return () => abort.abort(); - }, [current?.id, revision]); + }, [current?.id, version, revision]); // A closed panel leaves with a short slide instead of vanishing (a bare shell; the page itself is already gone). const hadCurrent = useRef(false); const [leaving, setLeaving] = useState(false); @@ -84,7 +90,7 @@ export function ArtifactPanel({ items, selected, onSelect, onClose, onCloseTab, const content =