Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Markdown preview: keep a failed load inside the dialog, and offer saving a file too large to preview

AdityaUmale82740b29c963
diff --git a/src/components/HtmlFilePreview.tsx b/src/components/HtmlFilePreview.tsx
index 167bfef5c3..8fcfd422c9 100644
--- a/src/components/HtmlFilePreview.tsx
+++ b/src/components/HtmlFilePreview.tsx
@@ -1,4 +1,4 @@
-import { lazy, Suspense, useEffect, useRef, useState } from "react";
+import { Component, lazy, Suspense, useEffect, useRef, useState, type ReactNode } from "react";
import { Check, Code2, Download, ExternalLink, Eye, LoaderCircle, RotateCcw } from "lucide-react";
import { requestMessageFile, useLocalFileSave, type MessageAttachmentContext } from "./AttachmentPreview";
import { TableDialog } from "./RichTable";
@@ -21,6 +21,20 @@ export function isMarkdownFile(name: string): boolean {
// Lazy, so the chat's Markdown renderer (which uses this file's chips) is not imported back into it.
const MarkdownFileContent = lazy(() => import("./MarkdownFilePreview"));
+// A failed import stays failed until the page reloads, and without a boundary it would replace the whole message.
+class MarkdownPreviewBoundary extends Component<{ children: ReactNode }, { failed: boolean }> {
+ state = { failed: false };
+
+ static getDerivedStateFromError() {
+ return { failed: true };
+ }
+
+ render() {
+ if (!this.state.failed) return this.props.children;
+ return <div className="p-8 text-center text-sm"><p role="alert" className="mb-3 text-ink-secondary">{t("markdown.loadFailed")}</p><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => location.reload()}>{t("page.broken.reload")}</button></div>;
+ }
+}
+
/** Security: `<base target="_blank">` turns every link into a popup, which the empty sandbox blocks, so the frame never navigates. */
export function htmlPreviewDocument(source: string): string {
return `<meta http-equiv="Content-Security-Policy" content="${HTML_PREVIEW_CSP}"><base target="_blank">${source}`;
@@ -52,7 +66,7 @@ export function useHtmlPreview(path: string, name: string, message?: MessageAtta
? <TableDialog title={name} returnFocus={opener.current} onClose={() => setOpen(false)}>
{kind === "html"
? <HtmlFileContent key={key} path={path} name={name} message={message} />
- : <Suspense fallback={<div role="status" className="flex items-center justify-center gap-2 p-12 text-sm text-ink-secondary"><LoaderCircle size={16} className="animate-spin" />{t("markdown.loading")}</div>}><MarkdownFileContent key={key} path={path} name={name} message={message} /></Suspense>}
+ : <MarkdownPreviewBoundary><Suspense fallback={<div role="status" className="flex items-center justify-center gap-2 p-12 text-sm text-ink-secondary"><LoaderCircle size={16} className="animate-spin" />{t("markdown.loading")}</div>}><MarkdownFileContent key={key} path={path} name={name} message={message} /></Suspense></MarkdownPreviewBoundary>}
</TableDialog>
: null,
};
diff --git a/src/components/MarkdownFilePreview.tsx b/src/components/MarkdownFilePreview.tsx
index 81206e3d7c..fde4faa24d 100644
--- a/src/components/MarkdownFilePreview.tsx
+++ b/src/components/MarkdownFilePreview.tsx
@@ -30,7 +30,8 @@ export default function MarkdownFileContent({ path, name, message }: { path: str
})();
return () => controller.abort();
}, [path, message.threadId, message.messageId, attempt]);
- if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="mb-3 text-ink-secondary">{t(error === "size" ? "markdown.tooLarge" : "markdown.loadFailed")}</p><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => setAttempt(attempt + 1)}>{t("chat.retry")}</button></div>;
+ if (error === "size") return <div className="p-8 text-center text-sm"><p role="alert" className="mb-3 text-ink-secondary">{t("markdown.tooLarge")}</p><button type="button" className="inline-flex items-center gap-1.5 rounded-lg border border-hairline px-3 py-2 hover:bg-raised" disabled={save.state === "saving"} onClick={() => void save.save()}><Download size={14} />{t("attach.saveAria", { name })}</button>{save.state === "failed" && <p role="alert" className="mt-2 text-[12px] text-danger">{save.reason}</p>}</div>;
+ if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="mb-3 text-ink-secondary">{t("markdown.loadFailed")}</p><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => setAttempt(attempt + 1)}>{t("chat.retry")}</button></div>;
if (source === null) return <div role="status" className="flex items-center justify-center gap-2 p-12 text-sm text-ink-secondary"><LoaderCircle size={16} className="animate-spin" />{t("markdown.loading")}</div>;
return <div className="flex flex-col gap-2">
<div className="flex items-center justify-end gap-1">