form
This commit is contained in:
1 parent
3ba3e4b865
commit
0b5e28c296
6 files changed
+240
-1
No files matched your search
@@ -0,0 +1,78 @@
|
|||||||
|
import Stack from "@mui/material/Stack";
|
||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
|
import { useTheme } from "@mui/material/styles";
|
||||||
|
import useMediaQuery from "@mui/material/useMediaQuery";
|
||||||
|
export const SplitBox = ({
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
}: {
|
||||||
|
left: React.ReactNode;
|
||||||
|
right: React.ReactNode;
|
||||||
|
}) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
const isSmall = useMediaQuery(theme.breakpoints.down("md"));
|
||||||
|
return (
|
||||||
|
<Stack
|
||||||
|
direction={isSmall ? "column" : "row"}
|
||||||
|
bgcolor={theme.palette.background.paper}
|
||||||
|
border={1}
|
||||||
|
borderColor={theme.palette.divider}
|
||||||
|
borderRadius={theme.spacing(2)}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
padding={theme.spacing(15)}
|
||||||
|
borderRight={isSmall ? 0 : 1}
|
||||||
|
borderBottom={isSmall ? 1 : 0}
|
||||||
|
borderColor={theme.palette.divider}
|
||||||
|
flex={0.7}
|
||||||
|
sx={{
|
||||||
|
background:
|
||||||
|
theme.palette.mode === "dark"
|
||||||
|
? "linear-gradient(135deg, rgba(255, 255, 255, 0.01) 0%, rgba(255, 255, 255, 0.02) 100%)"
|
||||||
|
: "linear-gradient(135deg, rgba(0, 0, 0, 0.01) 0%, rgba(0, 0, 0, 0.02) 100%)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{left}
|
||||||
|
</Box>
|
||||||
|
<Box
|
||||||
|
flex={1}
|
||||||
|
padding={theme.spacing(15)}
|
||||||
|
>
|
||||||
|
{right}
|
||||||
|
</Box>
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ConfigBox = ({
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
leftContent,
|
||||||
|
rightContent,
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
subtitle: string;
|
||||||
|
leftContent?: React.ReactNode;
|
||||||
|
rightContent: React.ReactNode;
|
||||||
|
}) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
return (
|
||||||
|
<SplitBox
|
||||||
|
left={
|
||||||
|
<Stack spacing={theme.spacing(4)}>
|
||||||
|
<Typography
|
||||||
|
textTransform={"capitalize"}
|
||||||
|
component="h2"
|
||||||
|
variant="h2"
|
||||||
|
>
|
||||||
|
{title}
|
||||||
|
</Typography>
|
||||||
|
<Typography component="p">{subtitle}</Typography>
|
||||||
|
{leftContent}
|
||||||
|
</Stack>
|
||||||
|
}
|
||||||
|
right={rightContent}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -17,3 +17,4 @@ export * from "./StatBox";
|
|||||||
export * from "./BaseChart";
|
export * from "./BaseChart";
|
||||||
export * from "./Gauge";
|
export * from "./Gauge";
|
||||||
export * from "./Tabs";
|
export * from "./Tabs";
|
||||||
|
export * from "./SplitBox";
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { useMemo } from "react";
|
||||||
|
import { notificationSchema } from "@/Validation/notifications";
|
||||||
|
import type { Notification } from "@/Types/Notification";
|
||||||
|
|
||||||
|
interface UseNotificationFormOptions {
|
||||||
|
data?: Notification | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
||||||
|
return useMemo(() => {
|
||||||
|
const defaults =
|
||||||
|
data?.type === "matrix"
|
||||||
|
? {
|
||||||
|
type: "matrix" as const,
|
||||||
|
notificationName: data.notificationName || "",
|
||||||
|
homeserverUrl: data.homeserverUrl || "",
|
||||||
|
roomId: data.roomId || "",
|
||||||
|
accessToken: data.accessToken || "",
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
type: (data?.type || "email") as Exclude<Notification["type"], "matrix">,
|
||||||
|
notificationName: data?.notificationName || "",
|
||||||
|
address: data?.address || "",
|
||||||
|
};
|
||||||
|
|
||||||
|
return { schema: notificationSchema, defaults };
|
||||||
|
}, [data]);
|
||||||
|
};
|
||||||
@@ -1,5 +1,78 @@
|
|||||||
|
import { BasePage, ConfigBox } from "@/Components/v2/design-elements";
|
||||||
|
import { TextField } from "@/Components/v2/inputs";
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
|
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";
|
||||||
|
|
||||||
const NotificationsCreatePage = () => {
|
const NotificationsCreatePage = () => {
|
||||||
return <div>Create Notifications Page</div>;
|
const { t } = useTranslation();
|
||||||
|
const { notificationId } = useParams<{ notificationId: string }>();
|
||||||
|
const isEditMode = Boolean(notificationId);
|
||||||
|
|
||||||
|
const { data: existingNotification, isLoading: notificationIsLoading } =
|
||||||
|
useGet<Notification>(isEditMode ? `/notifications/${notificationId}` : null);
|
||||||
|
|
||||||
|
const { schema, defaults } = useNotificationForm({ data: existingNotification });
|
||||||
|
|
||||||
|
const form = useForm<NotificationFormData>({
|
||||||
|
resolver: zodResolver(schema),
|
||||||
|
defaultValues: defaults,
|
||||||
|
});
|
||||||
|
|
||||||
|
const {
|
||||||
|
register,
|
||||||
|
control,
|
||||||
|
watch,
|
||||||
|
reset,
|
||||||
|
formState: { errors },
|
||||||
|
} = form;
|
||||||
|
|
||||||
|
// Reset form when defaults change (i.e., when data is fetched)
|
||||||
|
useEffect(() => {
|
||||||
|
reset(defaults);
|
||||||
|
}, [defaults, reset]);
|
||||||
|
|
||||||
|
const watchedType = watch("type");
|
||||||
|
|
||||||
|
// 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}
|
||||||
|
render={({ field }) => (
|
||||||
|
<TextField
|
||||||
|
{...field}
|
||||||
|
type="text"
|
||||||
|
fieldLabel={t("pages.notifications.form.notificationName.optionName")}
|
||||||
|
placeholder={t("pages.notifications.form.notificationName.placeholder")}
|
||||||
|
fullWidth
|
||||||
|
error={!!errors.notificationName}
|
||||||
|
helperText={
|
||||||
|
errors.notificationName ? errors.notificationName.message : ""
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</BasePage>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default NotificationsCreatePage;
|
export default NotificationsCreatePage;
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
const baseSchema = z.object({
|
||||||
|
notificationName: z
|
||||||
|
.string()
|
||||||
|
.min(1, "Notification name is required")
|
||||||
|
.max(100, "Notification name must be at most 100 characters"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const emailSchema = baseSchema.extend({
|
||||||
|
type: z.literal("email"),
|
||||||
|
address: z.string().min(1, "Email is required").email("Please enter a valid email address"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const slackSchema = baseSchema.extend({
|
||||||
|
type: z.literal("slack"),
|
||||||
|
address: z.string().min(1, "Webhook URL is required").url("Please enter a valid URL"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const discordSchema = baseSchema.extend({
|
||||||
|
type: z.literal("discord"),
|
||||||
|
address: z.string().min(1, "Webhook URL is required").url("Please enter a valid URL"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const webhookSchema = baseSchema.extend({
|
||||||
|
type: z.literal("webhook"),
|
||||||
|
address: z.string().min(1, "Webhook URL is required").url("Please enter a valid URL"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const pagerDutySchema = baseSchema.extend({
|
||||||
|
type: z.literal("pager_duty"),
|
||||||
|
address: z.string().min(1, "Integration key is required"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const matrixSchema = baseSchema.extend({
|
||||||
|
type: z.literal("matrix"),
|
||||||
|
homeserverUrl: z.string().min(1, "Homeserver URL is required").url("Please enter a valid URL"),
|
||||||
|
roomId: z.string().min(1, "Room ID is required"),
|
||||||
|
accessToken: z.string().min(1, "Access token is required"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const notificationSchema = z.discriminatedUnion("type", [
|
||||||
|
emailSchema,
|
||||||
|
slackSchema,
|
||||||
|
discordSchema,
|
||||||
|
webhookSchema,
|
||||||
|
pagerDutySchema,
|
||||||
|
matrixSchema,
|
||||||
|
]);
|
||||||
|
|
||||||
|
export type NotificationFormData = z.infer<typeof notificationSchema>;
|
||||||
@@ -342,6 +342,14 @@
|
|||||||
"headers": {
|
"headers": {
|
||||||
"destination": "Destination"
|
"destination": "Destination"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"form": {
|
||||||
|
"notificationName": {
|
||||||
|
"description": "A descriptive name for the notification channel",
|
||||||
|
"optionName": "Channel name",
|
||||||
|
"placeholder": "e.g. Production Alerts",
|
||||||
|
"title": "Channel name"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user