import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router"; import { FeatherArrowLeft, FeatherCornerUpLeft, FeatherCornerUpRight, FeatherDownload, FeatherFile, FeatherImage, FeatherLink, FeatherMail, FeatherMailOpen, FeatherUnlink, } from "@subframe/core"; import type { EmailAttachmentVM, EmailMessageDetailVM, EmailThreadMessageVM } from "@hivegrid/view-models"; import { useLocaleFormatters, type LocaleFormatters } from "~/locale/format"; import { Accordion } from "~/ui/components/Accordion"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Card } from "~/ui/components/Card"; import { Dialog } from "~/ui/components/Dialog"; import { EmptyState } from "~/ui/components/EmptyState"; import { Loader } from "~/ui/components/Loader"; import { TextInput } from "~/ui/components/TextInput"; /** AK-UI-02 interim (AD-28): message detail composed from ~/ui until David's final delta lands. */ export type EmailMessageDetailLinkTargets = { readonly kontaktId: string | null; readonly leadId: string | null; readonly projektId: string | null; }; export type EmailMessageDetailActions = { readonly onSetLink: (targets: EmailMessageDetailLinkTargets) => void | Promise; readonly onClearLink: () => void | Promise; readonly onImportAttachment: (attachmentId: string) => void | Promise; readonly onToggleRead: () => void | Promise; readonly onLoadImages: () => void; readonly onReply: () => void; readonly onForward: () => void; readonly onBack: () => void; }; /** Formatters pin the fixed app time zone — a raw toLocaleString would drift * between server and client render (React #418). */ function sentAtLabel(iso: string, formatters: LocaleFormatters): string { return formatters.dateTime(new Date(iso)); } /** Same rounding the previous inline route markup used. */ function attachmentSizeLabel(sizeBytes: number): string { return `${Math.max(1, Math.round(sizeBytes / 1024))} KB`; } function ThreadEntry({ entry, allowExternalImages, onLoadImages, formatters, }: { entry: EmailThreadMessageVM; allowExternalImages: boolean; onLoadImages: () => void; formatters: LocaleFormatters; }): React.JSX.Element { const { t } = useTranslation(); return (
{entry.fromName ?? entry.fromAddress} {t("emailInbox.detail.to")} {entry.toSummary}
{sentAtLabel(entry.sentAt, formatters)}
} >
{entry.blockedImageCount > 0 && !allowExternalImages ? ( } onClick={onLoadImages}> {t("emailInbox.detail.loadImages")} } className="w-full" data-ui-id="ui.email.detail.blockedImages" /> ) : null} {/* Sanitized server-side (dompurify) — safe to inject. */}
); } export function EmailMessageDetailPage({ vm, actions, actionErrorKey, justSent, }: { vm: EmailMessageDetailVM; actions: EmailMessageDetailActions; actionErrorKey?: string; justSent?: boolean; }): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); const readyData = vm.status === "ready" ? vm.data : undefined; const [linkDialogOpen, setLinkDialogOpen] = useState(false); const [clearLinkDialogOpen, setClearLinkDialogOpen] = useState(false); const [importTarget, setImportTarget] = useState(null); const [busy, setBusy] = useState(false); const [linkDraft, setLinkDraft] = useState<{ kontaktId: string; leadId: string; projektId: string }>({ kontaktId: "", leadId: "", projektId: "", }); if (vm.status === "idle" || vm.status === "loading") { return (
{t("emailInbox.detail.loading.headline")} {t("emailInbox.detail.loading.body")}
); } if (vm.status === "error" || !readyData) { return (
); } const { subject, mailbox, thread, attachments, match, canWrite, allowExternalImages } = readyData; const focused = thread.find((entry) => entry.isFocused) ?? thread[0]; const focusedIsRead = focused?.isRead ?? false; async function runBusy(action: () => void | Promise, close: () => void): Promise { setBusy(true); try { await action(); close(); } finally { setBusy(false); } } return (

{subject ?? t("emailInbox.list.noSubject")}

{mailbox.emailAddress}
{canWrite ? (
) : null}
{justSent ? ( ) : null} {actionErrorKey ? ( ) : null}
{t(match.sourceKey)} {match.links.map((link) => ( {t(`emailInbox.list.linkBadge.${link.type}`)} ))} {match.links.length === 0 ? ( {t("emailInbox.detail.match.noneHint")} ) : null}
{canWrite ? (
) : null}
{thread.map((entry) => ( ))}
{attachments.length > 0 ? (
{t("emailInbox.detail.attachments")}
{attachments.map((attachment) => (
{attachment.filename} {attachmentSizeLabel(attachment.sizeBytes)} {attachment.downloadHref ? ( ) : canWrite ? ( ) : null}
))}
) : null}
(open ? undefined : setLinkDialogOpen(false))}>
{t("emailInbox.detail.linkDialog.title")} {t("emailInbox.detail.linkDialog.body")}
setLinkDraft((current) => ({ ...current, kontaktId: event.target.value }))} data-ui-id="ui.email.detail.linkDialog.kontaktId" /> setLinkDraft((current) => ({ ...current, leadId: event.target.value }))} data-ui-id="ui.email.detail.linkDialog.leadId" /> setLinkDraft((current) => ({ ...current, projektId: event.target.value }))} data-ui-id="ui.email.detail.linkDialog.projektId" />
(open ? undefined : setClearLinkDialogOpen(false))}>
{t("emailInbox.detail.clearLinkDialog.title")} {t("emailInbox.detail.clearLinkDialog.body")}
(open ? undefined : setImportTarget(null))}>
{t("emailInbox.detail.importDialog.title")} {t("emailInbox.detail.importDialog.body", { filename: importTarget?.filename ?? "", size: importTarget ? attachmentSizeLabel(importTarget.sizeBytes) : "", })}
); }