Files

163 lines
7.9 KiB
TypeScript

// AK-P3 Interim-UI (AD-28) — Notification-Settings preview screen composed from
// ~/ui (A6 settings archetype). Pure presentational component: renders the
// server-built NotificationSettingsVM passed as a prop; toggles run through
// injected action callbacks (M014) implemented fetcher-side by the hosting
// route. Final Subframe optics land as a tracked design-nachzug once David's
// D-P3 ships.
import { useTranslation } from "react-i18next";
import { FeatherBell, FeatherLock } from "@subframe/core";
import type { NotificationSettingChannel, NotificationSettingsVM } from "@hivegrid/view-models";
import { Alert } from "~/ui/components/Alert";
import { Card } from "~/ui/components/Card";
import { Switch } from "~/ui/components/Switch";
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageIntro, PageShell } from "../shared/PageShell";
export type NotificationSettingsActions = {
onTogglePreference: (input: { category: string; channel: NotificationSettingChannel; enabled: boolean }) => void;
/** Master switch: true = turn ALL emails off (writes every category). */
onSetEmailMaster: (disableAllEmails: boolean) => void;
};
export type NotificationSettingsPageProps = {
vm: NotificationSettingsVM;
/** Plain-text result of the last save action (null = nothing saved yet). */
saveState: { ok: boolean; errorText?: string } | null;
actions: NotificationSettingsActions;
/** Inside the Mein-Konto hub (AK-SET-01) the hub owns the header. */
embedded?: boolean;
};
export function NotificationSettingsPage({ vm, saveState, actions, embedded = false }: NotificationSettingsPageProps) {
const { t } = useTranslation();
// Loading / not-ready: keep the marker so the shell + e2e see a stable surface.
if (vm.status !== "ready" || !vm.data) {
return (
<PageShell uiId="ui.settings.notifications.page">
{embedded ? null : (
<PageHeader
icon={<FeatherBell className={PAGE_HEADER_ICON_CLASS} />}
title={t("settings.notifications.heading")}
subtitle={t("settings.notifications.description")}
/>
)}
<PageBody>{null}</PageBody>
</PageShell>
);
}
const { groups, allEmailsDisabled } = vm.data;
return (
<PageShell uiId="ui.settings.notifications.page">
{embedded ? null : (
<PageHeader
icon={<FeatherBell className={PAGE_HEADER_ICON_CLASS} />}
title={t("settings.notifications.heading")}
subtitle={t("settings.notifications.description")}
/>
)}
<PageBody>
<div className="flex w-full justify-center">
<div className="flex w-full max-w-5xl flex-col gap-6">
{embedded ? <PageIntro uiId="ui.settings.notifications.intro">{t("settings.notifications.description")}</PageIntro> : null}
{saveState ? (
<div data-ui-id="ui.settings.notifications.saveState">
{saveState.ok ? (
<Alert variant="success" title={t("settings.notifications.saved")} />
) : (
<Alert variant="error" title={saveState.errorText ?? t("settings.notifications.errors.invalidPreferenceAction")} />
)}
</div>
) : null}
{/* Master switch: all emails off (card surface, 16px padding) */}
<Card className="flex-row flex-wrap items-center justify-between" data-ui-id="ui.settings.notifications.master.email">
<div className="flex flex-col gap-0">
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
{t("settings.notifications.master.heading")}
</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("settings.notifications.master.description")}
</span>
</div>
<label className="flex items-center gap-2">
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{t("settings.notifications.master.disableAllEmails")}
</span>
<Switch
checked={allEmailsDisabled}
onCheckedChange={(checked: boolean) => actions.onSetEmailMaster(checked)}
aria-label={t("settings.notifications.master.disableAllEmails")}
data-ui-id="ui.settings.notifications.master.switch"
/>
</label>
</Card>
{/* Event types grouped per module (A6) */}
<div className="flex w-full flex-col gap-4">
{groups.map((group) => (
<Card key={group.module} className="gap-0 p-0" data-ui-id={`ui.settings.notifications.module.${group.module}`}>
<span className="border-border-default text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary w-full border-b border-solid px-4 py-2 uppercase">
{t(group.moduleLabelKey)}
</span>
{group.categories.map((category) => (
<div
key={category.category}
className="flex w-full flex-wrap items-center justify-between gap-4 p-4"
data-ui-id={`ui.settings.notifications.category.${category.category}`}
>
<div className="flex flex-col gap-0">
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t(category.labelKey)}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{category.locked
? t("settings.notifications.lockedDescription")
: t("settings.notifications.categoryDescription", { category: t(category.labelKey) })}
</span>
</div>
{category.locked ? (
<span
className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary flex items-center gap-1"
data-ui-id={`ui.settings.notifications.locked.${category.category}`}
>
<FeatherLock className="text-body-small-regular-0 font-body-small-regular-0 text-icon-neutral-tertiary" />
{t("settings.notifications.alwaysOn")}
</span>
) : (
<div className="flex flex-wrap items-center gap-4">
<ChannelToggle
label={t("settings.notifications.channels.inApp")}
checked={category.channels.IN_APP}
uiId={`ui.settings.notifications.preference.${category.category}.in_app`}
onChange={(enabled) => actions.onTogglePreference({ category: category.category, channel: "IN_APP", enabled })}
/>
<ChannelToggle
label={t("settings.notifications.channels.email")}
checked={category.channels.EMAIL}
uiId={`ui.settings.notifications.preference.${category.category}.email`}
onChange={(enabled) => actions.onTogglePreference({ category: category.category, channel: "EMAIL", enabled })}
/>
</div>
)}
</div>
))}
</Card>
))}
</div>
</div>
</div>
</PageBody>
</PageShell>
);
}
function ChannelToggle({ label, checked, uiId, onChange }: { label: string; checked: boolean; uiId: string; onChange: (enabled: boolean) => void }) {
return (
<label className="flex items-center gap-2" data-ui-id={uiId}>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{label}</span>
<Switch checked={checked} onCheckedChange={onChange} aria-label={label} />
</label>
);
}