import type { MailIngestInboxEntry } from '@trek/shared'; import { Copy, FilePlus2, FileText, Inbox, Loader2, Mail, PencilLine, Plus, Trash2 } from 'lucide-react'; import { useId, type ReactElement } from 'react'; import { useTranslation } from '../../i18n'; import { copyText } from '../../utils/clipboard'; import { formatDate } from '../../utils/formatters'; import { RES_ICONS } from '../Planner/DayPlanSidebar.constants'; import { DialogButton, DialogHeader, DialogIconButton, DialogShell, DialogTile, NEUTRAL_TINT, } from '../shared/DialogShell'; import { useToast } from '../shared/Toast'; import { entryDates, entryTitle, reasonText, type InboxAction } from './mailInboxModel'; import { useMailInbox, type InboxTrip } from './useMailInbox'; /** The icon and the label of each way out, so a row and the trip picker word them alike. */ function actionLook(action: InboxAction, t: (key: string) => string): { icon: ReactElement; label: string } { switch (action) { case 'add': return { icon: , label: t('mailInbox.action.add') }; case 'choose': return { icon: , label: t('mailInbox.action.choose') }; case 'file': return { icon: , label: t('mailInbox.action.file') }; case 'manual': return { icon: , label: t('mailInbox.action.manual') }; case 'discard': return { icon: , label: t('mailInbox.action.discard') }; } } function Entry({ entry, inbox }: { entry: MailIngestInboxEntry; inbox: ReturnType }) { const { t, locale } = useTranslation(); const toast = useToast(); const title = entryTitle(entry) ?? t('mailInbox.untitled'); const dates = entryDates(entry); const reason = entry.reason ?? entry.message.reason; const Icon = entry.item ? RES_ICONS[entry.item.type as keyof typeof RES_ICONS] || Mail : Mail; const [primary, ...rest] = inbox.actionsOf(entry); const busy = inbox.busy === entry.id; const code = entry.message.forwarding_code; return (
  • {title} {dates && ( {formatDate(dates.start, locale)} {dates.end ? ` → ${formatDate(dates.end, locale)}` : ''} )}
    {reason &&
    {reasonText(reason, t)}
    } {code && (
    {code} } onClick={() => { void copyText(code).then((ok) => ok && toast.success(t('common.copied'))); }} />
    )}
    {entry.suggested_trip && ( {entry.suggested_trip.title} )}
    {busy && } {rest.map((action) => { const look = actionLook(action, t); return ( inbox.perform(entry, action)} /> ); })} {primary && primary !== 'discard' && ( inbox.perform(entry, primary)} > {actionLook(primary, t).label} )} {primary === 'discard' && ( inbox.perform(entry, 'discard')} /> )}
  • ); } function TripPicker({ trips, onPick, onCancel, }: { trips: InboxTrip[]; onPick: (id: number) => void; onCancel: () => void; }) { const { t, locale } = useTranslation(); return (
    {t('mailInbox.pickTrip')}
      {trips.map((trip) => (
    • ))}
    {t('common.cancel')}
    ); } /** * Bookings from forwarded mails that wait for the person: one row each with * what TREK read, why it waits, the trip it suggests, and the ways out (add, * choose a trip, keep the mail as a document, fill in the form, read again with * AI, discard). Mounted once by the layout; opened from the account menu, the * settings card or a notification. */ export default function MailInboxDialog(): ReactElement | null { const { t } = useTranslation(); const inbox = useMailInbox(); const titleId = useId(); if (!inbox.open) return null; const header = ( } tint={NEUTRAL_TINT} labelId={titleId} onClose={inbox.close} title={t('mailInbox.title')} // The empty state below says it already; the head only counts what waits. sub={inbox.pending > 0 ? t('mailInbox.pendingCount', { count: inbox.pending }) : undefined} /> ); return ( {inbox.picking ? ( ) : inbox.preview ? (
    {inbox.preview.subject ?? t('mailInbox.untitled')}
                {inbox.preview.text}
              
    {t('mailInbox.backToList')}
    ) : inbox.entries.length === 0 ? (
    {t('mailInbox.empty')}
    {t('mailInbox.emptyHint')}
    ) : (
      {inbox.entries.map((entry) => ( ))} {inbox.hasMore && (
    • {t('mailInbox.loadMore')}
    • )}
    )}
    ); }