"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ChevronLeft, ChevronRight, Download, Expand, FileText, ImageIcon, Loader2, Maximize, Minimize, Minus, Plus, RotateCcw, } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { useI18n } from "@/i18n/i18n-context"; import { base64ContentBlob } from "@/lib/binary-content"; import { sourcePreviewKind, fitSourceScale } from "@/lib/wiki-source-preview"; import type { WikiRawSource } from "@/types/wiki"; import { WikiSourcePreview } from "./source-preview-content"; import { SourcePdfPreview } from "./source-pdf-preview"; import { SourceDocxPreview } from "./source-docx-preview"; type PreviewSource = Pick< WikiRawSource, "path" | "content" | "contentBase64" | "mediaType" | "latestVersionId" >; export function WikiSourceViewer({ source }: { source: PreviewSource }) { return ( ); } function SourceViewer({ source }: { source: PreviewSource }) { const { t } = useI18n(); const root = useRef(null); const viewport = useRef(null); const drag = useRef< { id: number; x: number; y: number; left: number; top: number } | undefined >(undefined); const [bounds, setBounds] = useState({ width: 800, height: 550 }); const [natural, setNatural] = useState({ width: 794, height: 1123 }); const [manualScale, setManualScale] = useState(); const [page, setPage] = useState(1); const [pages, setPages] = useState(0); const [ready, setReady] = useState(false); const [failed, setFailed] = useState(false); const [fullscreen, setFullscreen] = useState(false); const [attempt, setAttempt] = useState(0); const kind = sourcePreviewKind(source.path, source.mediaType); const isText = source.content !== undefined; const blob = useMemo(() => { try { if (source.content !== undefined) return new Blob([source.content], { type: "text/plain;charset=utf-8" }); return source.contentBase64 ? base64ContentBlob(source.contentBase64, source.mediaType) : undefined; } catch { return undefined; } }, [source.content, source.contentBase64, source.mediaType]); const [objectUrl, setObjectUrl] = useState(); useEffect(() => { if (!blob) return; const url = URL.createObjectURL(blob); setObjectUrl(url); return () => URL.revokeObjectURL(url); }, [blob]); useEffect(() => { const el = viewport.current; if (!el) return; let timer: ReturnType | undefined; const update = () => { const next = { width: el.clientWidth, height: el.clientHeight }; setBounds((current) => current.width === next.width && current.height === next.height ? current : next, ); }; update(); const observer = new ResizeObserver(() => { if (timer !== undefined) clearTimeout(timer); timer = setTimeout(update, 120); }); observer.observe(el); return () => { observer.disconnect(); if (timer !== undefined) clearTimeout(timer); }; }, []); useEffect(() => { const update = () => setFullscreen(document.fullscreenElement === root.current); document.addEventListener("fullscreenchange", update); return () => document.removeEventListener("fullscreenchange", update); }, []); const onReady = useCallback(() => setReady(true), []); const onError = useCallback(() => { setFailed(true); setReady(false); }, []); const onSize = useCallback((size: { width: number; height: number }) => { setNatural((current) => current.width === size.width && current.height === size.height ? current : size, ); }, []); const scale = manualScale ?? fitSourceScale( natural.width, natural.height, bounds.width, bounds.height, kind === "image", ); const supported = isText || kind !== "unsupported"; const canRender = supported && !!blob && !failed; const filename = source.path.split("/").at(-1) ?? source.path; const Icon = kind === "image" ? ImageIcon : FileText; function zoom(factor: number) { setManualScale(Math.max(0.1, Math.min(4, scale * factor))); } async function toggleFullscreen() { try { if (document.fullscreenElement === root.current) await document.exitFullscreen(); else await root.current?.requestFullscreen(); } catch { toast.error(t("wiki.preview.fullscreenFailed")); } } function changePage(next: number) { setReady(false); setPage(next); viewport.current?.scrollTo({ top: 0, left: 0 }); } return (

{filename}

{t("wiki.preview.original")}

{kind === "pdf" && pages > 0 && !failed ? (
{page} / {pages}
) : null} {!isText && canRender ? ( <> ) : null}
{ if (kind !== "image" || event.button !== 0 || !ready) return; drag.current = { id: event.pointerId, x: event.clientX, y: event.clientY, left: event.currentTarget.scrollLeft, top: event.currentTarget.scrollTop, }; event.currentTarget.setPointerCapture(event.pointerId); }} onPointerMove={(event) => { const start = drag.current; if (!start || start.id !== event.pointerId) return; event.currentTarget.scrollLeft = start.left - event.clientX + start.x; event.currentTarget.scrollTop = start.top - event.clientY + start.y; }} onPointerUp={() => { drag.current = undefined; }} onPointerCancel={() => { drag.current = undefined; }} onLostPointerCapture={() => { drag.current = undefined; }} > {!canRender ? (

{t( !supported ? "wiki.preview.unsupported" : "wiki.preview.failed", )}

{t("wiki.preview.downloadHint")}

{failed ? ( ) : null}
) : ( <> {!isText && !ready ? (
{t("wiki.preview.loading")}
) : null}
{isText ? (
) : kind === "image" && objectUrl ? ( {filename} { onSize({ width: event.currentTarget.naturalWidth, height: event.currentTarget.naturalHeight, }); onReady(); }} onError={onError} /> ) : kind === "pdf" ? ( ) : kind === "docx" ? ( ) : null}
)}
{t( kind === "docx" ? "wiki.preview.docxHint" : kind === "image" ? "wiki.preview.imageHint" : "wiki.preview.readOnly", )} {blob ? ( {(blob.size / 1024).toFixed(1)} KB ) : null}
); }