import { ActionFunction, LoaderFunctionArgs } from "react-router"; import { useActionData, useLoaderData, useSubmit } from "react-router"; import { useEffect, useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import InfoBanner from "~/components/ui/banners/InfoBanner"; import ButtonSecondary from "~/components/ui/buttons/ButtonSecondary"; import ServerError from "~/components/ui/errors/ServerError"; import NotificationTemplatesTable from "~/modules/notifications/components/NotificationTemplatesTable"; import { NotificationChannelDto, NotificationChannels } from "~/modules/notifications/services/NotificationChannels"; import NotificationService, { IGetTemplatesData } from "~/modules/notifications/services/.server/NotificationService"; import { redactNotificationDiagnosticText } from "@toolctrl/db/notifications.server"; import { getUser } from "~/utils/db/users.db.server"; import { getPaginationFromCurrentUrl } from "~/utils/helpers/RowPaginationHelper"; import { getUserInfo, validateCSRFToken } from "~/utils/session.server"; import toast from "react-hot-toast"; import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService"; import { appendCSRFToken } from "~/utils/csrf"; import { getTranslations } from "~/locale/i18next.server"; type LoaderData = { items: IGetTemplatesData | null; }; export const loader = async ({ request, params }: LoaderFunctionArgs) => { await verifyUserHasPermission(request, "admin.notifications.view"); const urlSearchParams = new URL(request.url).searchParams; const currentPagination = getPaginationFromCurrentUrl(urlSearchParams); const items = await NotificationService.getNotificationTemplates({ limit: 100, page: currentPagination.page - 1, }); const data: LoaderData = { items, }; return data; }; type ActionData = { error?: string; success?: string; }; export const action: ActionFunction = async ({ request }) => { await verifyUserHasPermission(request, "admin.notifications.manage"); const { t } = await getTranslations(request); const userInfo = await getUserInfo(request); await validateCSRFToken(request); const form = await request.formData(); const user = await getUser(userInfo.userId); const action = form.get("action"); if (action === "delete") { const id = form.get("id")?.toString() ?? ""; try { await NotificationService.deleteNotificationTemplate(id.toString()); return Response.json({ success: t("notifications.admin.templates.deleted"), }); } catch (e: any) { return Response.json({ error: redactNotificationDiagnosticText(e instanceof Error ? e.message : String(e)) }, { status: 500 }); } } else if (action === "create") { // const name = form.get("name")?.toString() ?? ""; // const description = form.get("description")?.toString() ?? ""; try { // await NotificationService.createNotificationTemplate({ // notificationGroupId: "", // name, // description, // steps: [], // active: true, // draft: false, // critical: false, // }); return Response.json({ success: t("shared.created") }); } catch (e: any) { return Response.json({ error: redactNotificationDiagnosticText(e instanceof Error ? e.message : String(e)) }, { status: 500 }); } } else if (action === "send-preview") { const channel = form.get("channel")?.toString() ?? ""; try { await NotificationService.send({ channel, to: user!, notification: { message: t("notifications.admin.templates.previewMessage", { channel }), }, required: true, }); return Response.json({ success: t("notifications.admin.templates.previewSent", { channel }) }); } catch (e: any) { return Response.json({ error: redactNotificationDiagnosticText(e instanceof Error ? e.message : String(e)) }, { status: 500 }); } } }; export default function () { const { t } = useTranslation(); const data = useLoaderData(); const actionData = useActionData(); const submit = useSubmit(); const [showSetup, setShowSetup] = useState(false); useEffect(() => { let missingTemplate = false; NotificationChannels.forEach((i) => { if (!data.items?.data?.find((t) => t.name === i.name)) { missingTemplate = true; } }); if (missingTemplate) { setShowSetup(true); } }, [data.items?.data]); useEffect(() => { if (actionData?.error) { toast.error(actionData?.error); } else if (actionData?.success) { toast.success(actionData?.success); } }, [actionData]); // function onCreate({ name, description }: { name: string; description: string }) { // const form = new FormData(); // form.append("action", "create"); // form.append("name", name); // form.append("description", description); // submit(form, { // method: "post", // }); // } function onDelete(id: string) { const form = new FormData(); appendCSRFToken(form); form.append("action", "delete"); form.append("id", id); submit(form, { method: "post", }); } function onSendPreview(item: NotificationChannelDto) { const form = new FormData(); appendCSRFToken(form); form.append("action", "send-preview"); form.append("channel", item.name); submit(form, { method: "post", }); } return ( <>

{t("notifications.admin.channels.title")}

{!showSetup && setShowSetup(true)}>{t("notifications.admin.channels.actions.showSetupInstructions")}}
{showSetup && }
); } export function ErrorBoundary() { return ; } function SetUpInstructions() { const { t } = useTranslation(); return (
, type: , }} />
  • ◉ {t("notifications.admin.channels.setup.fields.content")} *:{" "} {"{{message}}"}
  • ◉ {t("notifications.admin.channels.setup.fields.redirectUrl")}{" "} {t("notifications.admin.channels.setup.fields.optional")}: {"{{action.url}}"}
); }