import { useState } from "react"; import { useTranslation } from "react-i18next"; import { AlertCircle } from "lucide-react"; import { CodeBlock } from "@/components/CodeBlock"; import { MarkdownText } from "@/components/MarkdownText"; import { SegmentedControl } from "@/components/ui/segmented-control"; import { ImageLightbox } from "@/components/ImageLightbox"; import { fileKindForPath, splitFilePath } from "@/components/FileReferenceChip"; import type { FilePreviewPayload } from "@/lib/types"; export type FilePreviewPanelState = | { status: "loading" } | { status: "error"; message?: string; routeMissing?: boolean } | { status: "ready"; payload: FilePreviewPayload }; export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange, onImageError, onOpenFilePreview }: { path: string; state: FilePreviewPanelState; imageOpen: boolean; onImageOpenChange: (open: boolean) => void; onImageError: () => void; onOpenFilePreview?: (path: string) => void; }) { const { t } = useTranslation(); const [mode, setMode] = useState<"preview" | "source">("preview"); const previewKind = fileKindForPath(path); const markdown = state.status === "ready" && state.payload.kind !== "image" && state.payload.language === "markdown"; const displayPath = state.status === "ready" ? state.payload.display_path : path; const { name } = splitFilePath(displayPath); const fileName = name || displayPath; const errorMessage = state.status === "error" ? (state.routeMissing ? t("filePreview.routeMissing", { defaultValue: "File preview needs the latest gateway. Restart nanobot gateway and try again.", }) : state.message ?? t("filePreview.failed", { defaultValue: "Could not preview this file." })) : null; return (
{markdown ? (
) : null}
{state.status === "loading" ? (
{t("filePreview.loading", { defaultValue: "Loading preview..." })}
{previewKind === "image" ?
: <> {previewKind === "markdown" ?
{[t("filePreview.preview"), t("filePreview.source")].map((label) => {label})}
: null} {previewKind === "markdown" && mode === "preview" ?

{["w-full", "w-5/6", "w-3/4"].map((width) =>
)}
:
{["w-2/3", "w-full", "w-5/6", "w-3/4", "w-1/2", "w-3/5"].map((width) =>
)}
} }
) : state.status === "error" ? (

{errorMessage}

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