import { useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherArrowLeft, FeatherFile, FeatherPaperclip, FeatherSend, FeatherSettings, FeatherX } from "@subframe/core"; import { parseRecipients, type EmailComposeVM } from "@hivegrid/view-models"; import { Alert } from "~/ui/components/Alert"; import { Button } from "~/ui/components/Button"; import { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { IconButton } from "~/ui/components/IconButton"; import { Loader } from "~/ui/components/Loader"; import { Select } from "~/ui/components/Select"; import { TextArea } from "~/ui/components/TextArea"; import { TextInput } from "~/ui/components/TextInput"; /** AK-UI-03 interim (AD-28): compose form composed from ~/ui until David's final delta lands. */ /** Mirrors the server cap (emails.new.tsx) so the user sees the problem before sending. */ export const COMPOSE_ATTACHMENT_MAX_BYTES = 10 * 1024 * 1024; /** Sentinel for the "no template" option — Radix Select items must not use "". */ const NO_TEMPLATE = "none"; export type EmailComposePayload = { readonly mailboxId: string; readonly to: string; readonly cc: string; readonly bcc: string; readonly subject: string; readonly body: string; readonly replyToMessageId: string | null; readonly quotedHtml: string; readonly attachments: readonly File[]; }; export type EmailComposeActions = { readonly onSend: (payload: EmailComposePayload) => void | Promise; readonly onBack: () => void; readonly onOpenSettings: () => void; }; function attachmentSizeLabel(sizeBytes: number): string { return `${Math.max(1, Math.round(sizeBytes / 1024))} KB`; } /** Templates carry html; the compose body is plain text (the action escapes it). */ function templateBodyAsText(htmlBody: string): string { return htmlBody .replace(/<[^>]+>/gu, " ") .replace(/\s+/gu, " ") .trim(); } export function EmailComposePage({ vm, actions, actionErrorKey, submitting, }: { vm: EmailComposeVM; actions: EmailComposeActions; actionErrorKey?: string; submitting?: boolean; }): React.JSX.Element { const { t } = useTranslation(); const readyData = vm.status === "ready" ? vm.data : undefined; const fileInputRef = useRef(null); const [mailboxId, setMailboxId] = useState(readyData?.defaultMailboxId ?? ""); const [to, setTo] = useState(readyData?.prefill.to ?? ""); const [cc, setCc] = useState(""); const [bcc, setBcc] = useState(""); const [subject, setSubject] = useState(readyData?.prefill.subject ?? ""); const [body, setBody] = useState(""); const [attachments, setAttachments] = useState([]); const [touchedRecipients, setTouchedRecipients] = useState(false); if (vm.status === "idle" || vm.status === "loading") { return (
{t("emailInbox.compose.loading.headline")} {t("emailInbox.compose.loading.body")}
); } if (vm.status === "error" || !readyData) { return (
); } const { mailboxes, templates, prefill, noMailboxKey } = readyData; const oversizedAttachment = attachments.some((file) => file.size > COMPOSE_ATTACHMENT_MAX_BYTES); const recipientCount = parseRecipients(to).length; const recipientsInvalid = recipientCount === 0; const canSend = !recipientsInvalid && !oversizedAttachment && mailboxId !== "" && submitting !== true; function addFiles(selected: FileList | null): void { if (!selected) return; const picked = Array.from(selected); setAttachments((current) => [...current, ...picked]); if (fileInputRef.current) fileInputRef.current.value = ""; } function removeFile(index: number): void { setAttachments((current) => current.filter((_, position) => position !== index)); } function send(): void { setTouchedRecipients(true); if (!canSend) return; void actions.onSend({ mailboxId, to, cc, bcc, subject, body, replyToMessageId: prefill.replyToMessageId, quotedHtml: prefill.quotedHtml, attachments, }); } return (

{t("emailInbox.compose.title")}

{mailboxes.length === 0 ? ( } onClick={actions.onOpenSettings} data-ui-id="ui.email.compose.connect"> {t("emailInbox.list.connectCta")} } data-ui-id="ui.email.compose.noMailbox" /> ) : ( <> {actionErrorKey ? ( ) : null}
setTo(event.target.value)} onBlur={() => setTouchedRecipients(true)} data-ui-id="ui.email.compose.to" /> setCc(event.target.value)} data-ui-id="ui.email.compose.cc" /> setBcc(event.target.value)} data-ui-id="ui.email.compose.bcc" /> {templates.length > 0 ? ( ) : null} setSubject(event.target.value)} data-ui-id="ui.email.compose.subject" /> {prefill.quotedHtml ? ( {t("emailInbox.compose.quotedHint")} ) : null}
{t("emailInbox.compose.attachments")} {/* The ~/ui FileUpload component is display-only (no input, hardcoded copy), * so the real picker stays a hidden native input driven by a ~/ui Button. */} addFiles(event.target.files)} data-ui-id="ui.email.compose.attachments" /> {attachments.map((file, index) => { const tooLarge = file.size > COMPOSE_ATTACHMENT_MAX_BYTES; return (
{file.name} {tooLarge ? t("emailInbox.compose.error.attachment_too_large") : attachmentSizeLabel(file.size)} } onClick={() => removeFile(index)} aria-label={t("emailInbox.compose.attachment.remove")} data-ui-id="ui.email.compose.attachment.remove" />
); })}
)}
); }