Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: add dark mode support for HTML email bodies

Gener8ab62ae1c127
diff --git a/src/web/src/app/(app)/w/[slug]/agents/[id]/email/page.tsx b/src/web/src/app/(app)/w/[slug]/agents/[id]/email/page.tsx
index b4c0a19bf..4195d78bf 100644
--- a/src/web/src/app/(app)/w/[slug]/agents/[id]/email/page.tsx
+++ b/src/web/src/app/(app)/w/[slug]/agents/[id]/email/page.tsx
@@ -14,6 +14,7 @@ import { toast } from "sonner";
import { Skeleton } from "@/components/ui/skeleton";
import { Loader2, Mail, Inbox, Send, Plus, Trash2, Forward, Reply, Paperclip, File as FileIcon, Copy, Check, ShieldX } from "lucide-react";
import { ResizablePanels } from "@/components/ui/resizable-panels";
+import { EmailBodyFrame } from "@/components/email-body-frame";
type Folder = "inbox" | "sent" | "rejected";
@@ -425,9 +426,9 @@ export default function AgentEmailPage() {
<p className="text-xs text-muted-foreground mb-2">{parent.subject}</p>
{threadBodies[parent.id] ? (
threadBodies[parent.id].isHtml ? (
- <div
- className="markdown max-w-full text-sm text-foreground"
- dangerouslySetInnerHTML={{ __html: threadBodies[parent.id].content }}
+ <EmailBodyFrame
+ html={threadBodies[parent.id].content}
+ className="max-w-full text-sm"
/>
) : (
<div className="text-sm whitespace-pre-wrap leading-[1.65] text-foreground">
@@ -473,14 +474,14 @@ export default function AgentEmailPage() {
<Loader2 className="size-4 animate-spin text-muted-foreground" />
</div>
) : selected.html_body ? (
- <div
- className="markdown max-w-full text-base text-foreground"
- dangerouslySetInnerHTML={{ __html: selected.html_body }}
+ <EmailBodyFrame
+ html={selected.html_body}
+ className="max-w-full"
/>
) : body?.isHtml ? (
- <div
- className="markdown max-w-full text-base text-foreground"
- dangerouslySetInnerHTML={{ __html: body.content }}
+ <EmailBodyFrame
+ html={body.content}
+ className="max-w-full"
/>
) : (
<div className="text-sm whitespace-pre-wrap leading-[1.65] text-foreground">
diff --git a/src/web/src/components/email-body-frame.test.ts b/src/web/src/components/email-body-frame.test.ts
new file mode 100644
index 000000000..516f25dcd
--- /dev/null
+++ b/src/web/src/components/email-body-frame.test.ts
@@ -0,0 +1,45 @@
+import { describe, it, expect } from "vitest";
+import { buildSrcDoc } from "./email-body-frame";
+
+describe("buildSrcDoc", () => {
+ const sampleHtml = '<p style="color: #000;">Hello world</p>';
+
+ it("wraps HTML in a full document", () => {
+ const doc = buildSrcDoc(sampleHtml, false);
+ expect(doc).toContain("<!DOCTYPE html>");
+ expect(doc).toContain("<body>");
+ expect(doc).toContain(sampleHtml);
+ });
+
+ it("injects light mode styles by default", () => {
+ const doc = buildSrcDoc(sampleHtml, false);
+ expect(doc).toContain('name="color-scheme" content="light"');
+ expect(doc).toContain("color: #1a1a1a");
+ expect(doc).not.toContain("color-scheme: dark");
+ });
+
+ it("injects dark mode styles when isDark is true", () => {
+ const doc = buildSrcDoc(sampleHtml, true);
+ expect(doc).toContain('name="color-scheme" content="dark"');
+ expect(doc).toContain("color-scheme: dark");
+ expect(doc).toContain("color: #e8e5e0");
+ expect(doc).toContain("background-color: transparent !important");
+ });
+
+ it("forces text color with !important in dark mode", () => {
+ const doc = buildSrcDoc(sampleHtml, true);
+ expect(doc).toContain("color: #e8e5e0 !important");
+ });
+
+ it("sets max-width on images to prevent overflow", () => {
+ const imgHtml = '<img src="photo.jpg" width="2000">';
+ const doc = buildSrcDoc(imgHtml, false);
+ expect(doc).toContain("max-width: 100%");
+ });
+
+ it("preserves the original HTML content unchanged", () => {
+ const complexHtml = '<table><tr><td style="background:#fff;color:#000">Cell</td></tr></table>';
+ const doc = buildSrcDoc(complexHtml, true);
+ expect(doc).toContain(complexHtml);
+ });
+});
diff --git a/src/web/src/components/email-body-frame.tsx b/src/web/src/components/email-body-frame.tsx
new file mode 100644
index 000000000..2eec2c8b3
--- /dev/null
+++ b/src/web/src/components/email-body-frame.tsx
@@ -0,0 +1,117 @@
+"use client";
+
+import { useRef, useEffect, useState } from "react";
+import { useTheme } from "next-themes";
+
+interface EmailBodyFrameProps {
+ html: string;
+ className?: string;
+}
+
+const LIGHT_STYLES = `
+ html, body {
+ margin: 0;
+ padding: 0;
+ background: transparent;
+ color: #1a1a1a;
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
+ font-size: 15px;
+ line-height: 1.6;
+ word-wrap: break-word;
+ overflow-wrap: break-word;
+ }
+ a { color: inherit; }
+ img { max-width: 100%; height: auto; }
+`;
+
+const DARK_STYLES = `
+ html, body {
+ margin: 0;
+ padding: 0;
+ background: transparent;
+ color: #e8e5e0;
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
+ font-size: 15px;
+ line-height: 1.6;
+ word-wrap: break-word;
+ overflow-wrap: break-word;
+ color-scheme: dark;
+ }
+ a { color: inherit; }
+ img { max-width: 100%; height: auto; }
+ /* Force dark-friendly defaults on common email patterns */
+ body, div, td, th, p, span, li {
+ color: #e8e5e0 !important;
+ }
+ table, tr, td, th, div, section, article, header, footer, main {
+ background-color: transparent !important;
+ border-color: rgba(255, 255, 255, 0.1) !important;
+ }
+ /* Preserve image backgrounds (logos, etc) */
+ img { background-color: transparent !important; }
+ h1, h2, h3, h4, h5, h6 {
+ color: #f0ede8 !important;
+ }
+ a { color: #93b5ff !important; }
+`;
+
+export function buildSrcDoc(html: string, isDark: boolean): string {
+ const styles = isDark ? DARK_STYLES : LIGHT_STYLES;
+ return `<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <meta name="color-scheme" content="${isDark ? "dark" : "light"}">
+ <style>${styles}</style>
+</head>
+<body>${html}</body>
+</html>`;
+}
+
+export function EmailBodyFrame({ html, className }: EmailBodyFrameProps) {
+ const iframeRef = useRef<HTMLIFrameElement>(null);
+ const { resolvedTheme } = useTheme();
+ const [height, setHeight] = useState(200);
+ const isDark = resolvedTheme === "dark";
+
+ useEffect(() => {
+ const iframe = iframeRef.current;
+ if (!iframe) return;
+
+ let ro: ResizeObserver | null = null;
+
+ iframe.srcdoc = buildSrcDoc(html, isDark);
+
+ const onLoad = () => {
+ const doc = iframe.contentDocument;
+ if (!doc) return;
+
+ const updateHeight = () => {
+ const h = doc.documentElement.scrollHeight;
+ if (h > 0) setHeight(h);
+ };
+
+ updateHeight();
+
+ ro = new ResizeObserver(updateHeight);
+ ro.observe(doc.documentElement);
+ };
+
+ iframe.addEventListener("load", onLoad);
+ return () => {
+ iframe.removeEventListener("load", onLoad);
+ ro?.disconnect();
+ };
+ }, [html, isDark]);
+
+ return (
+ <iframe
+ ref={iframeRef}
+ sandbox="allow-same-origin"
+ className={className}
+ style={{ width: "100%", height: `${height}px`, border: "none", display: "block" }}
+ title="Email content"
+ />
+ );
+}