import { BasePage, ConfigBox } from "@/Components/design-elements"; import { Button } from "@/Components/inputs"; import Stack from "@mui/material/Stack"; import { useTheme } from "@mui/material/styles"; import { useEffect, useMemo } from "react"; import { useParams } from "react-router-dom"; import { useNavigate } from "react-router-dom"; import { FormProvider, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useGet, usePost, usePatch } from "@/Hooks/UseApi"; import { useNotificationForm } from "@/Hooks/useNotificationForm"; import type { NotificationFormData } from "@/Validation/notifications"; import type { Notification } from "@/Types/Notification"; import { useTranslation } from "react-i18next"; import { NotificationChannels } from "@/Types/Notification"; import { FormTextField } from "@/Components/inputs/forms/FormTextField"; import { FormSelectField } from "@/Components/inputs/forms/FormSelectField"; const NotificationsCreatePage = () => { const { t } = useTranslation(); const theme = useTheme(); const navigate = useNavigate(); const { notificationId } = useParams<{ notificationId: string }>(); const isEditMode = Boolean(notificationId); const { data: existingNotification } = useGet( isEditMode ? `/notifications/${notificationId}` : null ); const { post, loading: isSubmitting } = usePost(); const { patch, loading: isPatching } = usePatch(); const { post: testPost, loading: isTesting } = usePost(); const { schema, defaults } = useNotificationForm({ data: existingNotification }); const form = useForm({ resolver: zodResolver(schema), defaultValues: defaults, }); const { watch, reset, handleSubmit, clearErrors, trigger, getValues } = form; useEffect(() => { reset(defaults); }, [defaults, reset]); const watchedType = watch("type"); useEffect(() => { clearErrors(); }, [watchedType, clearErrors]); const addressConfig = useMemo(() => { if (watchedType === "pager_duty") { return { title: t("pages.notifications.form.pagerDuty.title"), description: t("pages.notifications.form.pagerDuty.description"), fieldLabel: t("pages.notifications.form.pagerDuty.optionIntegrationKey"), placeholder: t("pages.notifications.form.pagerDuty.placeholder"), }; } if (watchedType === "email") { return { title: t("pages.notifications.form.address.title"), description: t("pages.notifications.form.address.description"), fieldLabel: t("pages.notifications.form.address.optionAddress"), placeholder: t("pages.notifications.form.address.placeholderEmail"), }; } return { title: t("pages.notifications.form.address.title"), description: t("pages.notifications.form.address.description"), fieldLabel: t("pages.notifications.form.address.optionAddress"), placeholder: t("pages.notifications.form.address.placeholderWebhook"), }; }, [watchedType, t]); const onSubmit = async (data: NotificationFormData) => { const result = isEditMode ? await patch(`/notifications/${notificationId}`, data) : await post("/notifications", data); if (result) { navigate("/notifications"); } }; const handleTest = async () => { const isValid = await trigger(); if (!isValid) return; const data = getValues(); await testPost("/notifications/test", data); }; return ( } /> ({ value: channel, label: t(`pages.notifications.form.type.value.${channel}`), }))} /> } /> {watchedType !== "matrix" && watchedType !== "telegram" && watchedType !== "pushover" && watchedType !== "signalgrid" && watchedType !== "twilio" && watchedType !== "ntfy" && ( } /> )} {watchedType === "ntfy" && ( } /> )} {watchedType === "telegram" && ( } /> )} {watchedType === "pushover" && ( } /> )} {watchedType === "signalgrid" && ( } /> )} {watchedType === "twilio" && ( } /> )} {watchedType === "matrix" && ( } /> )} ); }; export default NotificationsCreatePage;