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

247 lines
7.1 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 20:26:28 +00:00
import { TextField, Select, Button } from "@/Components/v2/inputs";
2026-01-29 19:34:49 +00:00
import MenuItem from "@mui/material/MenuItem";
import Typography from "@mui/material/Typography";
2026-01-29 20:26:28 +00:00
import Stack from "@mui/material/Stack";
import { useTheme } from "@mui/material/styles";
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 20:26:28 +00:00
const { t } = useTranslation();
const theme = useTheme();
const { notificationId } = useParams<{ notificationId: string }>();
const isEditMode = Boolean(notificationId);
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
const { data: existingNotification } = useGet<Notification>(
isEditMode ? `/notifications/${notificationId}` : null
);
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
const { schema, defaults } = useNotificationForm({ data: existingNotification });
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
const form = useForm<NotificationFormData>({
resolver: zodResolver(schema),
defaultValues: defaults,
});
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
const {
control,
watch,
reset,
handleSubmit,
} = form;
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
useEffect(() => {
reset(defaults);
}, [defaults, reset]);
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
const watchedType = watch("type");
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +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 20:12:50 +00:00
2026-01-29 20:26:28 +00:00
const onSubmit = (data: NotificationFormData) => {
console.log(data);
};
2026-01-29 20:12:50 +00:00
2026-01-29 20:26:28 +00:00
const handleTest = () => {
console.log("Test notification");
};
2026-01-29 19:29:36 +00:00
2026-01-29 20:26:28 +00:00
return (
<BasePage
component="form"
onSubmit={handleSubmit(onSubmit)}
>
2026-01-29 19:29:36 +00:00
<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:26:28 +00:00
render={({ field, fieldState }) => (
<Select
value={field.value}
fieldLabel={t("pages.notifications.form.type.optionType")}
error={!!fieldState.error}
onChange={field.onChange}
>
{NotificationChannels.map((type: string) => (
<MenuItem
key={type}
value={type}
>
<Typography textTransform="capitalize">{type}</Typography>
</MenuItem>
))}
</Select>
)}
2026-01-29 19:34:49 +00:00
/>
}
/>
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
2026-01-29 20:26:28 +00:00
title={t("pages.notifications.form.matrix.title")}
subtitle={t("pages.notifications.form.matrix.description")}
2026-01-29 20:12:50 +00:00
rightContent={
2026-01-29 20:26:28 +00:00
<Stack spacing={theme.spacing(8)}>
<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 ?? ""}
/>
)}
/>
<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 ?? ""}
/>
)}
/>
<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 ?? ""}
/>
)}
/>
</Stack>
2026-01-29 20:12:50 +00:00
}
/>
)}
2026-01-29 20:26:28 +00:00
<Stack
direction="row"
justifyContent="flex-end"
spacing={theme.spacing(2)}
>
<Button
variant="contained"
color="primary"
onClick={handleTest}
loading={false}
>
{t("common.buttons.test")}
</Button>
<Button
loading={false}
type="submit"
variant="contained"
color="primary"
>
{t("common.buttons.save")}
</Button>
</Stack>
2026-01-29 19:29:36 +00:00
</BasePage>
);
2026-01-29 18:41:46 +00:00
};
export default NotificationsCreatePage;