import { tr } from '../lib/i18n'; import { PreviewControls } from './PreviewControls'; 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'; import { PreviewMarkdown } from './PreviewMarkdown'; import { SyntaxCode } from './SyntaxCode'; 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[]; selected: string | null; onSelect: (id: string) => void; onClose: () => void; onCloseTab: (id: string) => void; 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(() => { setMode('preview'); setImageSize('fit'); setFileMenu(false); }, [current?.id]); useEffect(() => { setError(''); setLoading(false); if (!current || !['markdown', 'code', 'table'].includes(current.kind)) return; if (current.content !== undefined) { setText(current.content); return; } const url = current.url && safePreviewUrl(current.url); if (!url) { setError('This file cannot be opened.'); return; } const abort = new AbortController(); setLoading(true); fetch(url, { signal: abort.signal, credentials: 'omit' }).then(r => { if (!r.ok) throw new Error(`File returned ${r.status}`); return r.text(); }).then(content => { if (content.length > 500000) throw new Error('This text file is too large to preview.'); if (!abort.signal.aborted) setText(content); }).catch(e => { if (!abort.signal.aborted) setError(e.message); }).finally(() => { if (!abort.signal.aborted) setLoading(false); }); return () => abort.abort(); }, [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); useEffect(() => { if (current) { hadCurrent.current = true; setLeaving(false); return; } if (!hadCurrent.current || !inline) return; hadCurrent.current = false; if (matchMedia('(prefers-reduced-motion: reduce)').matches || document.documentElement.dataset.motion === 'reduced') return; setLeaving(true); // The leaving shell takes the 460 ms step of the motion scale (liquid.css); it is removed just after. const timer = setTimeout(() => setLeaving(false), 520); return () => clearTimeout(timer); }, [!!current, inline]); if (!current) return leaving ?