Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 2x 2x | import { useApiHandler } from "@/services/useApiHandler";
import { AlertCircle, Download, File as FileIcon, Loader2, X } from "lucide-react";
import React, { useEffect, useState } from "react";
// ─── Document preview modal ───────────────────────────────────────────────────
const TEXT_MIME_TYPES = new Set([
"application/json",
"application/xml",
"application/x-yaml",
"application/yaml",
"text/markdown",
"text/x-markdown",
"text/x-rst",
]);
function isTextMime(mime: string) {
return mime.startsWith("text/") || TEXT_MIME_TYPES.has(mime);
}
const DocumentPreviewModal: React.FC<{
knowledgeId: string;
doc: {
id: string;
source_id: string;
document_metadata?: {
original_filename?: string;
mime_type?: string;
size_bytes?: number;
};
};
onClose: () => void;
onDownload: () => void;
}> = ({ knowledgeId, doc, onClose, onDownload }) => {
const { getApiHandler } = useApiHandler();
const filename = doc.document_metadata?.original_filename ?? doc.source_id;
const mime = doc.document_metadata?.mime_type ?? "application/octet-stream";
const [blobUrl, setBlobUrl] = useState<string | null>(null);
const [textContent, setTextContent] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const [fetchError, setFetchError] = useState(false);
useEffect(() => {
let url: string | null = null;
setLoading(true);
setFetchError(false);
getApiHandler()
.knowledge_router.fetchDocumentBlob(knowledgeId, doc.id)
.then(async (objectUrl) => {
if (!objectUrl) {
setFetchError(true);
return;
}
url = objectUrl;
if (isTextMime(mime)) {
const res = await fetch(objectUrl);
const text = await res.text();
setTextContent(text);
} else {
setBlobUrl(objectUrl);
}
})
.catch(() => setFetchError(true))
.finally(() => setLoading(false));
return () => {
if (url) URL.revokeObjectURL(url);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [doc.id]);
// Close on Escape
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [onClose]);
const isPdf = mime === "application/pdf";
const isImage = mime.startsWith("image/");
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm"
onClick={(e) => {
if (e.target === e.currentTarget) onClose();
}}
>
<div className="relative flex flex-col w-full max-w-4xl max-h-[90vh] rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 shadow-2xl overflow-hidden">
{/* Header */}
<div className="flex items-center gap-3 px-5 py-3.5 border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/60 shrink-0">
<FileIcon className="size-4 text-gray-400 dark:text-gray-500 shrink-0" />
<span className="flex-1 text-sm font-medium text-gray-800 dark:text-gray-200 truncate">
{filename}
</span>
<button
onClick={onDownload}
title="Download"
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-lg text-xs font-medium text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors"
>
<Download className="size-3.5" />
Download
</button>
<button
onClick={onClose}
title="Close"
className="p-1.5 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors"
>
<X className="size-4 text-gray-500 dark:text-gray-400" />
</button>
</div>
{/* Body */}
<div className="flex-1 overflow-auto min-h-0">
{loading ? (
<div className="flex flex-col items-center justify-center h-64 gap-3">
<Loader2 className="size-6 text-gray-400 animate-spin" />
<p className="text-sm text-gray-400 dark:text-gray-500">
Loading preview…
</p>
</div>
) : fetchError ? (
<div className="flex flex-col items-center justify-center h-64 gap-2 text-center px-6">
<AlertCircle className="size-6 text-red-400" />
<p className="text-sm text-red-600 dark:text-red-400 font-medium">
Failed to load preview.
</p>
<p className="text-xs text-gray-400 dark:text-gray-500">
Try downloading the file instead.
</p>
</div>
) : isPdf && blobUrl ? (
<iframe
src={blobUrl}
title={filename}
className="w-full h-full min-h-[70vh]"
/>
) : isImage && blobUrl ? (
<div className="flex items-center justify-center p-6 bg-gray-50 dark:bg-gray-800/50 min-h-64">
<img
src={blobUrl}
alt={filename}
className="max-w-full max-h-[70vh] rounded-lg object-contain shadow-md"
/>
</div>
) : isTextMime(mime) && textContent !== null ? (
<pre className="p-5 text-xs font-mono text-gray-800 dark:text-gray-200 whitespace-pre-wrap break-words leading-relaxed bg-gray-50 dark:bg-gray-900 min-h-64">
{textContent}
</pre>
) : (
<div className="flex flex-col items-center justify-center h-64 gap-3 text-center px-6">
<div className="p-3 rounded-full bg-gray-100 dark:bg-gray-700">
<FileIcon className="size-6 text-gray-400 dark:text-gray-500" />
</div>
<p className="text-sm text-gray-600 dark:text-gray-400 font-medium">
Preview not available for this file type.
</p>
<button
onClick={onDownload}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80 transition-opacity"
>
<Download className="size-3.5" />
Download to view
</button>
</div>
)}
</div>
</div>
</div>
);
};
export default DocumentPreviewModal;
|