Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+62
-117
@@ -1,30 +1,28 @@
|
||||
import { ActionFunctionArgs, LoaderFunctionArgs, useActionData, useLoaderData } from "react-router";
|
||||
// AK-P3 — Notification-Settings route (`/app/:tenant/settings/notifications`).
|
||||
// The category registry + default-inheritance merge live in the pure
|
||||
// notification-settings VM (@hivegrid/view-models); the loader builds the VM
|
||||
// once and the route injects it plus fetcher-backed action callbacks into the
|
||||
// interim <NotificationSettingsPage /> screen (AD-28).
|
||||
|
||||
import { ActionFunctionArgs, LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
||||
import { upsertUserNotificationPreference, getUserNotificationPreferences } from "@hivegrid/db/notifications.server";
|
||||
import {
|
||||
getUserNotificationPreferences,
|
||||
upsertUserNotificationPreference,
|
||||
type NotificationPreferenceChannel,
|
||||
} from "@toolctrl/db/notifications.server";
|
||||
buildNotificationSettingsVM,
|
||||
isAllowedNotificationCategory,
|
||||
isAllowedNotificationChannel,
|
||||
NOTIFICATION_SETTING_CATEGORIES,
|
||||
type NotificationPreferenceInput,
|
||||
type NotificationSettingsVM,
|
||||
} from "@hivegrid/view-models";
|
||||
import { NotificationSettingsPage } from "~/components/screens/notifications/NotificationSettingsPage";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
const NOTIFICATION_SETTING_CATEGORIES = ["default", "billing", "team"] as const;
|
||||
const NOTIFICATION_SETTING_CHANNELS = ["IN_APP", "EMAIL"] as const;
|
||||
|
||||
type NotificationSettingCategory = (typeof NOTIFICATION_SETTING_CATEGORIES)[number];
|
||||
type NotificationSettingChannel = (typeof NOTIFICATION_SETTING_CHANNELS)[number];
|
||||
|
||||
type NotificationPreferenceRecord = {
|
||||
channel: NotificationPreferenceChannel | string;
|
||||
category: string;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
type LoaderData = {
|
||||
preferences: Record<NotificationSettingCategory, Record<NotificationSettingChannel, boolean>>;
|
||||
vm: NotificationSettingsVM;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
@@ -32,14 +30,6 @@ type ActionData = {
|
||||
error?: string;
|
||||
};
|
||||
|
||||
function isAllowedCategory(value: string): value is NotificationSettingCategory {
|
||||
return NOTIFICATION_SETTING_CATEGORIES.includes(value as NotificationSettingCategory);
|
||||
}
|
||||
|
||||
function isAllowedChannel(value: string): value is NotificationSettingChannel {
|
||||
return NOTIFICATION_SETTING_CHANNELS.includes(value as NotificationSettingChannel);
|
||||
}
|
||||
|
||||
function getRequiredFormValue(formData: FormData, key: string): string | null {
|
||||
const value = formData.get(key);
|
||||
|
||||
@@ -51,41 +41,9 @@ function getRequiredFormValue(formData: FormData, key: string): string | null {
|
||||
return normalized.length > 0 ? normalized : null;
|
||||
}
|
||||
|
||||
function createDefaultPreferences(): LoaderData["preferences"] {
|
||||
return {
|
||||
default: { IN_APP: true, EMAIL: true },
|
||||
billing: { IN_APP: true, EMAIL: true },
|
||||
team: { IN_APP: true, EMAIL: true },
|
||||
};
|
||||
}
|
||||
|
||||
function toLoaderData(records: NotificationPreferenceRecord[]): LoaderData {
|
||||
const preferences = createDefaultPreferences();
|
||||
const explicitPreferences = new Set<string>();
|
||||
|
||||
for (const record of records) {
|
||||
const category = record.category.trim().toLowerCase();
|
||||
const channel = record.channel.trim().replace(/-/g, "_").toUpperCase();
|
||||
|
||||
if (isAllowedCategory(category) && isAllowedChannel(channel)) {
|
||||
preferences[category][channel] = record.enabled;
|
||||
explicitPreferences.add(`${category}:${channel}`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const category of NOTIFICATION_SETTING_CATEGORIES) {
|
||||
if (category === "default") {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const channel of NOTIFICATION_SETTING_CHANNELS) {
|
||||
if (!explicitPreferences.has(`${category}:${channel}`)) {
|
||||
preferences[category][channel] = preferences.default[channel];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { preferences };
|
||||
function isCheckboxOn(formData: FormData, key: string): boolean {
|
||||
const value = formData.get(key);
|
||||
return value === "on" || value === "true";
|
||||
}
|
||||
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
@@ -94,9 +52,10 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
const userInfo = await getUserInfo(request);
|
||||
const records = (await getUserNotificationPreferences({ tenantId, userId: userInfo.userId })) as NotificationPreferenceRecord[];
|
||||
const records = (await getUserNotificationPreferences({ tenantId, userId: userInfo.userId })) as NotificationPreferenceInput[];
|
||||
|
||||
return Response.json(toLoaderData(records));
|
||||
const data: LoaderData = { vm: buildNotificationSettingsVM(records) };
|
||||
return Response.json(data);
|
||||
};
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
@@ -108,6 +67,24 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const formData = await request.formData();
|
||||
const actionName = getRequiredFormValue(formData, "action");
|
||||
|
||||
// Master "turn all emails off" switch: write the EMAIL preference for every
|
||||
// known category so explicit per-category overrides cannot leak emails back in.
|
||||
if (actionName === "set-email-master") {
|
||||
const emailsEnabled = !isCheckboxOn(formData, "disableAllEmails");
|
||||
|
||||
for (const category of NOTIFICATION_SETTING_CATEGORIES) {
|
||||
await upsertUserNotificationPreference({
|
||||
tenantId,
|
||||
userId: userInfo.userId,
|
||||
channel: "EMAIL",
|
||||
category,
|
||||
enabled: emailsEnabled,
|
||||
});
|
||||
}
|
||||
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
|
||||
if (actionName !== "update-preference") {
|
||||
return badRequest({ error: t("settings.notifications.errors.invalidPreferenceAction") });
|
||||
}
|
||||
@@ -115,11 +92,11 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const category = getRequiredFormValue(formData, "category")?.toLowerCase() ?? "";
|
||||
const channel = getRequiredFormValue(formData, "channel")?.replace(/-/g, "_").toUpperCase() ?? "";
|
||||
|
||||
if (!isAllowedCategory(category)) {
|
||||
if (!isAllowedNotificationCategory(category)) {
|
||||
return badRequest({ error: t("settings.notifications.errors.invalidPreferenceCategory") });
|
||||
}
|
||||
|
||||
if (!isAllowedChannel(channel)) {
|
||||
if (!isAllowedNotificationChannel(channel)) {
|
||||
return badRequest({ error: t("settings.notifications.errors.invalidPreferenceChannel") });
|
||||
}
|
||||
|
||||
@@ -128,67 +105,35 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
userId: userInfo.userId,
|
||||
channel,
|
||||
category,
|
||||
enabled: formData.get("enabled") === "on" || formData.get("enabled") === "true",
|
||||
enabled: isCheckboxOn(formData, "enabled"),
|
||||
});
|
||||
|
||||
return Response.json({ ok: true });
|
||||
};
|
||||
|
||||
export default function WorkspaceNotificationSettingsRoute() {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const rootData = useRootData();
|
||||
const fetcher = useFetcher<ActionData>();
|
||||
|
||||
const saveState = fetcher.data ? { ok: fetcher.data.ok === true, errorText: fetcher.data.error } : null;
|
||||
|
||||
function submit(form: Record<string, string>) {
|
||||
fetcher.submit({ ...form, ...(rootData.csrf ? { csrf: rootData.csrf } : {}) }, { method: "post" });
|
||||
}
|
||||
|
||||
return (
|
||||
<main data-ui-id="ui.settings.notifications.page" className="mx-auto grid max-w-3xl gap-5 px-4 py-5 sm:px-8">
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-semibold text-foreground">{t("settings.notifications.heading")}</h1>
|
||||
<p className="text-sm text-muted-foreground">{t("settings.notifications.description")}</p>
|
||||
</div>
|
||||
|
||||
{actionData?.error ? <p className="rounded-md border border-destructive/40 bg-destructive/10 p-3 text-sm text-destructive">{actionData.error}</p> : null}
|
||||
{actionData?.ok ? <p className="rounded-md border border-emerald-500/40 bg-emerald-500/10 p-3 text-sm text-emerald-700">{t("settings.notifications.saved")}</p> : null}
|
||||
|
||||
<div className="divide-y rounded-lg border bg-card">
|
||||
{NOTIFICATION_SETTING_CATEGORIES.map((category) => (
|
||||
<section
|
||||
key={category}
|
||||
data-ui-id={`ui.settings.notifications.category.${category}`}
|
||||
className="grid gap-3 p-4 sm:grid-cols-[1fr_auto] sm:items-center"
|
||||
>
|
||||
<div>
|
||||
<h2 className="text-sm font-medium text-foreground">{t(`settings.notifications.categories.${category}`)}</h2>
|
||||
<p className="text-sm text-muted-foreground">{t("settings.notifications.categoryDescription", { category: t(`settings.notifications.categories.${category}`).toLowerCase() })}</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{NOTIFICATION_SETTING_CHANNELS.map((channel) => {
|
||||
const inputId = `notification-${category}-${channel}`;
|
||||
return (
|
||||
<form
|
||||
key={channel}
|
||||
method="post"
|
||||
data-ui-id={`ui.settings.notifications.preference.${category}.${channel.toLowerCase()}`}
|
||||
className="flex items-center gap-2 rounded-md border px-3 py-2"
|
||||
>
|
||||
<input type="hidden" name="action" value="update-preference" />
|
||||
{rootData.csrf && <input type="hidden" name="csrf" value={rootData.csrf} />}
|
||||
<input type="hidden" name="category" value={category} />
|
||||
<input type="hidden" name="channel" value={channel} />
|
||||
<input id={inputId} name="enabled" type="checkbox" defaultChecked={data.preferences[category][channel]} />
|
||||
<label htmlFor={inputId} className="text-sm font-medium">
|
||||
{t(channel === "IN_APP" ? "settings.notifications.channels.inApp" : "settings.notifications.channels.email")}
|
||||
</label>
|
||||
<button type="submit" className="text-sm text-primary underline-offset-2 hover:underline">
|
||||
{t("shared.save")}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</section>
|
||||
))}
|
||||
</div>
|
||||
<main className="relative z-0 flex-1 pb-8">
|
||||
<NotificationSettingsPage
|
||||
vm={data.vm}
|
||||
saveState={saveState}
|
||||
actions={{
|
||||
onTogglePreference: ({ category, channel, enabled }) =>
|
||||
submit({ action: "update-preference", category, channel, enabled: enabled ? "true" : "false" }),
|
||||
onSetEmailMaster: (disableAllEmails) =>
|
||||
submit({ action: "set-email-master", ...(disableAllEmails ? { disableAllEmails: "true" } : {}) }),
|
||||
}}
|
||||
/>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user