2026-10-17 20:30:27 +08:00
|
|
|
import { useState } from "react";
|
2026-10-14 00:11:00 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { AlertCircle } from "lucide-react";
|
|
|
|
|
|
|
|
|
|
import { CodeBlock } from "@/components/CodeBlock";
|
2026-10-17 20:30:27 +08:00
|
|
|
import { MarkdownText } from "@/components/MarkdownText";
|
|
|
|
|
import { SegmentedControl } from "@/components/ui/segmented-control";
|
2026-10-14 00:11:00 +08:00
|
|
|
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 };
|
|
|
|
|
|
2026-10-17 20:30:27 +08:00
|
|
|
export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange, onImageError, onOpenFilePreview }: {
|
2026-10-14 00:11:00 +08:00
|
|
|
path: string;
|
|
|
|
|
state: FilePreviewPanelState;
|
|
|
|
|
imageOpen: boolean;
|
|
|
|
|
onImageOpenChange: (open: boolean) => void;
|
|
|
|
|
onImageError: () => void;
|
2026-10-17 20:30:27 +08:00
|
|
|
onOpenFilePreview?: (path: string) => void;
|
2026-10-14 00:11:00 +08:00
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
2026-10-17 20:30:27 +08:00
|
|
|
const [mode, setMode] = useState<"preview" | "source">("preview");
|
2026-10-19 15:57:32 +08:00
|
|
|
const previewKind = fileKindForPath(path);
|
2026-10-17 20:30:27 +08:00
|
|
|
const markdown = state.status === "ready" && state.payload.kind !== "image"
|
|
|
|
|
&& state.payload.language === "markdown";
|
2026-10-14 00:11:00 +08:00
|
|
|
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 (
|
|
|
|
|
<section aria-label={t("filePreview.aria")} data-testid="file-preview-panel" className="flex min-h-0 flex-1 flex-col">
|
2026-10-17 20:30:27 +08:00
|
|
|
{markdown ? (
|
|
|
|
|
<div className="flex shrink-0 items-center justify-end px-4 py-2">
|
|
|
|
|
<SegmentedControl value={mode} onChange={setMode} ariaLabel={t("filePreview.displayMode")}
|
|
|
|
|
options={[{ value: "preview", label: t("filePreview.preview") }, { value: "source", label: t("filePreview.source") }]}
|
|
|
|
|
itemClassName="px-3 [@media(pointer:coarse)]:min-h-9" />
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
<div key={mode} data-file-preview-scroll tabIndex={0}
|
2026-10-14 00:11:00 +08:00
|
|
|
className="min-h-0 flex-1 overflow-auto focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/60">
|
|
|
|
|
{state.status === "loading" ? (
|
|
|
|
|
<div role="status" aria-label={t("filePreview.loading", { defaultValue: "Loading preview..." })}
|
2026-10-19 15:57:32 +08:00
|
|
|
className="h-full" aria-busy="true">
|
2026-10-14 00:11:00 +08:00
|
|
|
<span className="sr-only">{t("filePreview.loading", { defaultValue: "Loading preview..." })}</span>
|
2026-10-19 15:57:32 +08:00
|
|
|
<div aria-hidden className="h-full animate-pulse [animation-duration:900ms] motion-reduce:animate-none">
|
|
|
|
|
{previewKind === "image" ? <div className="flex h-full items-center justify-center p-4">
|
|
|
|
|
<div className="aspect-[8/5] w-full max-w-sm rounded-compact bg-muted/40" />
|
|
|
|
|
</div> : <>
|
|
|
|
|
{previewKind === "markdown" ? <div className="flex justify-end px-4 py-2">
|
2026-10-20 16:33:05 +08:00
|
|
|
<div className="inline-flex min-h-8 items-center gap-1 rounded-full bg-muted/40 p-1 text-ui-label">
|
2026-10-19 15:57:32 +08:00
|
|
|
{[t("filePreview.preview"), t("filePreview.source")].map((label) => <span key={label} className="invisible px-3 py-1 [@media(pointer:coarse)]:min-h-9">{label}</span>)}
|
|
|
|
|
</div>
|
|
|
|
|
</div> : null}
|
2026-10-20 16:33:05 +08:00
|
|
|
{previewKind === "markdown" && mode === "preview" ? <div className="markdown-content px-5 pb-6 pt-1 sm:px-6">
|
|
|
|
|
<h1 data-file-preview-heading-skeleton className="flex h-[1lh] items-center"><div className="h-[0.75em] w-2/3 rounded bg-muted/40" /></h1>
|
|
|
|
|
{["w-full", "w-5/6", "w-3/4"].map((width) => <div key={width} className="mt-4 flex h-[1lh] items-center">
|
2026-10-19 15:57:32 +08:00
|
|
|
<div className={`h-3 rounded bg-muted/40 ${width}`} />
|
|
|
|
|
</div>)}
|
2026-10-20 16:33:05 +08:00
|
|
|
</div> : <div className="px-4 py-3 font-mono text-code">
|
|
|
|
|
{["w-2/3", "w-full", "w-5/6", "w-3/4", "w-1/2", "w-3/5"].map((width) => <div key={width} className="flex h-[1lh] items-center">
|
2026-10-19 15:57:32 +08:00
|
|
|
<div className="w-[4.25em] shrink-0 pr-[1.15rem]"><div className="ml-auto h-3 w-2 rounded bg-muted/40" /></div>
|
|
|
|
|
<div className={`h-3 max-w-[calc(100%-4.25em)] rounded bg-muted/40 ${width}`} />
|
|
|
|
|
</div>)}
|
|
|
|
|
</div>}
|
|
|
|
|
</>}
|
2026-10-14 00:11:00 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : state.status === "error" ? (
|
|
|
|
|
<div className="flex h-full items-center justify-center px-8 text-center text-sm text-muted-foreground">
|
|
|
|
|
<div className="max-w-sm">
|
|
|
|
|
<AlertCircle
|
|
|
|
|
className="mx-auto mb-3 h-5 w-5 text-muted-foreground/70"
|
|
|
|
|
aria-hidden
|
|
|
|
|
/>
|
|
|
|
|
<p>{errorMessage}</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : state.payload.kind === "image" ? (
|
|
|
|
|
<div className="flex min-h-full items-center justify-center p-4">
|
|
|
|
|
<button type="button" aria-label={`${t("lightbox.open")}: ${fileName}`} onClick={() => onImageOpenChange(true)}
|
|
|
|
|
className="flex max-h-full max-w-full cursor-zoom-in items-center justify-center rounded-compact focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
|
|
|
|
|
<img
|
|
|
|
|
src={state.payload.data_url}
|
|
|
|
|
alt={fileName}
|
|
|
|
|
className="max-h-full max-w-full object-contain"
|
|
|
|
|
onError={onImageError}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
<ImageLightbox images={[{ url: state.payload.data_url, name: fileName }]} index={imageOpen ? 0 : null}
|
|
|
|
|
onIndexChange={() => {}} onOpenChange={onImageOpenChange} />
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="min-h-full">
|
|
|
|
|
{state.payload.truncated ? (
|
|
|
|
|
<div className="mx-4 mt-3 rounded-md border border-amber-500/25 bg-amber-500/10 px-3 py-2 text-xs text-amber-700 dark:text-amber-200">
|
|
|
|
|
{t("filePreview.truncated", {
|
|
|
|
|
defaultValue: "Preview is truncated because this file is large.",
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2026-10-17 20:30:27 +08:00
|
|
|
{markdown && mode === "preview" ? (
|
|
|
|
|
<MarkdownText documentPath={state.payload.path} onOpenFilePreview={onOpenFilePreview}
|
2026-10-20 16:33:05 +08:00
|
|
|
className="px-5 pb-6 pt-1 [overflow-wrap:anywhere] sm:px-6 [&>:first-child]:mt-0">
|
2026-10-17 20:30:27 +08:00
|
|
|
{state.payload.content}
|
|
|
|
|
</MarkdownText>
|
|
|
|
|
) : <CodeBlock
|
2026-10-14 00:11:00 +08:00
|
|
|
language={state.payload.language}
|
|
|
|
|
code={state.payload.content}
|
|
|
|
|
chrome="none"
|
|
|
|
|
highlight
|
|
|
|
|
showLineNumbers
|
|
|
|
|
wrapLongLines={false}
|
|
|
|
|
viewportHighlight
|
|
|
|
|
className="min-h-full"
|
2026-10-17 20:30:27 +08:00
|
|
|
/>}
|
2026-10-14 00:11:00 +08:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
}
|