import { useEffect, useState } from 'react'; import { ExternalLink, LoaderCircle, RotateCw } from 'lucide-react'; import { api } from './api'; import { desktop } from './desktop'; import './LocalFiles.css'; type Preview = { id: string; url: string; projectId: string; path: string }; export function HtmlFilePreview({ projectId, path }: { projectId: string; path: string }) { const [preview, setPreview] = useState(null), [error, setError] = useState(''), [attempt, setAttempt] = useState(0), [loaded, setLoaded] = useState(false); useEffect(() => { let current = true, owned: Preview | null = null; const close = (value: Preview) => api('/projects/' + projectId + '/file-previews/' + value.id, {}, 'DELETE').catch(() => {}); setPreview(null); setError(''); setLoaded(false); void api('/projects/' + projectId + '/file-previews', { path }).then(value => { if (current) { owned = value; setPreview(value); } else void close(value); }).catch(cause => { if (current) setError(cause instanceof Error ? cause.message : '无法打开 HTML 预览。'); }); return () => { current = false; if (owned) void close(owned); }; }, [projectId, path, attempt]); return
HTML 文件预览{preview && (desktop?.openFilePreview ? : 独立页面)}
{error ?

{error}

: <>{!loaded &&
正在打开 HTML 页面…
}{preview &&