import { useEffect, useState } from 'react'; import { fileHwpPreviewUrl } from '../../lib/apiClient'; import { FilePreviewFallback } from './FilePreviewFallback'; interface HwpPreviewProps { fileId: string; fileName: string; } export function HwpPreview({ fileId, fileName }: HwpPreviewProps) { const [html, setHtml] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { let cancelled = false; setLoading(true); setError(null); setHtml(null); fetch(fileHwpPreviewUrl(fileId)) .then(async (res) => { if (!res.ok) { const body = (await res.json().catch(() => null)) as { message?: string } | null; throw new Error(body?.message ?? '한글 미리보기를 불러올 수 없습니다.'); } return res.json() as Promise<{ html: string }>; }) .then((data) => { if (!cancelled) { setHtml(data.html); setLoading(false); } }) .catch((e) => { if (!cancelled) { setError(e instanceof Error ? e.message : '한글 미리보기 실패'); setLoading(false); } }); return () => { cancelled = true; }; }, [fileId]); if (error) { return ( ); } if (loading || !html) { return ; } return (
); }