mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-07-17 00:23:09 +02:00
@ -16,17 +16,19 @@ interface TextViewProps {
|
|||||||
presentingDocument: OnyxDocument;
|
presentingDocument: OnyxDocument;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function TextView({
|
export default function TextView({
|
||||||
presentingDocument,
|
presentingDocument,
|
||||||
onClose,
|
onClose,
|
||||||
}: TextViewProps) {
|
}: TextViewProps) {
|
||||||
const [zoom, setZoom] = useState(100);
|
const [zoom, setZoom] = useState(100);
|
||||||
const [fileContent, setFileContent] = useState<string>("");
|
const [fileContent, setFileContent] = useState("");
|
||||||
const [fileUrl, setFileUrl] = useState<string>("");
|
const [fileUrl, setFileUrl] = useState("");
|
||||||
const [fileName, setFileName] = useState<string>("");
|
const [fileName, setFileName] = useState("");
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [fileType, setFileType] = useState<string>("application/octet-stream");
|
const [fileType, setFileType] = useState("application/octet-stream");
|
||||||
|
|
||||||
|
// Detect if a given MIME type is one of the recognized markdown formats
|
||||||
const isMarkdownFormat = (mimeType: string): boolean => {
|
const isMarkdownFormat = (mimeType: string): boolean => {
|
||||||
const markdownFormats = [
|
const markdownFormats = [
|
||||||
"text/markdown",
|
"text/markdown",
|
||||||
@ -38,6 +40,7 @@ export default function TextView({
|
|||||||
return markdownFormats.some((format) => mimeType.startsWith(format));
|
return markdownFormats.some((format) => mimeType.startsWith(format));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Detect if a given MIME type can be rendered in an <iframe>
|
||||||
const isSupportedIframeFormat = (mimeType: string): boolean => {
|
const isSupportedIframeFormat = (mimeType: string): boolean => {
|
||||||
const supportedFormats = [
|
const supportedFormats = [
|
||||||
"application/pdf",
|
"application/pdf",
|
||||||
@ -52,6 +55,7 @@ export default function TextView({
|
|||||||
const fetchFile = useCallback(async () => {
|
const fetchFile = useCallback(async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
const fileId = presentingDocument.document_id.split("__")[1];
|
const fileId = presentingDocument.document_id.split("__")[1];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`/api/chat/file/${encodeURIComponent(fileId)}`,
|
`/api/chat/file/${encodeURIComponent(fileId)}`,
|
||||||
@ -62,18 +66,33 @@ export default function TextView({
|
|||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
const url = window.URL.createObjectURL(blob);
|
const url = window.URL.createObjectURL(blob);
|
||||||
setFileUrl(url);
|
setFileUrl(url);
|
||||||
setFileName(presentingDocument.semantic_identifier || "document");
|
|
||||||
const contentType =
|
const originalFileName =
|
||||||
|
presentingDocument.semantic_identifier || "document";
|
||||||
|
setFileName(originalFileName);
|
||||||
|
|
||||||
|
let contentType =
|
||||||
response.headers.get("Content-Type") || "application/octet-stream";
|
response.headers.get("Content-Type") || "application/octet-stream";
|
||||||
|
|
||||||
|
// If it's octet-stream but file name suggests a markdown extension, override and attempt to read as markdown
|
||||||
|
if (
|
||||||
|
contentType === "application/octet-stream" &&
|
||||||
|
(originalFileName.toLowerCase().endsWith(".md") ||
|
||||||
|
originalFileName.toLowerCase().endsWith(".markdown"))
|
||||||
|
) {
|
||||||
|
contentType = "text/markdown";
|
||||||
|
}
|
||||||
setFileType(contentType);
|
setFileType(contentType);
|
||||||
|
|
||||||
if (isMarkdownFormat(blob.type)) {
|
// If the final content type looks like markdown, read its text
|
||||||
|
if (isMarkdownFormat(contentType)) {
|
||||||
const text = await blob.text();
|
const text = await blob.text();
|
||||||
setFileContent(text);
|
setFileContent(text);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching file:", error);
|
console.error("Error fetching file:", error);
|
||||||
} finally {
|
} finally {
|
||||||
|
// Keep the slight delay for a smoother loading experience
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
@ -97,11 +116,8 @@ export default function TextView({
|
|||||||
const handleZoomOut = () => setZoom((prev) => Math.max(prev - 25, 100));
|
const handleZoomOut = () => setZoom((prev) => Math.max(prev - 25, 100));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={true} onOpenChange={onClose}>
|
<Dialog open onOpenChange={onClose}>
|
||||||
<DialogContent
|
<DialogContent className="max-w-5xl w-[90vw] flex flex-col justify-between gap-y-0 h-full max-h-[80vh] p-0">
|
||||||
hideCloseIcon
|
|
||||||
className="max-w-5xl w-[90vw] flex flex-col justify-between gap-y-0 h-full max-h-[80vh] p-0"
|
|
||||||
>
|
|
||||||
<DialogHeader className="px-4 mb-0 pt-2 pb-3 flex flex-row items-center justify-between border-b">
|
<DialogHeader className="px-4 mb-0 pt-2 pb-3 flex flex-row items-center justify-between border-b">
|
||||||
<DialogTitle className="text-lg font-medium truncate">
|
<DialogTitle className="text-lg font-medium truncate">
|
||||||
{fileName}
|
{fileName}
|
||||||
@ -120,12 +136,13 @@ export default function TextView({
|
|||||||
<Download className="h-4 w-4" />
|
<Download className="h-4 w-4" />
|
||||||
<span className="sr-only">Download</span>
|
<span className="sr-only">Download</span>
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="ghost" size="icon" onClick={() => onClose()}>
|
<Button variant="ghost" size="icon" onClick={onClose}>
|
||||||
<XIcon className="h-4 w-4" />
|
<XIcon className="h-4 w-4" />
|
||||||
<span className="sr-only">Close</span>
|
<span className="sr-only">Close</span>
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|
||||||
<div className="mt-0 rounded-b-lg flex-1 overflow-hidden">
|
<div className="mt-0 rounded-b-lg flex-1 overflow-hidden">
|
||||||
<div className="flex items-center justify-center w-full h-full">
|
<div className="flex items-center justify-center w-full h-full">
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
@ -137,7 +154,7 @@ export default function TextView({
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div
|
<div
|
||||||
className={`w-full h-full transform origin-center transition-transform duration-300 ease-in-out`}
|
className="w-full h-full transform origin-center transition-transform duration-300 ease-in-out"
|
||||||
style={{ transform: `scale(${zoom / 100})` }}
|
style={{ transform: `scale(${zoom / 100})` }}
|
||||||
>
|
>
|
||||||
{isSupportedIframeFormat(fileType) ? (
|
{isSupportedIframeFormat(fileType) ? (
|
||||||
@ -150,7 +167,7 @@ export default function TextView({
|
|||||||
<div className="w-full h-full p-6 overflow-y-scroll overflow-x-hidden">
|
<div className="w-full h-full p-6 overflow-y-scroll overflow-x-hidden">
|
||||||
<MinimalMarkdown
|
<MinimalMarkdown
|
||||||
content={fileContent}
|
content={fileContent}
|
||||||
className="w-full pb-4 h-full text-lg text-wrap break-words"
|
className="w-full pb-4 h-full text-lg break-words"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
|
Reference in New Issue
Block a user