//Components import Breadcrumbs from "@/Components/v1/Breadcrumbs/index.jsx"; import ConfigBox from "@/Components/v1/ConfigBox/index.jsx"; import FieldWrapper from "@/Components/v1/Inputs/FieldWrapper/index.jsx"; import Link from "@/Components/v1/Link/index.jsx"; import Select from "@/Components/v1/Inputs/Select/index.jsx"; import TextInput from "@/Components/v1/Inputs/TextInput/index.jsx"; import { Box, Stack, Typography, Button, ButtonGroup } from "@mui/material"; import { HttpAdornment } from "@/Components/v1/Inputs/TextInput/Adornments/index.jsx"; import MonitorStatusHeader from "./Components/MonitorStatusHeader.jsx"; import MonitorActionButtons from "./Components/MonitorActionButtons.jsx"; import CustomAlertsSection from "./Components/CustomAlertsSection.jsx"; import DiskSelection from "./Components/DiskSelection.jsx"; // Utils import NotificationsConfig from "@/Components/v1/NotificationConfig/index.jsx"; import { useGetNotificationsByTeamId } from "../../../../Hooks/v1/useNotifications.js"; import { networkService } from "../../../../Utils/NetworkService"; import { useParams } from "react-router-dom"; import { useState, useEffect } from "react"; import { useTheme } from "@emotion/react"; import { useTranslation } from "react-i18next"; import { useDeleteMonitor, useFetchGlobalSettings, useFetchHardwareMonitorById, usePauseMonitor, } from "../../../../Hooks/v1/monitorHooks.js"; import useInfrastructureMonitorForm from "./hooks/useInfrastructureMonitorForm.jsx"; import useValidateInfrastructureForm from "./hooks/useValidateInfrastructureForm.jsx"; import useInfrastructureSubmit from "./hooks/useInfrastructureSubmit.jsx"; const CreateInfrastructureMonitor = () => { const { monitorId } = useParams(); const isCreate = typeof monitorId === "undefined"; const theme = useTheme(); const { t } = useTranslation(); // State const [https, setHttps] = useState(false); const [updateTrigger, setUpdateTrigger] = useState(false); const [availableDisks, setAvailableDisks] = useState([]); // 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; setHttps(false); initializeInfrastructureMonitorForCreate(globalSettings); } else if (monitor) { setHttps(monitor.url.startsWith("https")); initializeInfrastructureMonitorForUpdate(monitor); const fetchLastCheck = async () => { try { const { stats } = monitor ?? {}; let latestCheck = stats?.aggregateData?.latestCheck; let disks = latestCheck?.disk || []; if (disks.length > 0) { setAvailableDisks(disks); return; } const response = await networkService.getChecksByMonitor({ monitorId, dateRange: "all", rowsPerPage: 1, sortOrder: "desc", }); disks = response?.data?.data?.checks?.[0]?.disk || []; setAvailableDisks(disks); } catch (error) { setAvailableDisks([]); } }; fetchLastCheck(); } }, [ isCreate, monitor, globalSettings, globalSettingsLoading, initializeInfrastructureMonitorForCreate, initializeInfrastructureMonitorForUpdate, ]); // Handlers const onSubmit = async (event) => { event.preventDefault(); const form = buildForm(infrastructureMonitor, https); const error = validateForm(form); if (error) { return; } submitInfrastructureForm(infrastructureMonitor, form, isCreate, monitorId); }; const triggerUpdate = () => { setUpdateTrigger(!updateTrigger); }; const onChange = (event) => { const { value, name } = event.target; onChangeForm(name, value); validateField(name, value); }; 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; return ( {!isCreate ? infrastructureMonitor.name : t("createYour") + " "} {isCreate ? ( {t("monitor")} ) : ( <> )} {!isCreate && ( )} {!isCreate && ( )} {t("settingsGeneralSettings")} {t("infrastructureCreateGeneralSettingsDescription")} {t("infrastructureServerRequirement")}{" "} } placeholder={"localhost:59232/api/v1/metrics"} label={t("infrastructureServerUrlLabel")} https={https} value={infrastructureMonitor.url} onChange={onChange} error={errors["url"] ? true : false} helperText={errors["url"]} disabled={!isCreate} /> {isCreate && ( )} {t("notificationConfig.title")} {t("notificationConfig.description")} {t("createMonitorPage.incidentConfigTitle")} {t("createMonitorPage.incidentConfigDescription")} {monitorId && ( onChangeForm("selectedDisks", newSelectedDisks) } /> )} {t("distributedUptimeCreateAdvancedSettings")}