Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Improve app load by deferring heavy renderers

Louis Beaumont95a0293d12fd
diff --git a/apps/screenpipe-app-tauri/components/rewind/chat-message.tsx b/apps/screenpipe-app-tauri/components/rewind/chat-message.tsx
index ab7ff6a4f..bf7a16ac5 100644
--- a/apps/screenpipe-app-tauri/components/rewind/chat-message.tsx
+++ b/apps/screenpipe-app-tauri/components/rewind/chat-message.tsx
@@ -1,12 +1,11 @@
import { Message } from "ai";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
-import { useState, useEffect, useRef } from "react";
+import { Suspense, lazy, useState, useEffect, useRef } from "react";
import { emit } from "@tauri-apps/api/event";
import { cn } from "@/lib/utils";
import { useTimelineStore } from "@/lib/hooks/use-timeline-store";
-import { CodeBlock } from "@/components/ui/codeblock";
import { MemoizedReactMarkdown } from "@/components/markdown";
import {
IconOpenAI,
@@ -17,12 +16,54 @@ import {
import { ChatMessageActions } from "@/components/rewind/chat-message-actions";
import { useSettings } from "@/lib/hooks/use-settings";
import { VideoComponent } from "./video";
-import { MermaidDiagram } from "./mermaid-diagram";
import { convertFileSrc } from "@tauri-apps/api/core";
import { ChevronDown } from "lucide-react";
import { commands } from "@/lib/utils/tauri";
import { toast } from "@/components/ui/use-toast";
+const MermaidDiagram = lazy(() =>
+ import("./mermaid-diagram").then((mod) => ({ default: mod.MermaidDiagram })),
+);
+const CodeBlock = lazy(() =>
+ import("@/components/ui/codeblock").then((mod) => ({ default: mod.CodeBlock })),
+);
+
+function MermaidDiagramBlock({ chart }: { chart: string }) {
+ return (
+ <Suspense
+ fallback={
+ <div className="my-4 text-xs text-muted-foreground">
+ rendering diagram...
+ </div>
+ }
+ >
+ <MermaidDiagram chart={chart} />
+ </Suspense>
+ );
+}
+
+function LazyCodeBlock({
+ language,
+ value,
+}: {
+ language: string;
+ value: string;
+}) {
+ return (
+ <Suspense
+ fallback={
+ <pre className="overflow-x-auto rounded-lg bg-neutral-900 dark:bg-neutral-950 p-3 my-2 text-xs max-w-full not-prose">
+ <code className="font-mono text-xs block whitespace-pre-wrap break-all text-neutral-200">
+ {value}
+ </code>
+ </pre>
+ }
+ >
+ <CodeBlock language={language} value={value} />
+ </Suspense>
+ );
+}
+
export interface ChatMessageProps {
message: Message;
}
@@ -269,7 +310,7 @@ export function ChatMessage({ message, ...props }: ChatMessageProps) {
// Render mermaid diagrams
if (language === "mermaid") {
- return <MermaidDiagram chart={content} />;
+ return <MermaidDiagramBlock chart={content} />;
}
if (isMP4File || !match) {
@@ -287,11 +328,10 @@ export function ChatMessage({ message, ...props }: ChatMessageProps) {
}
return (
- <CodeBlock
+ <LazyCodeBlock
key={Math.random()}
language={language}
value={content}
- {...props}
/>
);
},
diff --git a/apps/screenpipe-app-tauri/components/rewind/mermaid-diagram.tsx b/apps/screenpipe-app-tauri/components/rewind/mermaid-diagram.tsx
index 3e344a33a..4a2538c8f 100644
--- a/apps/screenpipe-app-tauri/components/rewind/mermaid-diagram.tsx
+++ b/apps/screenpipe-app-tauri/components/rewind/mermaid-diagram.tsx
@@ -1,7 +1,6 @@
"use client";
import { useEffect, useRef, useState } from "react";
-import mermaid from "mermaid";
// Screenpipe brand theme: grayscale, sharp corners, minimal
const SCREENPIPE_THEME = {
@@ -74,6 +73,8 @@ export function MermaidDiagram({ chart, className }: MermaidDiagramProps) {
if (!containerRef.current || !chart.trim()) return;
try {
+ const { default: mermaid } = await import("mermaid");
+
// Detect dark mode
const isDark = document.documentElement.classList.contains("dark");
const theme = isDark ? SCREENPIPE_THEME_DARK : SCREENPIPE_THEME;
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index b09d632d2..5acab8874 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -29,7 +29,6 @@ import { PipeAIIconLarge } from "@/components/pipe-ai-icon";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { MemoizedReactMarkdown } from "@/components/markdown";
import { VideoComponent } from "@/components/rewind/video";
-import { MermaidDiagram } from "@/components/rewind/mermaid-diagram";
import { convertFileSrc } from "@tauri-apps/api/core";
import { AIPresetsSelector } from "@/components/rewind/ai-presets-selector";
import { AIPreset, PiQueuedPrompt } from "@/lib/utils/tauri";
@@ -79,6 +78,26 @@ import {
normalizeQueueEventPayload,
queuedPreviewMatchesText,
} from "@/lib/chat-queue-controls";
+
+const MermaidDiagram = React.lazy(() =>
+ import("@/components/rewind/mermaid-diagram").then((mod) => ({
+ default: mod.MermaidDiagram,
+ }))
+);
+
+function MermaidDiagramBlock({ chart }: { chart: string }) {
+ return (
+ <React.Suspense
+ fallback={
+ <div className="my-4 text-xs text-muted-foreground">
+ rendering diagram...
+ </div>
+ }
+ >
+ <MermaidDiagram chart={chart} />
+ </React.Suspense>
+ );
+}
// Session ID is per-conversation — set on mount (new conv) and updated on load/new.
// Stored as a ref so event listeners always see the current value without stale closures.
@@ -1334,7 +1353,7 @@ function MarkdownBlock({ text, isUser }: { text: string; isUser: boolean }) {
const isCodeBlock = className?.includes("language-");
if (language === "mermaid") {
- return <MermaidDiagram chart={content} />;
+ return <MermaidDiagramBlock chart={content} />;
}
if (language === "app-stats") {
diff --git a/apps/screenpipe-app-tauri/components/ui/codeblock.tsx b/apps/screenpipe-app-tauri/components/ui/codeblock.tsx
index dd230dac1..f108dd0fb 100644
--- a/apps/screenpipe-app-tauri/components/ui/codeblock.tsx
+++ b/apps/screenpipe-app-tauri/components/ui/codeblock.tsx
@@ -4,7 +4,7 @@
"use client";
import { FC, memo } from "react";
-import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
+import { PrismAsyncLight as SyntaxHighlighter } from "react-syntax-highlighter";
import { coldarkDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { useCopyToClipboard } from "@/lib/hooks/use-copy-to-clipboard";