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 =