mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
a7adef5353
- 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.
125 lines
3.7 KiB
TypeScript
125 lines
3.7 KiB
TypeScript
import type { MailIngestAdminStatusResponse, MailIngestAdminTestResponse } from '@trek/shared';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { useTranslation } from '../../../i18n';
|
|
import { mailIngestRepo } from '../../../repo/mailIngestRepo';
|
|
import { getApiErrorMessage } from '../../../types';
|
|
import { useToast } from '../../shared/Toast';
|
|
import {
|
|
ENABLED_KEY,
|
|
changesOf,
|
|
enabledOf,
|
|
formOf,
|
|
problemsOf,
|
|
type MailAdminField,
|
|
type MailAdminForm,
|
|
} from './mailIngestAdminModel';
|
|
|
|
/**
|
|
* The admin card of the booking mail import, desktop and phone alike: the
|
|
* instance switch (saved at once, as every switch in the admin settings), the
|
|
* transport fields (saved together, only the changed ones), the connection
|
|
* test and the status in counts. Loads once when shown.
|
|
*/
|
|
export function useMailIngestAdmin(active: boolean) {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const [enabled, setEnabled] = useState(false);
|
|
const [saved, setSaved] = useState<MailAdminForm | null>(null);
|
|
const [form, setForm] = useState<MailAdminForm | null>(null);
|
|
const [status, setStatus] = useState<MailIngestAdminStatusResponse | null>(null);
|
|
const [test, setTest] = useState<MailIngestAdminTestResponse | null>(null);
|
|
const [saving, setSaving] = useState(false);
|
|
const [testing, setTesting] = useState(false);
|
|
|
|
const loadStatus = useCallback(async () => {
|
|
try {
|
|
setStatus(await mailIngestRepo.adminStatus());
|
|
} catch {
|
|
setStatus(null);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!active) return;
|
|
let cancelled = false;
|
|
void (async () => {
|
|
try {
|
|
const raw = await mailIngestRepo.adminSettings();
|
|
if (cancelled) return;
|
|
const loaded = formOf(raw);
|
|
setEnabled(enabledOf(raw));
|
|
setSaved(loaded);
|
|
setForm(loaded);
|
|
} catch {
|
|
if (!cancelled) toast.error(t('mailAdmin.loadError'));
|
|
}
|
|
if (!cancelled) await loadStatus();
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [active, loadStatus, t, toast]);
|
|
|
|
const toggle = async (on: boolean) => {
|
|
setEnabled(on);
|
|
try {
|
|
await mailIngestRepo.saveAdminSettings({ [ENABLED_KEY]: on });
|
|
void loadStatus();
|
|
} catch (err: unknown) {
|
|
setEnabled(!on);
|
|
toast.error(getApiErrorMessage(err, t('common.error')));
|
|
}
|
|
};
|
|
|
|
const set = <K extends MailAdminField>(field: K, value: MailAdminForm[K]) =>
|
|
setForm((current) => (current ? { ...current, [field]: value } : current));
|
|
|
|
const patch = form && saved ? changesOf(form, saved) : {};
|
|
const problems = form ? problemsOf(form) : [];
|
|
|
|
const save = async () => {
|
|
if (!form || Object.keys(patch).length === 0 || problems.length > 0) return;
|
|
setSaving(true);
|
|
try {
|
|
await mailIngestRepo.saveAdminSettings(patch);
|
|
setSaved(form);
|
|
setTest(null);
|
|
toast.success(t('common.saved'));
|
|
void loadStatus();
|
|
} catch (err: unknown) {
|
|
toast.error(getApiErrorMessage(err, t('common.error')));
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
const runTest = async () => {
|
|
setTesting(true);
|
|
try {
|
|
setTest(await mailIngestRepo.adminTest());
|
|
} catch (err: unknown) {
|
|
setTest({ transport: form?.transport ?? 'off', ok: false, error: getApiErrorMessage(err, t('common.error')) });
|
|
} finally {
|
|
setTesting(false);
|
|
}
|
|
};
|
|
|
|
return {
|
|
loaded: form !== null,
|
|
enabled,
|
|
toggle: (on: boolean) => void toggle(on),
|
|
form,
|
|
set,
|
|
dirty: Object.keys(patch).length > 0,
|
|
problems,
|
|
saving,
|
|
save: () => void save(),
|
|
status,
|
|
test,
|
|
testing,
|
|
runTest: () => void runTest(),
|
|
};
|
|
}
|
|
|
|
export type MailIngestAdmin = ReturnType<typeof useMailIngestAdmin>;
|