Files
Checkmate/client/src/Pages/Notifications/create/index.tsx
T

234 lines
6.9 KiB
TypeScript
Raw Normal View History

2026-01-29 19:29:36 +00:00
import { BasePage, ConfigBox } from "@/Components/v2/design-elements";
2026-01-29 19:34:49 +00:00
import { TextField, Select } from "@/Components/v2/inputs";
import MenuItem from "@mui/material/MenuItem";
import Typography from "@mui/material/Typography";
2026-01-29 19:29:36 +00:00
2026-01-29 20:12:50 +00:00
import { useEffect, useMemo } from "react";
2026-01-29 19:29:36 +00:00
import { useParams } from "react-router-dom";
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useGet } 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";
2026-01-29 19:34:49 +00:00
import { NotificationChannels } from "@/Types/Notification";
2026-01-29 19:29:36 +00:00
2026-01-29 18:41:46 +00:00
const NotificationsCreatePage = () => {
2026-01-29 19:29:36 +00:00
const { t } = useTranslation();
const { notificationId } = useParams<{ notificationId: string }>();
const isEditMode = Boolean(notificationId);
2026-01-29 20:12:50 +00:00
const { data: existingNotification } = useGet<Notification>(
isEditMode ? `/notifications/${notificationId}` : null
);
2026-01-29 19:29:36 +00:00
const { schema, defaults } = useNotificationForm({ data: existingNotification });
const form = useForm<NotificationFormData>({
resolver: zodResolver(schema),
defaultValues: defaults,
});
const {
register,
control,
watch,
reset,
2026-01-29 20:12:50 +00:00
formState: {},
2026-01-29 19:29:36 +00:00
} = form;
// Reset form when defaults change (i.e., when data is fetched)
useEffect(() => {
reset(defaults);
}, [defaults, reset]);
const watchedType = watch("type");
2026-01-29 20:12:50 +00:00
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]);
2026-01-29 19:29:36 +00:00
// Suppress unused variable warnings - these will be used when UI is built
void register;
void control;
return (
<BasePage>
<ConfigBox
title={t("pages.notifications.form.notificationName.title")}
subtitle={t("pages.notifications.form.notificationName.description")}
rightContent={
<Controller
name="notificationName"
control={control}
defaultValue={defaults.notificationName}
2026-01-29 20:12:50 +00:00
render={({ field, fieldState }) => (
2026-01-29 19:29:36 +00:00
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.notificationName.optionName")}
placeholder={t("pages.notifications.form.notificationName.placeholder")}
fullWidth
2026-01-29 20:12:50 +00:00
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
2026-01-29 19:29:36 +00:00
/>
)}
/>
}
/>
2026-01-29 19:34:49 +00:00
<ConfigBox
title={t("pages.notifications.form.type.title")}
subtitle={t("pages.notifications.form.type.description")}
rightContent={
<Controller
name="type"
control={control}
defaultValue={defaults.type}
2026-01-29 20:12:50 +00:00
render={({ field, fieldState }) => {
2026-01-29 19:34:49 +00:00
return (
<Select
value={field.value}
fieldLabel={t("pages.notifications.form.type.optionType")}
2026-01-29 20:12:50 +00:00
error={!!fieldState.error}
2026-01-29 19:34:49 +00:00
onChange={field.onChange}
>
{NotificationChannels.map((type: string) => {
return (
<MenuItem
key={type}
value={type}
>
<Typography textTransform={"capitalize"}>{type}</Typography>
</MenuItem>
);
})}
</Select>
);
}}
/>
}
/>
2026-01-29 20:12:50 +00:00
{watchedType !== "matrix" && (
<ConfigBox
title={addressConfig.title}
subtitle={addressConfig.description}
rightContent={
<Controller
name="address"
control={control}
defaultValue={defaults.address}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={addressConfig.fieldLabel}
placeholder={addressConfig.placeholder}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
}
/>
)}
{watchedType === "matrix" && (
<ConfigBox
title={t("pages.notifications.form.homeServer.title")}
subtitle={t("pages.notifications.form.homeServer.description")}
rightContent={
<Controller
name="homeserverUrl"
control={control}
defaultValue={defaults.homeserverUrl}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.homeServer.optionHomeServer")}
placeholder={t("pages.notifications.form.homeServer.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
}
/>
)}
{watchedType === "matrix" && (
<ConfigBox
title={t("pages.notifications.form.roomId.title")}
subtitle={t("pages.notifications.form.roomId.description")}
rightContent={
<Controller
name="roomId"
control={control}
defaultValue={defaults.roomId}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.roomId.optionRoomId")}
placeholder={t("pages.notifications.form.roomId.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
}
/>
)}
{watchedType === "matrix" && (
<ConfigBox
title={t("pages.notifications.form.accessToken.title")}
subtitle={t("pages.notifications.form.accessToken.description")}
rightContent={
<Controller
name="accessToken"
control={control}
defaultValue={defaults.accessToken}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.accessToken.optionAccessToken")}
placeholder={t("pages.notifications.form.accessToken.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
}
/>
)}
2026-01-29 19:29:36 +00:00
</BasePage>
);
2026-01-29 18:41:46 +00:00
};
export default NotificationsCreatePage;