Files
Checkmate/client/src/Pages/v1/Infrastructure/Create/index.jsx
T

368 lines
10 KiB
React
Raw Normal View History

//Components
2025-09-25 10:11:41 -07:00
import Breadcrumbs from "../../../../Components/Breadcrumbs/index.jsx";
import ConfigBox from "../../../../Components/ConfigBox/index.jsx";
import FieldWrapper from "../../../../Components/Inputs/FieldWrapper/index.jsx";
import Link from "../../../../Components/Link/index.jsx";
import Select from "../../../../Components/Inputs/Select/index.jsx";
import TextInput from "../../../../Components/Inputs/TextInput/index.jsx";
import { Box, Stack, Typography, Button, ButtonGroup } from "@mui/material";
2025-09-25 10:11:41 -07:00
import { HttpAdornment } from "../../../../Components/Inputs/TextInput/Adornments/index.jsx";
import MonitorStatusHeader from "./Components/MonitorStatusHeader.jsx";
import MonitorActionButtons from "./Components/MonitorActionButtons.jsx";
import CustomAlertsSection from "./Components/CustomAlertsSection.jsx";
// Utils
2025-09-25 10:11:41 -07:00
import NotificationsConfig from "../../../../Components/NotificationConfig/index.jsx";
import { useGetNotificationsByTeamId } from "../../../../Hooks/v1/useNotifications.js";
2025-06-12 12:44:46 +08:00
import { useParams } from "react-router-dom";
import { useState, useEffect } from "react";
import { useTheme } from "@emotion/react";
2025-03-12 00:56:28 +05:30
import { useTranslation } from "react-i18next";
2025-07-29 00:42:56 +05:30
import {
useDeleteMonitor,
2025-07-29 00:42:56 +05:30
useFetchGlobalSettings,
useFetchHardwareMonitorById,
usePauseMonitor,
2025-09-25 10:11:41 -07:00
} from "../../../../Hooks/v1/monitorHooks.js";
import useInfrastructureMonitorForm from "./hooks/useInfrastructureMonitorForm.jsx";
import useValidateInfrastructureForm from "./hooks/useValidateInfrastructureForm.jsx";
import useInfrastructureSubmit from "./hooks/useInfrastructureSubmit.jsx";
2024-12-09 13:35:48 +08:00
2024-10-31 15:19:57 -04:00
const CreateInfrastructureMonitor = () => {
2025-03-14 22:12:52 -04:00
const { monitorId } = useParams();
const isCreate = typeof monitorId === "undefined";
const theme = useTheme();
const { t } = useTranslation();
2024-12-09 13:35:48 +08:00
// State
const [https, setHttps] = useState(false);
const [updateTrigger, setUpdateTrigger] = useState(false);
// Fetch monitor details if editing
const [monitor, isLoading] = useFetchHardwareMonitorById({
monitorId,
updateTrigger,
});
const [deleteMonitor, isDeleting] = useDeleteMonitor();
const [globalSettings, globalSettingsLoading] = useFetchGlobalSettings();
const [notifications, notificationsAreLoading] = useGetNotificationsByTeamId();
const [pauseMonitor, isPausing] = usePauseMonitor();
const {
infrastructureMonitor,
setInfrastructureMonitor,
onChangeForm,
handleCheckboxChange,
initializeInfrastructureMonitorForCreate,
initializeInfrastructureMonitorForUpdate,
} = useInfrastructureMonitorForm();
const { errors, validateField, validateForm } = useValidateInfrastructureForm();
const { buildForm, submitInfrastructureForm, isCreating, isUpdating } =
useInfrastructureSubmit();
const FREQUENCIES = [
{ _id: 0.25, name: t("time.fifteenSeconds") },
{ _id: 0.5, name: t("time.thirtySeconds") },
{ _id: 1, name: t("time.oneMinute") },
{ _id: 2, name: t("time.twoMinutes") },
{ _id: 5, name: t("time.fiveMinutes") },
{ _id: 10, name: t("time.tenMinutes") },
];
const CRUMBS = [
{ name: "Infrastructure monitors", path: "/infrastructure" },
...(isCreate
? [{ name: "Create", path: "/infrastructure/create" }]
: [
{ name: "Details", path: `/infrastructure/${monitorId}` },
{ name: "Configure", path: `/infrastructure/configure/${monitorId}` },
]),
];
// Populate form fields if editing
useEffect(() => {
if (isCreate) {
if (globalSettingsLoading) return;
2025-07-31 16:04:41 +05:30
setHttps(false);
initializeInfrastructureMonitorForCreate(globalSettings);
} else if (monitor) {
2025-07-31 16:04:41 +05:30
setHttps(monitor.url.startsWith("https"));
initializeInfrastructureMonitorForUpdate(monitor);
}
2025-09-19 00:06:31 +05:30
}, [
isCreate,
monitor,
globalSettings,
globalSettingsLoading,
initializeInfrastructureMonitorForCreate,
initializeInfrastructureMonitorForUpdate,
]);
2024-12-09 13:35:48 +08:00
// Handlers
const onSubmit = async (event) => {
2024-11-11 11:16:06 -05:00
event.preventDefault();
const form = buildForm(infrastructureMonitor, https);
const error = validateForm(form);
2024-12-09 13:35:48 +08:00
if (error) {
return;
2024-10-31 15:19:57 -04:00
}
submitInfrastructureForm(infrastructureMonitor, form, isCreate, monitorId);
};
2024-12-09 13:35:48 +08:00
const triggerUpdate = () => {
setUpdateTrigger(!updateTrigger);
};
const onChange = (event) => {
2024-12-10 11:30:38 +08:00
const { value, name } = event.target;
onChangeForm(name, value);
validateField(name, value);
2024-12-09 13:35:48 +08:00
};
const handlePause = async () => {
await pauseMonitor({ monitorId, triggerUpdate });
};
const handleRemove = async (event) => {
event.preventDefault();
await deleteMonitor({ monitor, redirect: "/infrastructure" });
};
const isBusy =
isLoading ||
isUpdating ||
isCreating ||
isDeleting ||
isPausing ||
notificationsAreLoading;
2024-10-31 15:19:57 -04:00
return (
<Box className="create-infrastructure-monitor">
<Breadcrumbs list={CRUMBS} />
2024-10-31 15:19:57 -04:00
<Stack
component="form"
onSubmit={onSubmit}
2024-10-31 15:19:57 -04:00
noValidate
spellCheck="false"
gap={theme.spacing(12)}
mt={theme.spacing(6)}
>
<Stack
direction="row"
gap={theme.spacing(2)}
2024-10-31 15:19:57 -04:00
>
<Box>
<Typography
component="h1"
variant="h1"
>
<Typography
component="span"
fontSize="inherit"
color={
!isCreate ? theme.palette.primary.contrastTextSecondary : undefined
}
>
{!isCreate ? infrastructureMonitor.name : t("createYour") + " "}
</Typography>
{isCreate ? (
<Typography
component="span"
fontSize="inherit"
fontWeight="inherit"
color={theme.palette.primary.contrastTextSecondary}
>
{t("monitor")}
</Typography>
) : (
<></>
)}
</Typography>
{!isCreate && (
<MonitorStatusHeader
monitor={monitor}
infrastructureMonitor={infrastructureMonitor}
/>
)}
</Box>
{!isCreate && (
<MonitorActionButtons
monitor={monitor}
isBusy={isBusy}
handlePause={handlePause}
handleRemove={handleRemove}
/>
)}
</Stack>
2024-10-31 15:19:57 -04:00
<ConfigBox>
2025-07-28 16:28:44 -04:00
<Stack>
2025-05-27 09:10:32 -07:00
<Typography
component="h2"
variant="h2"
>
{t("settingsGeneralSettings")}
</Typography>
2024-12-09 13:35:48 +08:00
<Typography component="p">
2025-03-12 00:56:28 +05:30
{t("infrastructureCreateGeneralSettingsDescription")}
2024-12-09 13:35:48 +08:00
</Typography>
<Typography component="p">
2025-03-12 00:56:28 +05:30
{t("infrastructureServerRequirement")}{" "}
2024-12-09 13:35:48 +08:00
<Link
level="primary"
url="https://github.com/bluewave-labs/checkmate-agent"
label={t("common.monitoringAgentName")}
2024-12-09 13:35:48 +08:00
/>
</Typography>
</Stack>
<Stack gap={theme.spacing(8)}>
<TextInput
2024-12-09 13:35:48 +08:00
type="url"
id="url"
2024-12-10 11:30:38 +08:00
name="url"
2024-12-09 13:35:48 +08:00
startAdornment={<HttpAdornment https={https} />}
placeholder={"localhost:59232/api/v1/metrics"}
2025-03-12 00:56:28 +05:30
label={t("infrastructureServerUrlLabel")}
2024-12-09 13:35:48 +08:00
https={https}
2024-10-31 15:19:57 -04:00
value={infrastructureMonitor.url}
onChange={onChange}
error={errors["url"] ? true : false}
helperText={errors["url"]}
disabled={!isCreate}
2024-10-31 15:19:57 -04:00
/>
{isCreate && (
<FieldWrapper
label={t("infrastructureProtocol")}
labelVariant="p"
>
2025-05-27 09:10:32 -07:00
<ButtonGroup>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
{t("https")}
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
{t("http")}
</Button>
</ButtonGroup>
</FieldWrapper>
)}
<TextInput
2024-10-31 15:19:57 -04:00
type="text"
id="name"
2024-12-10 11:30:38 +08:00
name="name"
2025-03-12 00:56:28 +05:30
label={t("infrastructureDisplayNameLabel")}
2024-11-29 10:17:09 -05:00
placeholder="Google"
2024-10-31 15:19:57 -04:00
isOptional={true}
value={infrastructureMonitor.name}
onChange={onChange}
2024-10-31 15:19:57 -04:00
error={errors["name"]}
/>
<TextInput
2024-11-04 10:06:00 -05:00
type="text"
id="secret"
2024-12-10 11:30:38 +08:00
name="secret"
2025-03-12 00:56:28 +05:30
label={t("infrastructureAuthorizationSecretLabel")}
2024-11-04 10:06:00 -05:00
value={infrastructureMonitor.secret}
onChange={onChange}
error={errors["secret"] ? true : false}
helperText={errors["secret"]}
2024-10-31 15:19:57 -04:00
/>
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
2025-07-28 16:28:44 -04:00
<Typography
component="h2"
variant="h2"
>
{t("notificationConfig.title")}
</Typography>
<Typography component="p">{t("notificationConfig.description")}</Typography>
2024-10-31 15:19:57 -04:00
</Box>
<NotificationsConfig
notifications={notifications}
setMonitor={setInfrastructureMonitor}
setNotifications={infrastructureMonitor.notifications}
/>
2024-11-04 21:02:28 -05:00
</ConfigBox>
<ConfigBox>
<Box>
<Typography
component="h2"
variant="h2"
>
{t("createMonitorPage.incidentConfigTitle")}
</Typography>
<Typography component="p">
{t("createMonitorPage.incidentConfigDescription")}
</Typography>
</Box>
<Stack gap={theme.spacing(20)}>
<TextInput
name="statusWindowSize"
label={t("createMonitorPage.incidentConfigStatusWindowLabel")}
type="number"
value={infrastructureMonitor.statusWindowSize}
onChange={onChange}
error={errors["statusWindowSize"] ? true : false}
helperText={errors["statusWindowSize"]}
/>
<TextInput
name="statusWindowThreshold"
label={t("createMonitorPage.incidentConfigStatusWindowThresholdLabel")}
type="number"
value={infrastructureMonitor.statusWindowThreshold}
onChange={onChange}
error={errors["statusWindowThreshold"] ? true : false}
helperText={errors["statusWindowThreshold"]}
/>
</Stack>
</ConfigBox>
<CustomAlertsSection
errors={errors}
onChange={onChange}
infrastructureMonitor={infrastructureMonitor}
handleCheckboxChange={handleCheckboxChange}
/>
2024-10-31 15:19:57 -04:00
<ConfigBox>
<Box>
2025-05-27 09:10:32 -07:00
<Typography
component="h2"
variant="h2"
>
{t("distributedUptimeCreateAdvancedSettings")}
</Typography>
2024-10-31 15:19:57 -04:00
</Box>
<Stack gap={theme.spacing(12)}>
<Select
id="interval"
2024-12-10 11:30:38 +08:00
name="interval"
2024-10-31 15:19:57 -04:00
label="Check frequency"
2024-11-07 15:27:40 -05:00
value={infrastructureMonitor.interval || 15}
onChange={onChange}
items={FREQUENCIES}
2024-10-31 15:19:57 -04:00
/>
</Stack>
</ConfigBox>
<Stack
direction="row"
justifyContent="flex-end"
>
2025-02-13 15:20:12 -08:00
<Button
type="submit"
2024-10-31 15:19:57 -04:00
variant="contained"
2025-01-14 08:09:03 -05:00
color="accent"
loading={isBusy}
2024-10-31 15:19:57 -04:00
>
2025-05-27 09:10:32 -07:00
{t(isCreate ? "infrastructureCreateMonitor" : "infrastructureEditMonitor")}
2025-02-13 15:20:12 -08:00
</Button>
2024-10-31 15:19:57 -04:00
</Stack>
</Stack>
</Box>
);
};
export default CreateInfrastructureMonitor;