All files / src/components ComposeModal.tsx

99.5% Statements 202/203
88.58% Branches 163/184
100% Functions 47/47
100% Lines 180/180

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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691                                                                                      480x   1x                                     479x                                                   480x 480x 93x     480x     480x 88x 88x 70x 46x   480x 88x 88x 70x 64x   480x 88x 88x 70x   480x 88x 88x 70x 24x 46x 38x   480x 88x 88x 70x 46x 8x 8x     8x   38x   480x 88x 88x 86x 25x 61x 53x   480x 88x 88x   480x 88x 88x 70x 64x   480x 88x 88x   480x 480x 480x 480x 480x 480x 480x 480x 480x     480x 88x   23x 23x       5x   18x   88x       480x 95x 13x         480x 445x     480x 5x     480x 19x 19x 2x 2x   17x 2x 2x 2x 2x     15x 3x 3x 2x 2x     13x 13x 13x 13x 19x 19x                 11x     2x 2x       480x 7x 7x     480x   352x 2x 2x           480x   5x   4x 4x 4x   4x 4x 4x     1x   1x 1x 1x   5x 5x         480x   7x     7x           7x 3x 3x   4x           480x 1x 1x           480x   3x   1x 1x 1x 1x 1x 1x 1x 1x       2x             480x               480x       480x               1x     353x           74x               1x                                                 1x       28x                                 241x 241x                 3x                 5x                                   18x 18x                                 49x 49x                             27x                                                                                             7x                       15x                                                           1x 1x                 1x 1x                                                                                                                                 2400x 2400x 2400x 2400x   2400x         2x 2x                   480x         2x 2x 2x 1x                  
import { useCallback, useEffect, useRef, useState } from "react";
import type { Identity } from "../api";
import {
	buildForwardHtmlBody,
	buildForwardSubject,
	buildReplyAllCc,
	buildReplyBody,
	buildReplyHtmlBody,
	buildReplySubject,
	type ComposeFormat,
	type ComposeMode,
	clearDraft,
	draftKey,
	getInitialFormat,
	htmlToPlainText,
	loadDraft,
	plainTextToHtml,
	saveDraft,
	validateEmails,
} from "../compose-utils";
import { useFocusTrap } from "../hooks";
import { XIcon } from "./Icons";
 
export type { ComposeMode } from "../compose-utils";
 
interface ComposeModalProps {
	mode: ComposeMode;
	identities?: Identity[];
	selectedIdentityId?: number | null;
	onClose: () => void;
	onSend: (data: {
		identityId?: number;
		to: string;
		cc: string;
		bcc: string;
		subject: string;
		body: string;
		htmlBody?: string;
	}) => void | Promise<void>;
}
 
/** Expand/collapse icon for the compose window */
function ExpandIcon({ expanded }: { expanded: boolean }) {
	if (expanded) {
		// Collapse icon (shrink)
		return (
			<svg
				className="w-4 h-4"
				viewBox="0 0 24 24"
				fill="none"
				stroke="currentColor"
				strokeWidth="2"
				strokeLinecap="round"
				strokeLinejoin="round"
			>
				<title>Collapse</title>
				<polyline points="4 14 10 14 10 20" />
				<polyline points="20 10 14 10 14 4" />
				<line x1="14" y1="10" x2="21" y2="3" />
				<line x1="3" y1="21" x2="10" y2="14" />
			</svg>
		);
	}
	// Expand icon (grow)
	return (
		<svg
			className="w-4 h-4"
			viewBox="0 0 24 24"
			fill="none"
			stroke="currentColor"
			strokeWidth="2"
			strokeLinecap="round"
			strokeLinejoin="round"
		>
			<title>Expand</title>
			<polyline points="15 3 21 3 21 9" />
			<polyline points="9 21 3 21 3 15" />
			<line x1="21" y1="3" x2="14" y2="10" />
			<line x1="3" y1="21" x2="10" y2="14" />
		</svg>
	);
}
 
export function ComposeModal({
	mode,
	identities,
	selectedIdentityId,
	onClose,
	onSend,
}: ComposeModalProps) {
	const currentDraftKey = draftKey(mode);
	const userEmail = identities?.find(
		(a) => a.id === (selectedIdentityId ?? identities[0]?.id),
	)?.email;
 
	const [fromIdentityId, setFromIdentityId] = useState<number | undefined>(
		selectedIdentityId ?? undefined,
	);
	const [to, setTo] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved) return saved.to;
		if (mode.type === "reply" || mode.type === "reply-all") return mode.original.from_address;
		return "";
	});
	const [cc, setCc] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved) return saved.cc;
		if (mode.type === "reply-all") return buildReplyAllCc(mode.original, userEmail);
		return "";
	});
	const [bcc, setBcc] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved) return saved.bcc;
		return "";
	});
	const [subject, setSubject] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved) return saved.subject;
		if (mode.type === "reply" || mode.type === "reply-all")
			return buildReplySubject(mode.original.subject);
		if (mode.type === "forward") return buildForwardSubject(mode.original.subject);
		return "";
	});
	const [body, setBody] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved) return saved.body;
		if (mode.type === "reply" || mode.type === "reply-all") return buildReplyBody(mode.original);
		if (mode.type === "forward") {
			const msg = mode.original;
			const from = msg.from_name
				? `${msg.from_name} <${msg.from_address || "unknown"}>`
				: msg.from_address || "unknown";
			return `\n\n---------- Forwarded message ----------\nFrom: ${from}\nDate: ${new Date(msg.date).toLocaleString()}\nSubject: ${msg.subject || "(no subject)"}\nTo: ${msg.to_addresses || ""}\n\n${msg.text_body || ""}`;
		}
		return "";
	});
	const [htmlBody, setHtmlBody] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved?.htmlBody) return saved.htmlBody;
		if (mode.type === "reply" || mode.type === "reply-all")
			return buildReplyHtmlBody(mode.original);
		if (mode.type === "forward") return buildForwardHtmlBody(mode.original);
		return "";
	});
	const [format, setFormat] = useState<ComposeFormat>(() => {
		const saved = loadDraft(currentDraftKey);
		return getInitialFormat(mode, saved);
	});
	const [showCc, setShowCc] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		if (saved) return !!saved.cc;
		if (mode.type === "reply-all") return buildReplyAllCc(mode.original, userEmail).length > 0;
		return false;
	});
	const [showBcc, setShowBcc] = useState(() => {
		const saved = loadDraft(currentDraftKey);
		return !!saved?.bcc;
	});
	const [expanded, setExpanded] = useState(false);
	const [sending, setSending] = useState(false);
	const [validationError, setValidationError] = useState<string | null>(null);
	const [showFormatWarning, setShowFormatWarning] = useState(false);
	const pendingFormatRef = useRef<ComposeFormat | null>(null);
	const toInputRef = useRef<HTMLInputElement>(null);
	const dialogRef = useRef<HTMLDivElement>(null);
	const editorRef = useRef<HTMLDivElement>(null);
	useFocusTrap(dialogRef);
 
	// Auto-focus the To field on mount
	useEffect(() => {
		const timer = setTimeout(() => {
			// Don't steal focus if the user has already clicked into another field
			const modal = toInputRef.current?.closest('[role="dialog"]');
			if (
				modal?.contains(document.activeElement) &&
				document.activeElement !== toInputRef.current
			) {
				return;
			}
			toInputRef.current?.focus();
		}, 50);
		return () => clearTimeout(timer);
	}, []);
 
	// Set initial HTML content in the editor
	useEffect(() => {
		if (format === "html" && editorRef.current && !editorRef.current.innerHTML) {
			editorRef.current.innerHTML = htmlBody;
		}
	}, [format, htmlBody]);
 
	// Auto-save draft for all compose modes
	useEffect(() => {
		saveDraft(currentDraftKey, { to, cc, bcc, subject, body, htmlBody, format });
	}, [currentDraftKey, to, cc, bcc, subject, body, htmlBody, format]);
 
	const getEditorHtml = useCallback((): string => {
		return editorRef.current?.innerHTML || "";
	}, []);
 
	const handleSend = useCallback(async () => {
		const toErr = validateEmails(to);
		if (toErr) {
			setValidationError(toErr);
			return;
		}
		if (cc.trim()) {
			const ccErr = validateEmails(cc);
			Eif (ccErr) {
				setValidationError(ccErr);
				return;
			}
		}
		if (bcc.trim()) {
			const bccErr = validateEmails(bcc);
			if (bccErr) {
				setValidationError(bccErr);
				return;
			}
		}
		setValidationError(null);
		setSending(true);
		try {
			const currentHtml = format === "html" ? getEditorHtml() : undefined;
			const textBody = format === "html" ? htmlToPlainText(currentHtml || "") : body;
			await onSend({
				identityId: fromIdentityId,
				to: to.trim(),
				cc: cc.trim(),
				bcc: bcc.trim(),
				subject,
				body: textBody,
				htmlBody: currentHtml,
			});
			clearDraft(currentDraftKey);
		} catch (err) {
			// Send failed — keep the draft and let the user retry
			setSending(false);
			setValidationError(err instanceof Error ? err.message : "Failed to send message");
		}
	}, [to, cc, bcc, subject, body, format, fromIdentityId, onSend, currentDraftKey, getEditorHtml]);
 
	const handleDiscard = useCallback(() => {
		clearDraft(currentDraftKey);
		onClose();
	}, [currentDraftKey, onClose]);
 
	const handleKeyDown = useCallback(
		(e: React.KeyboardEvent) => {
			if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
				e.preventDefault();
				handleSend();
			}
		},
		[handleSend],
	);
 
	const switchFormat = useCallback(
		(newFormat: ComposeFormat) => {
			if (newFormat === "html" && format === "plain") {
				// Convert plain text body to HTML
				const html = plainTextToHtml(body);
				setHtmlBody(html);
				setFormat("html");
				// Set content in editor after it renders
				requestAnimationFrame(() => {
					Eif (editorRef.current) {
						editorRef.current.innerHTML = html;
					}
				});
			E} else if (newFormat === "plain" && format === "html") {
				// Convert HTML to plain text
				const currentHtml = getEditorHtml();
				setBody(htmlToPlainText(currentHtml));
				setFormat("plain");
			}
			setShowFormatWarning(false);
			pendingFormatRef.current = null;
		},
		[format, body, getEditorHtml],
	);
 
	const requestFormatSwitch = useCallback(
		(newFormat: ComposeFormat) => {
			Iif (newFormat === format) return;
			// Warn about formatting loss when switching from HTML to plain text
			const hasContent =
				format === "html"
					? (getEditorHtml() || "")
							.replace(/<br\s*\/?>/gi, "")
							.replace(/<[^>]*>/g, "")
							.trim().length > 0
					: body.trim().length > 0;
			if (hasContent && format === "html" && newFormat === "plain") {
				pendingFormatRef.current = newFormat;
				setShowFormatWarning(true);
			} else {
				switchFormat(newFormat);
			}
		},
		[format, body, getEditorHtml, switchFormat],
	);
 
	const handleEditorInput = useCallback(() => {
		Eif (editorRef.current) {
			setHtmlBody(editorRef.current.innerHTML);
		}
	}, []);
 
	// Auto-escalate: when user uses a formatting control while in plain text mode,
	// automatically switch to HTML
	const handleToolbarAction = useCallback(
		(command: string, arg?: string) => {
			if (format === "plain") {
				// Switch to HTML first, then apply the command
				const html = plainTextToHtml(body);
				setHtmlBody(html);
				setFormat("html");
				requestAnimationFrame(() => {
					Eif (editorRef.current) {
						editorRef.current.innerHTML = html;
						editorRef.current.focus();
						document.execCommand(command, false, arg);
					}
				});
			} else {
				document.execCommand(command, false, arg);
			}
		},
		[format, body],
	);
 
	const modeLabel =
		mode.type === "reply"
			? "Reply"
			: mode.type === "reply-all"
				? "Reply All"
				: mode.type === "forward"
					? "Forward"
					: "New Message";
 
	const containerClass = expanded
		? "w-full h-full sm:w-[calc(100%-3rem)] sm:h-[calc(100%-3rem)] sm:max-w-6xl bg-white dark:bg-gray-900 sm:rounded-xl shadow-2xl flex flex-col"
		: "w-full max-w-2xl bg-white dark:bg-gray-900 rounded-t-xl sm:rounded-xl shadow-2xl flex flex-col max-h-[80vh]";
 
	return (
		<div
			ref={dialogRef}
			className="fixed inset-0 z-50 flex items-end sm:items-center justify-center bg-black/30"
			role="dialog"
			aria-modal="true"
			aria-label={modeLabel}
			onClick={(e) => {
				Eif (e.target === e.currentTarget) handleDiscard();
			}}
			onKeyDown={(e) => {
				if (e.key === "Escape") handleDiscard();
			}}
		>
			<div
				className={containerClass}
				onKeyDown={handleKeyDown}
				onClick={(e) => e.stopPropagation()}
			>
				{/* Header */}
				<div className="flex items-center justify-between px-4 py-3 border-b border-gray-200 dark:border-gray-800">
					<h3 className="font-semibold">{modeLabel}</h3>
					<div className="flex items-center gap-1">
						<button
							type="button"
							onClick={() => setExpanded(!expanded)}
							className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 p-0.5"
						>
							<ExpandIcon expanded={expanded} />
						</button>
						<button
							type="button"
							onClick={handleDiscard}
							className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 p-0.5"
						>
							<XIcon className="w-4 h-4" />
						</button>
					</div>
				</div>
 
				{/* Fields */}
				<div className="px-4 py-2 space-y-2 border-b border-gray-100 dark:border-gray-800">
					{identities && identities.length > 1 && (
						<div className="flex items-center gap-2">
							<label htmlFor="compose-from" className="text-sm text-gray-500 w-14 flex-shrink-0">
								From
							</label>
							<select
								id="compose-from"
								value={fromIdentityId ?? ""}
								onChange={(e) => setFromIdentityId(Number(e.target.value) || undefined)}
								className="flex-1 bg-transparent text-sm outline-none border-none dark:text-gray-100"
							>
								{identities.map((a) => (
									<option key={a.id} value={a.id} className="bg-white dark:bg-gray-900">
										{a.name} &lt;{a.email}&gt;
									</option>
								))}
							</select>
						</div>
					)}
					<div className="flex items-center gap-2">
						<label htmlFor="compose-to" className="text-sm text-gray-500 w-14">
							To
						</label>
						<input
							ref={toInputRef}
							id="compose-to"
							type="text"
							value={to}
							onChange={(e) => {
								setTo(e.target.value);
								if (validationError) setValidationError(null);
							}}
							className="flex-1 bg-transparent text-sm outline-none"
							placeholder="recipient@example.com"
						/>
						<div className="flex items-center gap-1">
							{!showCc && (
								<button
									type="button"
									onClick={() => setShowCc(true)}
									className="text-xs text-gray-400 hover:text-gray-600"
								>
									Cc
								</button>
							)}
							{!showBcc && (
								<button
									type="button"
									onClick={() => setShowBcc(true)}
									className="text-xs text-gray-400 hover:text-gray-600"
								>
									Bcc
								</button>
							)}
						</div>
					</div>
					{showCc && (
						<div className="flex items-center gap-2">
							<label htmlFor="compose-cc" className="text-sm text-gray-500 w-14">
								Cc
							</label>
							<input
								id="compose-cc"
								type="text"
								value={cc}
								onChange={(e) => {
									setCc(e.target.value);
									if (validationError) setValidationError(null);
								}}
								className="flex-1 bg-transparent text-sm outline-none"
								placeholder="cc@example.com"
							/>
						</div>
					)}
					{showBcc && (
						<div className="flex items-center gap-2">
							<label htmlFor="compose-bcc" className="text-sm text-gray-500 w-14">
								Bcc
							</label>
							<input
								id="compose-bcc"
								type="text"
								value={bcc}
								onChange={(e) => {
									setBcc(e.target.value);
									if (validationError) setValidationError(null);
								}}
								className="flex-1 bg-transparent text-sm outline-none"
								placeholder="bcc@example.com"
							/>
						</div>
					)}
					<div className="flex items-center gap-2">
						<label htmlFor="compose-subject" className="text-sm text-gray-500 w-14">
							Subject
						</label>
						<input
							id="compose-subject"
							type="text"
							value={subject}
							onChange={(e) => setSubject(e.target.value)}
							className="flex-1 bg-transparent text-sm outline-none"
						/>
					</div>
				</div>
 
				{/* Formatting toolbar (visible in HTML mode, clickable in plain to auto-escalate) */}
				<div className="flex items-center gap-0.5 px-4 py-1 border-b border-gray-100 dark:border-gray-800">
					<ToolbarActionButton
						label="Bold"
						display="B"
						bold
						command="bold"
						onAction={handleToolbarAction}
					/>
					<ToolbarActionButton
						label="Italic"
						display="I"
						italic
						command="italic"
						onAction={handleToolbarAction}
					/>
					<ToolbarActionButton
						label="Underline"
						display="U"
						underline
						command="underline"
						onAction={handleToolbarAction}
					/>
					<div className="w-px h-4 bg-gray-200 dark:bg-gray-700 mx-1" />
					<ToolbarActionButton
						label="Bulleted list"
						display="&bull;"
						command="insertUnorderedList"
						onAction={handleToolbarAction}
					/>
					<ToolbarActionButton
						label="Numbered list"
						display="1."
						command="insertOrderedList"
						onAction={handleToolbarAction}
					/>
					<div className="w-px h-4 bg-gray-200 dark:bg-gray-700 mx-1" />
					<LinkButton onAction={handleToolbarAction} />
					<div className="flex-1" />
					<button
						type="button"
						onClick={() => requestFormatSwitch(format === "html" ? "plain" : "html")}
						className="text-xs text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 px-1.5 py-0.5 rounded hover:bg-gray-100 dark:hover:bg-gray-800"
						title={format === "html" ? "Switch to plain text" : "Switch to rich text"}
					>
						{format === "html" ? "Plain text" : "Rich text"}
					</button>
				</div>
 
				{/* Body */}
				{format === "plain" ? (
					<textarea
						value={body}
						onChange={(e) => setBody(e.target.value)}
						className={`flex-1 p-4 bg-transparent text-sm resize-none outline-none ${expanded ? "min-h-[400px]" : "min-h-[200px]"}`}
						placeholder="Write your message…"
					/>
				) : (
					<div
						ref={editorRef}
						contentEditable
						tabIndex={0}
						onInput={handleEditorInput}
						className={`flex-1 p-4 bg-transparent text-sm outline-none overflow-y-auto ${expanded ? "min-h-[400px]" : "min-h-[200px]"}`}
						data-placeholder="Write your message…"
						role="textbox"
						aria-label="Message body"
						style={{
							minHeight: expanded ? "400px" : "200px",
						}}
					/>
				)}
 
				{/* Format switch warning */}
				{showFormatWarning && (
					<div className="px-4 py-2 text-xs bg-amber-50 dark:bg-amber-950/30 border-t border-amber-200 dark:border-amber-800 flex items-center justify-between">
						<span className="text-amber-700 dark:text-amber-400">
							Switching to plain text will remove formatting. Continue?
						</span>
						<div className="flex gap-2">
							<button
								type="button"
								onClick={() => {
									setShowFormatWarning(false);
									pendingFormatRef.current = null;
								}}
								className="px-2 py-0.5 text-xs text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-800 rounded"
							>
								Cancel
							</button>
							<button
								type="button"
								onClick={() => {
									Eif (pendingFormatRef.current) {
										switchFormat(pendingFormatRef.current);
									}
								}}
								className="px-2 py-0.5 text-xs text-amber-700 dark:text-amber-400 hover:bg-amber-100 dark:hover:bg-amber-900/50 rounded font-medium"
							>
								Switch
							</button>
						</div>
					</div>
				)}
 
				{/* Validation error */}
				{validationError && (
					<div className="px-4 py-1.5 text-xs text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-950/30 border-t border-red-200 dark:border-red-800">
						{validationError}
					</div>
				)}
 
				{/* Footer */}
				<div className="flex items-center justify-between px-4 py-3 border-t border-gray-200 dark:border-gray-800">
					<div className="text-xs text-gray-400">
						<kbd className="bg-gray-100 dark:bg-gray-800 px-1 rounded">⌘+Enter</kbd> to send
					</div>
					<div className="flex items-center gap-2">
						<button
							type="button"
							onClick={handleDiscard}
							className="px-3 py-1.5 text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-md transition-colors"
						>
							Discard
						</button>
						<button
							type="button"
							onClick={handleSend}
							disabled={!to.trim() || sending}
							className="px-4 py-1.5 text-sm bg-stork-600 hover:bg-stork-700 disabled:opacity-50 text-white rounded-md font-medium transition-colors"
						>
							{sending ? "Sending…" : "Send"}
						</button>
					</div>
				</div>
			</div>
		</div>
	);
}
 
/** Toolbar button that auto-escalates to HTML mode */
function ToolbarActionButton({
	label,
	display,
	command,
	bold,
	italic,
	underline,
	onAction,
}: {
	label: string;
	display: string;
	command: string;
	bold?: boolean;
	italic?: boolean;
	underline?: boolean;
	onAction: (command: string, arg?: string) => void;
}) {
	let className =
		"px-1.5 py-0.5 text-xs rounded hover:bg-gray-200 dark:hover:bg-gray-700 text-gray-600 dark:text-gray-400";
	if (bold) className += " font-bold";
	if (italic) className += " italic";
	if (underline) className += " underline";
 
	return (
		<button
			type="button"
			title={label}
			onMouseDown={(e) => {
				e.preventDefault();
				onAction(command);
			}}
			className={className}
			dangerouslySetInnerHTML={{ __html: display }}
		/>
	);
}
 
/** Link insertion button */
function LinkButton({ onAction }: { onAction: (command: string, arg?: string) => void }) {
	return (
		<button
			type="button"
			title="Insert link"
			onMouseDown={(e) => {
				e.preventDefault();
				const url = prompt("Enter URL:");
				if (url) {
					onAction("createLink", url);
				}
			}}
			className="px-1.5 py-0.5 text-xs rounded hover:bg-gray-200 dark:hover:bg-gray-700 text-gray-600 dark:text-gray-400"
		>
			Link
		</button>
	);
}