340 lines
14 KiB
TypeScript
340 lines
14 KiB
TypeScript
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<void>;
|
|
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<HTMLInputElement>(null);
|
|
const [mailboxId, setMailboxId] = useState<string>(readyData?.defaultMailboxId ?? "");
|
|
const [to, setTo] = useState<string>(readyData?.prefill.to ?? "");
|
|
const [cc, setCc] = useState("");
|
|
const [bcc, setBcc] = useState("");
|
|
const [subject, setSubject] = useState<string>(readyData?.prefill.subject ?? "");
|
|
const [body, setBody] = useState("");
|
|
const [attachments, setAttachments] = useState<readonly File[]>([]);
|
|
const [touchedRecipients, setTouchedRecipients] = useState(false);
|
|
|
|
if (vm.status === "idle" || vm.status === "loading") {
|
|
return (
|
|
<div className="flex w-full flex-col items-start gap-6 px-6 py-6" data-ui-id="ui.email.compose">
|
|
<Card data-ui-id="ui.email.compose.loading">
|
|
<div className="flex w-full items-center gap-3">
|
|
<Loader />
|
|
<div className="flex flex-col items-start">
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
|
|
{t("emailInbox.compose.loading.headline")}
|
|
</span>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
|
|
{t("emailInbox.compose.loading.body")}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (vm.status === "error" || !readyData) {
|
|
return (
|
|
<div className="flex w-full flex-col items-start gap-6 px-6 py-6" data-ui-id="ui.email.compose">
|
|
<EmptyState
|
|
outlined
|
|
variant="error"
|
|
title={t("emailInbox.compose.error.headline")}
|
|
body={vm.status === "error" ? t(vm.error.messageKey) : t("emailInbox.compose.error.body")}
|
|
data-ui-id="ui.email.compose.error"
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<div className="bg-surface-neutral-primary flex w-full flex-col items-start" data-ui-id="ui.email.compose">
|
|
<div className="border-border-default bg-surface-neutral-secondary mobile:flex-col mobile:px-4 flex w-full items-start justify-between gap-4 border-b border-solid px-6 py-6">
|
|
<div className="flex flex-col items-start gap-1">
|
|
<Button variant="tertiary" size="small" icon={<FeatherArrowLeft />} onClick={actions.onBack} data-ui-id="ui.email.compose.back">
|
|
{t("emailInbox.compose.back")}
|
|
</Button>
|
|
<h1 className="text-heading- font-heading- text-text-neutral-primary">{t("emailInbox.compose.title")}</h1>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="mobile:px-4 flex w-full flex-col items-start gap-4 px-6 py-6">
|
|
{mailboxes.length === 0 ? (
|
|
<EmptyState
|
|
outlined
|
|
title={t(noMailboxKey)}
|
|
body={t("emailInbox.list.noMailbox.body")}
|
|
action={
|
|
<Button variant="tertiary" icon={<FeatherSettings />} onClick={actions.onOpenSettings} data-ui-id="ui.email.compose.connect">
|
|
{t("emailInbox.list.connectCta")}
|
|
</Button>
|
|
}
|
|
data-ui-id="ui.email.compose.noMailbox"
|
|
/>
|
|
) : (
|
|
<>
|
|
{actionErrorKey ? (
|
|
<Alert variant="error" notDismissible title={t(actionErrorKey)} className="w-full" data-ui-id="ui.email.compose.error" />
|
|
) : null}
|
|
|
|
<Card className="w-full">
|
|
<div className="flex w-full max-w-[768px] flex-col items-start gap-4">
|
|
<Select
|
|
className="w-full"
|
|
label={t("emailInbox.compose.mailbox")}
|
|
value={mailboxId}
|
|
onValueChange={setMailboxId}
|
|
data-ui-id="ui.email.compose.mailbox"
|
|
>
|
|
{mailboxes.map((mailbox) => (
|
|
<Select.Item key={mailbox.id} value={mailbox.id}>
|
|
{mailbox.emailAddress}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
|
|
<TextInput
|
|
className="w-full"
|
|
label={t("emailInbox.compose.to")}
|
|
error={touchedRecipients && recipientsInvalid}
|
|
message={touchedRecipients && recipientsInvalid ? t("emailInbox.compose.error.no_recipients") : undefined}
|
|
>
|
|
<TextInput.Input
|
|
value={to}
|
|
placeholder={t("emailInbox.compose.recipientsPlaceholder")}
|
|
onChange={(event) => setTo(event.target.value)}
|
|
onBlur={() => setTouchedRecipients(true)}
|
|
data-ui-id="ui.email.compose.to"
|
|
/>
|
|
</TextInput>
|
|
|
|
<TextInput className="w-full" label={t("emailInbox.compose.cc")}>
|
|
<TextInput.Input
|
|
value={cc}
|
|
placeholder={t("emailInbox.compose.recipientsPlaceholder")}
|
|
onChange={(event) => setCc(event.target.value)}
|
|
data-ui-id="ui.email.compose.cc"
|
|
/>
|
|
</TextInput>
|
|
|
|
<TextInput className="w-full" label={t("emailInbox.compose.bcc")}>
|
|
<TextInput.Input
|
|
value={bcc}
|
|
placeholder={t("emailInbox.compose.recipientsPlaceholder")}
|
|
onChange={(event) => setBcc(event.target.value)}
|
|
data-ui-id="ui.email.compose.bcc"
|
|
/>
|
|
</TextInput>
|
|
|
|
{templates.length > 0 ? (
|
|
<Select
|
|
className="w-full"
|
|
label={t("emailInbox.compose.template")}
|
|
placeholder={t("emailInbox.compose.noTemplate")}
|
|
value={NO_TEMPLATE}
|
|
onValueChange={(value) => {
|
|
const template = templates.find((candidate) => candidate.id === value);
|
|
if (!template) return;
|
|
setSubject(template.subject);
|
|
setBody(templateBodyAsText(template.htmlBody));
|
|
}}
|
|
data-ui-id="ui.email.compose.template"
|
|
>
|
|
<Select.Item value={NO_TEMPLATE}>{t("emailInbox.compose.noTemplate")}</Select.Item>
|
|
{templates.map((template) => (
|
|
<Select.Item key={template.id} value={template.id}>
|
|
{template.name}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
) : null}
|
|
|
|
<TextInput className="w-full" label={t("emailInbox.compose.subject")}>
|
|
<TextInput.Input value={subject} onChange={(event) => setSubject(event.target.value)} data-ui-id="ui.email.compose.subject" />
|
|
</TextInput>
|
|
|
|
<TextArea className="w-full" label={t("emailInbox.compose.body")}>
|
|
<TextArea.Input
|
|
className="h-auto min-h-[192px] w-full"
|
|
value={body}
|
|
onChange={(event) => setBody(event.target.value)}
|
|
data-ui-id="ui.email.compose.body"
|
|
/>
|
|
</TextArea>
|
|
|
|
{prefill.quotedHtml ? (
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
|
|
{t("emailInbox.compose.quotedHint")}
|
|
</span>
|
|
) : null}
|
|
|
|
<div className="flex w-full flex-col items-start gap-2">
|
|
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-primary">
|
|
{t("emailInbox.compose.attachments")}
|
|
</span>
|
|
{/* 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. */}
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
multiple
|
|
className="hidden"
|
|
onChange={(event) => addFiles(event.target.files)}
|
|
data-ui-id="ui.email.compose.attachments"
|
|
/>
|
|
<Button
|
|
variant="secondary"
|
|
icon={<FeatherPaperclip />}
|
|
onClick={() => fileInputRef.current?.click()}
|
|
data-ui-id="ui.email.compose.attachments.add"
|
|
>
|
|
{t("emailInbox.compose.attachment.add")}
|
|
</Button>
|
|
{attachments.map((file, index) => {
|
|
const tooLarge = file.size > COMPOSE_ATTACHMENT_MAX_BYTES;
|
|
return (
|
|
<div
|
|
key={`${file.name}:${String(index)}`}
|
|
className="border-border-default flex w-full items-center gap-3 rounded-md border border-solid px-3 py-2"
|
|
data-attachment-name={file.name}
|
|
data-attachment-oversized={tooLarge}
|
|
>
|
|
<FeatherFile className="text-body-large font-body-large text-icon-neutral-secondary flex-none" />
|
|
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-primary min-w-0 grow shrink-0 basis-0 truncate">
|
|
{file.name}
|
|
</span>
|
|
<span
|
|
className={
|
|
tooLarge
|
|
? "text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative flex-none whitespace-nowrap"
|
|
: "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary flex-none whitespace-nowrap"
|
|
}
|
|
>
|
|
{tooLarge ? t("emailInbox.compose.error.attachment_too_large") : attachmentSizeLabel(file.size)}
|
|
</span>
|
|
<IconButton
|
|
size="small"
|
|
icon={<FeatherX />}
|
|
onClick={() => removeFile(index)}
|
|
aria-label={t("emailInbox.compose.attachment.remove")}
|
|
data-ui-id="ui.email.compose.attachment.remove"
|
|
/>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
<div className="flex w-full items-center justify-end gap-2 pt-2">
|
|
<Button
|
|
variant="primary"
|
|
icon={<FeatherSend />}
|
|
loading={submitting === true}
|
|
disabled={!canSend}
|
|
onClick={send}
|
|
data-ui-id="ui.email.compose.send"
|
|
>
|
|
{t("emailInbox.compose.send")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Card>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|