2026-10-14 00:11:00 +08:00
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
|
|
import { FilePreviewPanelView, type FilePreviewPanelState } from "@/components/FilePreviewPanelView";
|
2026-06-06 19:49:33 +08:00
|
|
|
import { ApiError, fetchFilePreview } from "@/lib/api";
|
2026-09-24 01:44:23 +08:00
|
|
|
import type { FilePreviewPayload } from "@/lib/types";
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
interface FilePreviewPanelProps {
|
|
|
|
|
sessionKey: string;
|
|
|
|
|
path: string;
|
|
|
|
|
token: string;
|
2026-09-21 17:48:49 +08:00
|
|
|
loadPreview?: (path: string) => Promise<FilePreviewPayload>;
|
2026-09-24 01:44:23 +08:00
|
|
|
initialPreview?: FilePreviewPayload;
|
2026-10-17 20:30:27 +08:00
|
|
|
onOpenFilePreview?: (path: string) => void;
|
2026-10-17 23:00:29 +08:00
|
|
|
onResolvePath?: (path: string, canonicalPath: string) => void;
|
2026-06-06 19:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function FilePreviewPanel({
|
|
|
|
|
sessionKey,
|
|
|
|
|
path,
|
|
|
|
|
token,
|
2026-09-21 17:48:49 +08:00
|
|
|
loadPreview,
|
2026-09-24 01:44:23 +08:00
|
|
|
initialPreview,
|
2026-10-17 20:30:27 +08:00
|
|
|
onOpenFilePreview,
|
2026-10-17 23:00:29 +08:00
|
|
|
onResolvePath,
|
2026-06-06 19:49:33 +08:00
|
|
|
}: FilePreviewPanelProps) {
|
2026-10-14 00:11:00 +08:00
|
|
|
const [state, setState] = useState<FilePreviewPanelState>(() => initialPreview
|
2026-09-24 01:44:23 +08:00
|
|
|
? { 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;
|
2026-09-24 01:57:03 +08:00
|
|
|
// 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;
|
2026-10-17 23:00:29 +08:00
|
|
|
const onResolvePathRef = useRef(onResolvePath);
|
|
|
|
|
onResolvePathRef.current = onResolvePath;
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
2026-09-24 01:44:23 +08:00
|
|
|
setImageOpen(false);
|
|
|
|
|
const cached = initialPreviewRef.current;
|
2026-09-24 12:02:33 +08:00
|
|
|
setState(previous => cached
|
|
|
|
|
? (previous.status === "ready" && previous.payload === cached ? previous : { status: "ready", payload: cached })
|
|
|
|
|
: (previous.status === "loading" ? previous : { status: "loading" }));
|
2026-09-24 01:57:03 +08:00
|
|
|
(loadPreview?.(path) ?? fetchFilePreview(tokenRef.current, sessionKey, path))
|
2026-06-06 19:49:33 +08:00
|
|
|
.then((payload) => {
|
2026-10-17 23:00:29 +08:00
|
|
|
if (cancelled) return;
|
|
|
|
|
setState(previous => previous.status === "ready" && previous.payload === payload
|
2026-09-24 12:02:33 +08:00
|
|
|
? previous : { status: "ready", payload });
|
2026-10-17 23:00:29 +08:00
|
|
|
onResolvePathRef.current?.(path, payload.path);
|
2026-06-06 19:49:33 +08:00
|
|
|
})
|
|
|
|
|
.catch((error: unknown) => {
|
2026-10-14 00:11:00 +08:00
|
|
|
if (!cancelled) setState({ status: "error",
|
|
|
|
|
...(error instanceof ApiError ? {
|
|
|
|
|
message: error.message,
|
|
|
|
|
routeMissing: error.status === 404 && /API route not found/i.test(error.message),
|
|
|
|
|
} : {}),
|
|
|
|
|
});
|
2026-06-06 19:49:33 +08:00
|
|
|
});
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-09-24 01:57:03 +08:00
|
|
|
}, [path, sessionKey, loadPreview]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
2026-10-14 00:11:00 +08:00
|
|
|
const reportImageError = useCallback(() => setState({ status: "error" }), []);
|
2026-10-17 20:30:27 +08:00
|
|
|
return <FilePreviewPanelView key={`${sessionKey}:${path}`} path={path} state={state} imageOpen={imageOpen}
|
|
|
|
|
onOpenFilePreview={onOpenFilePreview}
|
2026-10-14 00:11:00 +08:00
|
|
|
onImageOpenChange={setImageOpen} onImageError={reportImageError} />;
|
2026-06-06 19:49:33 +08:00
|
|
|
}
|