feat(mail-ingest): admin card, review mark, inbox events and links, translations

- Admin settings (desktop and phone, self-hosted only): the instance switch,
  how mail arrives (IMAP or a signed webhook) with its fields, the address
  template, the AI allowance per person and day, a connection test and the
  status in counts. Changed fields only are saved; secrets stay masked.
- The review mark of an imported booking takes its mark off: the pill in the
  desktop dialog and a row in the phone's transport sheet ('Looks right'),
  through the ordinary booking update. Its hint no longer speaks of airports.
- The inbox follows mailInbox:changed and mailInbox:settings, and
  ?inbox=mail (the 'bookings waiting' notification) opens it.
- mailIngestRepo: the online-only path the views take, answers parsed
  against the shared contracts.
- Dropped 'read with AI' and the unused mail-review contract: the server
  reads a passing AI failure again on its own.
- mailInbox, mailAdmin and the new reservation, settings and notification
  strings in all 28 locales; the pending notice is a plural group.
- The live flight line on the booking cards is one of the card's fields,
  full width on the phone; the phone's live card keeps its map to the middle.
- Admin flight and mail cards sit in the left column.
This commit is contained in:
Maurice
2026-11-13 18:31:07 +01:00
parent bd11cd347d
commit a7adef5353
209 changed files with 5915 additions and 343 deletions
+28 -3
View File
@@ -1,13 +1,18 @@
// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-004
// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-005
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../api/mailIngest', () => ({
mailIngestApi: { settings: vi.fn(), inbox: vi.fn(), inboxCount: vi.fn(), act: vi.fn() },
}));
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => false) }));
const listeners = vi.hoisted(() => new Set<(event: Record<string, unknown>) => void>());
vi.mock('../api/websocket', () => ({
addListener: (fn: (event: Record<string, unknown>) => void) => listeners.add(fn),
removeListener: (fn: (event: Record<string, unknown>) => void) => listeners.delete(fn),
}));
import { mailIngestApi } from '../api/mailIngest';
import { useMailIngestStore } from './mailIngestStore';
import { useMailIngestStore, watchMailInbox } from './mailIngestStore';
const candidate = (id: number, resolution = 'pending') => ({
id,
@@ -79,8 +84,28 @@ describe('mailIngestStore', () => {
it('FE-STORE-MAILINGEST-004: an entry still open after an action stays, updated', async () => {
useMailIngestStore.setState({ entries: [entry(1)] as never, pending: 1 });
vi.mocked(mailIngestApi.act).mockResolvedValue({ candidate: { ...candidate(1), reason: 'ai_provider_error' } });
await useMailIngestStore.getState().act(1, 'read-ai');
await useMailIngestStore.getState().act(1, 'apply');
expect(useMailIngestStore.getState().entries[0].reason).toBe('ai_provider_error');
expect(useMailIngestStore.getState().pending).toBe(1);
});
it('FE-STORE-MAILINGEST-005: the server inbox events move the count, read the list again and reload the settings', async () => {
vi.mocked(mailIngestApi.inbox).mockResolvedValue({ entries: [entry(3)], pending: 1, next_before: null });
vi.mocked(mailIngestApi.settings).mockResolvedValue({ nope: true });
const stop = watchMailInbox();
const send = (event: Record<string, unknown>) => listeners.forEach((fn) => fn(event));
send({ type: 'mailInbox:changed', pending: 1 });
expect(useMailIngestStore.getState().pending).toBe(1);
await vi.waitFor(() => expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([3]));
send({ type: 'mailInbox:settings' });
expect(mailIngestApi.settings).toHaveBeenCalled();
// A malformed event changes nothing, and after stopping nothing is heard.
send({ type: 'mailInbox:changed', pending: 'many' });
expect(useMailIngestStore.getState().pending).toBe(1);
stop();
expect(listeners.size).toBe(0);
});
});
+38 -18
View File
@@ -1,14 +1,8 @@
import {
mailIngestCandidateResponseSchema,
mailIngestInboxCountResponseSchema,
mailIngestInboxResponseSchema,
mailIngestSettingsResponseSchema,
type MailIngestCandidate,
type MailIngestInboxEntry,
type MailIngestSettingsResponse,
} from '@trek/shared';
import type { MailIngestCandidate, MailIngestInboxEntry, MailIngestSettingsResponse } from '@trek/shared';
import { create } from 'zustand';
import { mailIngestApi, type MailIngestAction } from '../api/mailIngest';
import type { MailIngestAction } from '../api/mailIngest';
import { addListener, removeListener } from '../api/websocket';
import { mailIngestRepo } from '../repo/mailIngestRepo';
import { isEffectivelyOffline } from '../sync/networkMode';
/**
@@ -43,16 +37,23 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => ({
loadSettings: async () => {
if (isEffectivelyOffline()) return;
const parsed = mailIngestSettingsResponseSchema.safeParse(await mailIngestApi.settings().catch(() => null));
if (parsed.success) set({ settings: parsed.data, pending: parsed.data.pending });
try {
const settings = await mailIngestRepo.settings();
set({ settings, pending: settings.pending });
} catch {
// Without an answer the menus keep what they showed; the next sign-in or event reads it again.
}
},
setSettings: (settings) => set({ settings, pending: settings.pending }),
loadCount: async () => {
if (isEffectivelyOffline()) return;
const parsed = mailIngestInboxCountResponseSchema.safeParse(await mailIngestApi.inboxCount().catch(() => null));
if (parsed.success) set({ pending: parsed.data.pending });
try {
set({ pending: await mailIngestRepo.inboxCount() });
} catch {
// The badge keeps its last count until the next event brings a new one.
}
},
loadInbox: async (more = false) => {
@@ -60,9 +61,7 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => ({
set({ loadingInbox: true });
try {
const before = more ? (get().nextBefore ?? undefined) : undefined;
const parsed = mailIngestInboxResponseSchema.safeParse(await mailIngestApi.inbox({ status: 'open', before }));
if (!parsed.success) return;
const { entries, pending, next_before } = parsed.data;
const { entries, pending, next_before } = await mailIngestRepo.inbox(before);
set((state) => ({ entries: more ? [...state.entries, ...entries] : entries, pending, nextBefore: next_before }));
} catch {
// The inbox stays as it was; the next open reads it again.
@@ -72,7 +71,7 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => ({
},
act: async (candidateId, action, body = {}) => {
const { candidate } = mailIngestCandidateResponseSchema.parse(await mailIngestApi.act(candidateId, action, body));
const candidate = await mailIngestRepo.act(candidateId, action, body);
set((state) => {
const stillOpen = OPEN.has(candidate.resolution);
const entries = state.entries.flatMap((entry) =>
@@ -84,3 +83,24 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => ({
return candidate;
},
}));
/**
* Keeps the inbox current while the app is open: the server says when the
* person's open count changed (a mail read, an entry decided on another
* device) and when their import settings did. The count moves at once; a
* list already read is read again, so an open inbox shows the new entry
* without a reload. Mounted once per signed-in shell (Navbar, MMailInboxHost).
*/
export function watchMailInbox(): () => void {
const onEvent = (event: Record<string, unknown>) => {
const store = useMailIngestStore.getState();
if (event?.type === 'mailInbox:changed' && typeof event.pending === 'number') {
useMailIngestStore.setState({ pending: event.pending });
if (store.entries.length > 0 || event.pending > 0) void store.loadInbox();
} else if (event?.type === 'mailInbox:settings') {
void store.loadSettings();
}
};
addListener(onEvent);
return () => removeListener(onEvent);
}