import { useEffect, useRef, useState } from "react"; import { AlertCircle } from "lucide-react"; import { useTranslation } from "react-i18next"; import { CodeBlock } from "@/components/CodeBlock"; import { ImageLightbox } from "@/components/ImageLightbox"; import { fileKindForPath, splitFilePath } from "@/components/FileReferenceChip"; import { ApiError, fetchFilePreview } from "@/lib/api"; import type { FilePreviewPayload } from "@/lib/types"; interface FilePreviewPanelProps { sessionKey: string; path: string; token: string; loadPreview?: (path: string) => Promise; initialPreview?: FilePreviewPayload; } type PreviewState = | { status: "loading" } | { status: "error"; error: unknown } | { status: "ready"; payload: FilePreviewPayload }; export function FilePreviewPanel({ sessionKey, path, token, loadPreview, initialPreview, }: FilePreviewPanelProps) { const { t } = useTranslation(); const [state, setState] = useState(() => initialPreview ? { status: "ready", payload: initialPreview } : { status: "loading" }); const [imageOpen, setImageOpen] = useState(false); // Cache aging must not restart an already open preview on unrelated rerenders. const initialPreviewRef = useRef(initialPreview); initialPreviewRef.current = initialPreview; // Credential renewal is not a new file selection. Read the current token only // when a session/path/loader change actually starts another request. const tokenRef = useRef(token); tokenRef.current = token; useEffect(() => { let cancelled = false; setImageOpen(false); const cached = initialPreviewRef.current; setState(cached ? { status: "ready", payload: cached } : { status: "loading" }); (loadPreview?.(path) ?? fetchFilePreview(tokenRef.current, sessionKey, path)) .then((payload) => { if (!cancelled) setState({ status: "ready", payload }); }) .catch((error: unknown) => { if (!cancelled) setState({ status: "error", error }); }); return () => { cancelled = true; }; }, [path, sessionKey, loadPreview]); const displayPath = state.status === "ready" ? state.payload.display_path : path; const { name } = splitFilePath(displayPath); const fileName = name || displayPath; const errorMessage = state.status === "error" ? (state.error instanceof ApiError ? (state.error.status === 404 && /API route not found/i.test(state.error.message) ? t("filePreview.routeMissing", { defaultValue: "File preview needs the latest gateway. Restart nanobot gateway and try again.", }) : state.error.message) : t("filePreview.failed", { defaultValue: "Could not preview this file." })) : null; return (
{state.status === "loading" ? (
{t("filePreview.loading", { defaultValue: "Loading preview..." })}
{fileKindForPath(path) === "image" ?
: <>{["w-2/3", "w-full", "w-5/6", "w-3/4"].map((width) =>
)}}
) : state.status === "error" ? (

{errorMessage}

) : state.payload.kind === "image" ? (
{}} onOpenChange={setImageOpen} />
) : (
{state.payload.truncated ? (
{t("filePreview.truncated", { defaultValue: "Preview is truncated because this file is large.", })}
) : null}
)}
); }