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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 268x 3x 2x 2x 2x 2x 2x | import type { Message } from "../api";
import { formatFullDate, hasRemoteImages, sanitizeEmailHtml } from "../email-sanitizer";
import { formatAddressList } from "../utils";
import { AttachmentList } from "./AttachmentList";
import {
ChevronDownIcon,
ChevronRightIcon,
ForwardIcon,
ImageIcon,
ReplyAllIcon,
ReplyIcon,
} from "./Icons";
import { SandboxedEmail } from "./SandboxedEmail";
import { highlightText } from "./SearchPanel";
interface ThreadMessageProps {
msg: Message;
isThread: boolean;
isLast: boolean;
expanded: boolean;
showHtml: boolean;
imagesAllowed: boolean;
senderTrusted: boolean;
dark?: boolean;
searchQuery?: string;
onToggleExpanded: (id: number) => void;
onToggleShowHtml: () => void;
onAllowImages: (id: number) => void;
onTrustSender: (senderAddress: string) => void;
onReply: (msg: Message) => void;
onReplyAll: (msg: Message) => void;
onForward: (msg: Message) => void;
}
/** Renders a single message within a thread or standalone view.
* Handles the collapsible header, HTML/plain text body toggle,
* remote image blocking banner, attachments, and reply/forward actions. */
export function ThreadMessage({
msg,
isThread,
isLast,
expanded,
showHtml,
imagesAllowed,
senderTrusted,
dark,
searchQuery,
onToggleExpanded,
onToggleShowHtml,
onAllowImages,
onTrustSender,
onReply,
onReplyAll,
onForward,
}: ThreadMessageProps) {
return (
<div className="border-b border-gray-100 dark:border-gray-800">
{/* Message header โ clickable to expand/collapse in threads */}
<button
type="button"
onClick={() => (!isLast ? onToggleExpanded(msg.id) : undefined)}
aria-expanded={isThread && !isLast ? expanded : undefined}
aria-label={
isThread && !isLast
? `${expanded ? "Collapse" : "Expand"} message from ${msg.from_name || msg.from_address}`
: undefined
}
className={`w-full text-left px-6 py-3 ${
isThread && !isLast ? "cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-900" : ""
}`}
>
<div className="flex items-center gap-3">
{/* Thread expand/collapse chevron */}
{isThread && !isLast && (
<div className="flex-shrink-0 text-gray-400">
{expanded ? (
<ChevronDownIcon className="w-3.5 h-3.5" />
) : (
<ChevronRightIcon className="w-3.5 h-3.5" />
)}
</div>
)}
{/* Avatar */}
<div className="w-8 h-8 rounded-full bg-stork-100 dark:bg-stork-900 flex items-center justify-center text-sm font-medium text-stork-700 dark:text-stork-300 flex-shrink-0">
{(msg.from_name || msg.from_address || "?")[0]?.toUpperCase()}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-baseline gap-2">
<span className="font-medium text-sm">{msg.from_name || msg.from_address}</span>
<span className="text-xs text-gray-400 truncate"><{msg.from_address}></span>
</div>
{expanded && (
<div className="text-xs text-gray-500 mt-0.5">
To: {formatAddressList(msg.to_addresses)}
{msg.cc_addresses && <span> ยท CC: {formatAddressList(msg.cc_addresses)}</span>}
</div>
)}
</div>
<div className="text-xs text-gray-400 flex-shrink-0">
{expanded
? formatFullDate(msg.date)
: msg.date && !Number.isNaN(new Date(msg.date).getTime())
? new Date(msg.date).toLocaleDateString()
: ""}
</div>
</div>
</button>
{/* Message body */}
{expanded && (
<div className="px-6 pb-4">
{/* Toggle HTML/Plain text */}
{msg.html_body && msg.text_body && (
<div className="mb-2">
<button
type="button"
onClick={onToggleShowHtml}
className="text-xs text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
>
{showHtml ? "Show plain text" : "Show formatted"}
</button>
</div>
)}
{/* Remote images banner */}
{showHtml &&
msg.html_body &&
!imagesAllowed &&
!senderTrusted &&
hasRemoteImages(msg.html_body) && (
<div className="mb-3 flex items-center gap-2 px-3 py-2 bg-amber-50 dark:bg-amber-950/30 border border-amber-200 dark:border-amber-800 rounded-md text-sm text-amber-700 dark:text-amber-400">
<ImageIcon className="w-4 h-4 flex-shrink-0" />
<span>
Remote images are hidden to protect your privacy. Tracking pixels are always
blocked.
</span>
<div className="ml-auto flex items-center gap-2">
<button
type="button"
onClick={() => onAllowImages(msg.id)}
className="text-xs font-medium text-amber-600 dark:text-amber-300 hover:text-amber-800 dark:hover:text-amber-200 whitespace-nowrap"
title="Load remote images for this message only. This choice is not remembered โ images will be hidden again next time you open this message."
>
Show once
</button>
{msg.from_address && (
<>
<span className="text-amber-300 dark:text-amber-700">|</span>
<button
type="button"
onClick={() => onTrustSender(msg.from_address)}
className="text-xs font-medium text-amber-600 dark:text-amber-300 hover:text-amber-800 dark:hover:text-amber-200 whitespace-nowrap"
title={`Always load remote images from ${msg.from_address}. Tracking pixels are still blocked.`}
>
Always show from this sender
</button>
</>
)}
</div>
</div>
)}
{showHtml && msg.html_body ? (
<SandboxedEmail
html={sanitizeEmailHtml(msg.html_body, {
blockRemoteImages: !imagesAllowed && !senderTrusted,
messageId: msg.id,
})}
className="email-content"
allowRemoteImages={imagesAllowed || senderTrusted}
dark={dark}
searchQuery={searchQuery}
/>
) : (
<pre className="whitespace-pre-wrap text-sm text-gray-700 dark:text-gray-300 font-sans">
{searchQuery
? highlightText(msg.text_body || "(empty message)", searchQuery)
: msg.text_body || "(empty message)"}
</pre>
)}
{/* Attachments */}
{msg.has_attachments > 0 && <AttachmentList messageId={msg.id} />}
{/* Actions โ available on every thread message */}
<div className="flex items-center gap-2 mt-4 pt-3 border-t border-gray-100 dark:border-gray-800">
<button
type="button"
onClick={() => onReply(msg)}
className="px-3 py-1.5 text-sm bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded-md transition-colors flex items-center gap-1.5"
>
<ReplyIcon className="w-3.5 h-3.5" /> Reply
</button>
<button
type="button"
onClick={() => onReplyAll(msg)}
className="px-3 py-1.5 text-sm bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded-md transition-colors flex items-center gap-1.5"
>
<ReplyAllIcon className="w-3.5 h-3.5" /> Reply All
</button>
<button
type="button"
onClick={() => onForward(msg)}
className="px-3 py-1.5 text-sm bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded-md transition-colors flex items-center gap-1.5"
>
<ForwardIcon className="w-3.5 h-3.5" /> Forward
</button>
</div>
</div>
)}
</div>
);
}
|