Merge pull request #3673 from bluewave-labs/feat/fe/uptime-progress-indicator
Feat: Uptime Progress Indicator
This commit is contained in:
6 files changed
+237
-43
No files matched your search
@@ -0,0 +1,42 @@
|
|||||||
|
import Stack from "@mui/material/Stack";
|
||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import { useTheme, alpha } from "@mui/material/styles";
|
||||||
|
import { LAYOUT } from "@/Utils/Theme/constants";
|
||||||
|
|
||||||
|
interface StepProgressProps {
|
||||||
|
steps: number;
|
||||||
|
current: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const StepProgress = ({ steps, current }: StepProgressProps) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
const activeBg = `linear-gradient(135deg, ${theme.palette.primary.light} 0%, ${theme.palette.primary.main} 100%)`;
|
||||||
|
const completedBg = `linear-gradient(135deg, ${alpha(theme.palette.primary.light, 0.5)} 0%, ${alpha(theme.palette.primary.main, 0.5)} 100%)`;
|
||||||
|
|
||||||
|
const getSegmentBackground = (index: number) => {
|
||||||
|
if (index < current) return completedBg;
|
||||||
|
if (index === current) return activeBg;
|
||||||
|
return theme.palette.divider;
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
gap={theme.spacing(LAYOUT.XS)}
|
||||||
|
width="100%"
|
||||||
|
>
|
||||||
|
{Array.from({ length: steps }).map((_, index) => (
|
||||||
|
<Box
|
||||||
|
key={index}
|
||||||
|
flex={1}
|
||||||
|
height={theme.spacing(LAYOUT.XXS)}
|
||||||
|
borderRadius={theme.shape.borderRadius}
|
||||||
|
sx={{
|
||||||
|
background: getSegmentBackground(index),
|
||||||
|
transition: "background 0.2s ease",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -27,3 +27,4 @@ export * from "./OfflineBanner";
|
|||||||
export * from "./Avatar";
|
export * from "./Avatar";
|
||||||
export * from "./StatusCodeLabel";
|
export * from "./StatusCodeLabel";
|
||||||
export * from "./StrategyBadge";
|
export * from "./StrategyBadge";
|
||||||
|
export * from "./StepProgress";
|
||||||
@@ -16,17 +16,19 @@ const getBaseDefaults = (data?: Monitor | null) => ({
|
|||||||
tags: data?.tags || [],
|
tags: data?.tags || [],
|
||||||
statusWindowSize: data?.statusWindowSize || 5,
|
statusWindowSize: data?.statusWindowSize || 5,
|
||||||
statusWindowThreshold: data?.statusWindowThreshold || 60,
|
statusWindowThreshold: data?.statusWindowThreshold || 60,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Geo-check defaults — only http and ping support geo checks.
|
||||||
|
const getGeoCheckDefaults = (data?: Monitor | null) => ({
|
||||||
geoCheckEnabled: data?.geoCheckEnabled ?? false,
|
geoCheckEnabled: data?.geoCheckEnabled ?? false,
|
||||||
geoCheckLocations: data?.geoCheckLocations || [],
|
geoCheckLocations: data?.geoCheckLocations || [],
|
||||||
geoCheckInterval: data?.geoCheckInterval || 300000,
|
geoCheckInterval: data?.geoCheckInterval || 300000,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useMonitorForm = ({
|
export const getMonitorDefaults = (
|
||||||
data = null,
|
type: MonitorType,
|
||||||
defaultType = "http",
|
data: Monitor | null = null
|
||||||
}: UseMonitorFormOptions = {}) => {
|
): MonitorFormData => {
|
||||||
return useMemo(() => {
|
|
||||||
const type = data?.type || defaultType;
|
|
||||||
const base = getBaseDefaults(data);
|
const base = getBaseDefaults(data);
|
||||||
|
|
||||||
let defaults: MonitorFormData;
|
let defaults: MonitorFormData;
|
||||||
@@ -35,6 +37,7 @@ export const useMonitorForm = ({
|
|||||||
case "http":
|
case "http":
|
||||||
defaults = {
|
defaults = {
|
||||||
...base,
|
...base,
|
||||||
|
...getGeoCheckDefaults(data),
|
||||||
type: "http",
|
type: "http",
|
||||||
url: data?.url || "",
|
url: data?.url || "",
|
||||||
ignoreTlsErrors: data?.ignoreTlsErrors || false,
|
ignoreTlsErrors: data?.ignoreTlsErrors || false,
|
||||||
@@ -47,6 +50,7 @@ export const useMonitorForm = ({
|
|||||||
case "ping":
|
case "ping":
|
||||||
defaults = {
|
defaults = {
|
||||||
...base,
|
...base,
|
||||||
|
...getGeoCheckDefaults(data),
|
||||||
type: "ping",
|
type: "ping",
|
||||||
url: data?.url || "",
|
url: data?.url || "",
|
||||||
};
|
};
|
||||||
@@ -126,6 +130,7 @@ export const useMonitorForm = ({
|
|||||||
default:
|
default:
|
||||||
defaults = {
|
defaults = {
|
||||||
...base,
|
...base,
|
||||||
|
...getGeoCheckDefaults(data),
|
||||||
type: "http",
|
type: "http",
|
||||||
url: "",
|
url: "",
|
||||||
ignoreTlsErrors: false,
|
ignoreTlsErrors: false,
|
||||||
@@ -136,6 +141,15 @@ export const useMonitorForm = ({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { schema: monitorSchema, defaults };
|
return defaults;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useMonitorForm = ({
|
||||||
|
data = null,
|
||||||
|
defaultType = "http",
|
||||||
|
}: UseMonitorFormOptions = {}) => {
|
||||||
|
return useMemo(() => {
|
||||||
|
const type = data?.type || defaultType;
|
||||||
|
return { schema: monitorSchema, defaults: getMonitorDefaults(type, data) };
|
||||||
}, [data, defaultType]);
|
}, [data, defaultType]);
|
||||||
};
|
};
|
||||||
@@ -18,7 +18,12 @@ import { Trash2 } from "lucide-react";
|
|||||||
import { HeaderDeleteControls } from "@/Components/monitors";
|
import { HeaderDeleteControls } from "@/Components/monitors";
|
||||||
import { GeoContinents } from "@/Types/GeoCheck";
|
import { GeoContinents } from "@/Types/GeoCheck";
|
||||||
|
|
||||||
import { BasePage, ColoredLabel, ConfigBox } from "@/Components/design-elements";
|
import {
|
||||||
|
BasePage,
|
||||||
|
ColoredLabel,
|
||||||
|
ConfigBox,
|
||||||
|
StepProgress,
|
||||||
|
} from "@/Components/design-elements";
|
||||||
import {
|
import {
|
||||||
RadioWithDescription,
|
RadioWithDescription,
|
||||||
Button,
|
Button,
|
||||||
@@ -31,7 +36,7 @@ import {
|
|||||||
} from "@/Components/inputs";
|
} from "@/Components/inputs";
|
||||||
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
|
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
|
||||||
import { useGet, usePost, usePatch, useDelete } from "@/Hooks/UseApi";
|
import { useGet, usePost, usePatch, useDelete } from "@/Hooks/UseApi";
|
||||||
import { useMonitorForm } from "@/Hooks/useMonitorForm";
|
import { useMonitorForm, getMonitorDefaults } from "@/Hooks/useMonitorForm";
|
||||||
import {
|
import {
|
||||||
type Monitor,
|
type Monitor,
|
||||||
type MonitorType,
|
type MonitorType,
|
||||||
@@ -41,7 +46,11 @@ import {
|
|||||||
} from "@/Types/Monitor";
|
} from "@/Types/Monitor";
|
||||||
import type { Notification } from "@/Types/Notification";
|
import type { Notification } from "@/Types/Notification";
|
||||||
import type { Tag } from "@/Types/Tag";
|
import type { Tag } from "@/Types/Tag";
|
||||||
import type { MonitorFormData } from "@/Validation/monitor";
|
import {
|
||||||
|
stepFieldsFor,
|
||||||
|
monitorStepCount,
|
||||||
|
type MonitorFormData,
|
||||||
|
} from "@/Validation/monitor";
|
||||||
|
|
||||||
interface GeneralSettingsConfig {
|
interface GeneralSettingsConfig {
|
||||||
urlLabel: string;
|
urlLabel: string;
|
||||||
@@ -249,20 +258,41 @@ const CreateMonitorPage = () => {
|
|||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
const { control, watch, handleSubmit, clearErrors } = form;
|
const { control, watch, handleSubmit, clearErrors, trigger, reset } = form;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
form.reset(defaults);
|
reset(defaults);
|
||||||
}, [defaults, form]);
|
}, [defaults, reset]);
|
||||||
|
|
||||||
|
// Multi-step wizard, uptime create only (showTypeSelector). Other flows
|
||||||
|
// keep the flat form, where showStep() is always true.
|
||||||
|
const [currentStep, setCurrentStep] = useState(0);
|
||||||
|
const showStep = (step: number) => !showTypeSelector || currentStep === step;
|
||||||
|
|
||||||
const watchedType = watch("type") as MonitorType;
|
const watchedType = watch("type") as MonitorType;
|
||||||
|
|
||||||
const watchedUseAdvancedMatching = watch("useAdvancedMatching") as boolean;
|
const watchedUseAdvancedMatching = watch("useAdvancedMatching") as boolean;
|
||||||
const watchGeoCheckEnabled = watch("geoCheckEnabled") as boolean;
|
const watchGeoCheckEnabled = watch("geoCheckEnabled") as boolean;
|
||||||
|
|
||||||
|
// Steps without an advanced section drop it, so the last step is the form's.
|
||||||
|
const totalSteps = monitorStepCount(watchedType);
|
||||||
|
|
||||||
|
const handleNext = async () => {
|
||||||
|
const isValid = await trigger(stepFieldsFor(watchedType, currentStep));
|
||||||
|
if (isValid) {
|
||||||
|
setCurrentStep((step) => step + 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const handleBack = () => setCurrentStep((step) => step - 1);
|
||||||
|
|
||||||
|
// Reset the form when the monitor type changes so stale type-specific fields
|
||||||
|
// can't produce an invalid monitor. Type can only be changed on step 0.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (showTypeSelector) {
|
||||||
|
reset(getMonitorDefaults(watchedType));
|
||||||
|
} else {
|
||||||
clearErrors();
|
clearErrors();
|
||||||
}, [watchedType, clearErrors]);
|
}
|
||||||
|
}, [watchedType, showTypeSelector, reset, clearErrors]);
|
||||||
|
|
||||||
const generalSettingsConfig = useMemo(
|
const generalSettingsConfig = useMemo(
|
||||||
() => getGeneralSettingsConfig(watchedType, t),
|
() => getGeneralSettingsConfig(watchedType, t),
|
||||||
@@ -332,8 +362,14 @@ const CreateMonitorPage = () => {
|
|||||||
refetch={refetchMonitor}
|
refetch={refetchMonitor}
|
||||||
onDelete={handleDeleteClick}
|
onDelete={handleDeleteClick}
|
||||||
/>
|
/>
|
||||||
{/* Monitor Type Selection - only shown for uptime monitors */}
|
|
||||||
{showTypeSelector && (
|
{showTypeSelector && (
|
||||||
|
<StepProgress
|
||||||
|
steps={totalSteps}
|
||||||
|
current={currentStep}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{/* Monitor Type Selection - only shown for uptime monitors */}
|
||||||
|
{showTypeSelector && showStep(0) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.type.title")}
|
title={t("pages.createMonitor.form.type.title")}
|
||||||
subtitle={t("pages.createMonitor.form.type.description")}
|
subtitle={t("pages.createMonitor.form.type.description")}
|
||||||
@@ -411,6 +447,7 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{showStep(0) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.general.title")}
|
title={t("pages.createMonitor.form.general.title")}
|
||||||
subtitle={
|
subtitle={
|
||||||
@@ -513,7 +550,9 @@ const CreateMonitorPage = () => {
|
|||||||
error={!!fieldState.error}
|
error={!!fieldState.error}
|
||||||
>
|
>
|
||||||
<MenuItem value="">
|
<MenuItem value="">
|
||||||
{t("pages.createMonitor.form.general.option.game.placeholder")}{" "}
|
{t(
|
||||||
|
"pages.createMonitor.form.general.option.game.placeholder"
|
||||||
|
)}{" "}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
{games &&
|
{games &&
|
||||||
Object.entries(games).map(([key, game]) => (
|
Object.entries(games).map(([key, game]) => (
|
||||||
@@ -563,7 +602,9 @@ const CreateMonitorPage = () => {
|
|||||||
{...field}
|
{...field}
|
||||||
value={field.value ?? ""}
|
value={field.value ?? ""}
|
||||||
type="text"
|
type="text"
|
||||||
fieldLabel={t("pages.createMonitor.form.general.option.secret.label")}
|
fieldLabel={t(
|
||||||
|
"pages.createMonitor.form.general.option.secret.label"
|
||||||
|
)}
|
||||||
placeholder={t(
|
placeholder={t(
|
||||||
"pages.createMonitor.form.general.option.secret.placeholder"
|
"pages.createMonitor.form.general.option.secret.placeholder"
|
||||||
)}
|
)}
|
||||||
@@ -611,7 +652,9 @@ const CreateMonitorPage = () => {
|
|||||||
error={!!fieldState.error}
|
error={!!fieldState.error}
|
||||||
>
|
>
|
||||||
<MenuItem value={"A"}>
|
<MenuItem value={"A"}>
|
||||||
{t("pages.createMonitor.form.general.option.dnsRecordType.value.A")}
|
{t(
|
||||||
|
"pages.createMonitor.form.general.option.dnsRecordType.value.A"
|
||||||
|
)}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem value={"AAAA"}>
|
<MenuItem value={"AAAA"}>
|
||||||
{t(
|
{t(
|
||||||
@@ -661,7 +704,9 @@ const CreateMonitorPage = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.frequency.title")}
|
title={t("pages.createMonitor.form.frequency.title")}
|
||||||
subtitle={t("pages.createMonitor.form.frequency.description")}
|
subtitle={t("pages.createMonitor.form.frequency.description")}
|
||||||
@@ -733,9 +778,10 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Alert Thresholds - only for hardware type */}
|
{/* Alert Thresholds - only for hardware type */}
|
||||||
{generalSettingsConfig.showSecret && (
|
{generalSettingsConfig.showSecret && showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.thresholds.title")}
|
title={t("pages.createMonitor.form.thresholds.title")}
|
||||||
subtitle={t("pages.createMonitor.form.thresholds.description")}
|
subtitle={t("pages.createMonitor.form.thresholds.description")}
|
||||||
@@ -818,6 +864,7 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.incidents.title")}
|
title={t("pages.createMonitor.form.incidents.title")}
|
||||||
subtitle={t("pages.createMonitor.form.incidents.description")}
|
subtitle={t("pages.createMonitor.form.incidents.description")}
|
||||||
@@ -830,7 +877,9 @@ const CreateMonitorPage = () => {
|
|||||||
<SliderWithLabel
|
<SliderWithLabel
|
||||||
{...field}
|
{...field}
|
||||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||||
fieldLabel={t("pages.createMonitor.form.incidents.option.checks.label")}
|
fieldLabel={t(
|
||||||
|
"pages.createMonitor.form.incidents.option.checks.label"
|
||||||
|
)}
|
||||||
min={1}
|
min={1}
|
||||||
max={25}
|
max={25}
|
||||||
valueLabelDisplay="auto"
|
valueLabelDisplay="auto"
|
||||||
@@ -856,7 +905,9 @@ const CreateMonitorPage = () => {
|
|||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.notifications.title")}
|
title={t("pages.createMonitor.form.notifications.title")}
|
||||||
subtitle={t("pages.createMonitor.form.notifications.description")}
|
subtitle={t("pages.createMonitor.form.notifications.description")}
|
||||||
@@ -924,7 +975,9 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showStep(1) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.tags.title")}
|
title={t("pages.createMonitor.form.tags.title")}
|
||||||
subtitle={t("pages.createMonitor.form.tags.description")}
|
subtitle={t("pages.createMonitor.form.tags.description")}
|
||||||
@@ -978,7 +1031,9 @@ const CreateMonitorPage = () => {
|
|||||||
size="small"
|
size="small"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
field.onChange(
|
field.onChange(
|
||||||
(field.value ?? []).filter((id: string) => id !== tag.id)
|
(field.value ?? []).filter(
|
||||||
|
(id: string) => id !== tag.id
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
aria-label="Remove tag"
|
aria-label="Remove tag"
|
||||||
@@ -996,8 +1051,10 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{(watchedType === "http" ||
|
{showStep(2) &&
|
||||||
|
(watchedType === "http" ||
|
||||||
watchedType === "grpc" ||
|
watchedType === "grpc" ||
|
||||||
watchedType === "websocket") && (
|
watchedType === "websocket") && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
@@ -1027,7 +1084,7 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{watchedType === "http" && (
|
{showStep(2) && watchedType === "http" && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.advanced.title")}
|
title={t("pages.createMonitor.form.advanced.title")}
|
||||||
subtitle={t("pages.createMonitor.form.advanced.description")}
|
subtitle={t("pages.createMonitor.form.advanced.description")}
|
||||||
@@ -1142,7 +1199,7 @@ const CreateMonitorPage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{supportsGeoCheck(watchedType) && (
|
{showStep(2) && supportsGeoCheck(watchedType) && (
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.createMonitor.form.geoChecks.title")}
|
title={t("pages.createMonitor.form.geoChecks.title")}
|
||||||
subtitle={t("pages.createMonitor.form.geoChecks.description")}
|
subtitle={t("pages.createMonitor.form.geoChecks.description")}
|
||||||
@@ -1278,9 +1335,32 @@ const CreateMonitorPage = () => {
|
|||||||
|
|
||||||
<Stack
|
<Stack
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="flex-end"
|
justifyContent={showTypeSelector ? "space-between" : "flex-end"}
|
||||||
>
|
>
|
||||||
|
{showTypeSelector && (
|
||||||
<Button
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outlined"
|
||||||
|
color="secondary"
|
||||||
|
disabled={currentStep === 0}
|
||||||
|
onClick={handleBack}
|
||||||
|
>
|
||||||
|
{t("common.buttons.back")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{showTypeSelector && currentStep < totalSteps - 1 ? (
|
||||||
|
<Button
|
||||||
|
key="wizard-next"
|
||||||
|
type="button"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
onClick={handleNext}
|
||||||
|
>
|
||||||
|
{t("common.buttons.next")}
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
key="wizard-save"
|
||||||
loading={isSubmitting}
|
loading={isSubmitting}
|
||||||
type="submit"
|
type="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
@@ -1288,6 +1368,7 @@ const CreateMonitorPage = () => {
|
|||||||
>
|
>
|
||||||
{t("common.buttons.save")}
|
{t("common.buttons.save")}
|
||||||
</Button>
|
</Button>
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
<Dialog
|
<Dialog
|
||||||
open={isDeleteDialogOpen}
|
open={isDeleteDialogOpen}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import type { FieldPath } from "react-hook-form";
|
||||||
import { GeoContinents } from "@/Types/GeoCheck";
|
import { GeoContinents } from "@/Types/GeoCheck";
|
||||||
import { DnsRecordTypes, PageSpeedStrategies } from "@/Types/Monitor";
|
import { DnsRecordTypes, PageSpeedStrategies, type MonitorType } from "@/Types/Monitor";
|
||||||
|
|
||||||
|
// Wizard step a field is validated on. Attached inline to each field below so
|
||||||
|
// the grouping lives next to the field definition; unannotated fields default
|
||||||
|
// to step 0. Read via `stepFieldsFor`.
|
||||||
|
export const monitorStepRegistry = z.registry<{ step: number }>();
|
||||||
|
|
||||||
// URL schema with custom error message
|
// URL schema with custom error message
|
||||||
const urlSchema = z.url({ message: "Please enter a valid URL" });
|
const urlSchema = z.url({ message: "Please enter a valid URL" });
|
||||||
@@ -12,40 +18,60 @@ const baseSchema = z.object({
|
|||||||
.min(1, "Monitor name is required")
|
.min(1, "Monitor name is required")
|
||||||
.max(50, "Monitor name must be at most 50 characters"),
|
.max(50, "Monitor name must be at most 50 characters"),
|
||||||
description: z.string().optional(),
|
description: z.string().optional(),
|
||||||
interval: z.number().min(15000, "Interval must be at least 15 seconds"),
|
interval: z
|
||||||
notifications: z.array(z.string()),
|
.number()
|
||||||
tags: z.array(z.string()),
|
.min(15000, "Interval must be at least 15 seconds")
|
||||||
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
|
notifications: z.array(z.string()).register(monitorStepRegistry, { step: 1 }),
|
||||||
|
tags: z.array(z.string()).register(monitorStepRegistry, { step: 1 }),
|
||||||
statusWindowSize: z
|
statusWindowSize: z
|
||||||
.number({ message: "Status window size is required" })
|
.number({ message: "Status window size is required" })
|
||||||
.min(1, "Status window size must be at least 1")
|
.min(1, "Status window size must be at least 1")
|
||||||
.max(25, "Status window size must be at most 25"),
|
.max(25, "Status window size must be at most 25")
|
||||||
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
statusWindowThreshold: z
|
statusWindowThreshold: z
|
||||||
.number({ message: "Threshold percentage is required" })
|
.number({ message: "Threshold percentage is required" })
|
||||||
.min(1, "Incident percentage must be at least 1")
|
.min(1, "Incident percentage must be at least 1")
|
||||||
.max(100, "Incident percentage must be at most 100"),
|
.max(100, "Incident percentage must be at most 100")
|
||||||
geoCheckEnabled: z.boolean().optional(),
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
geoCheckLocations: z.array(z.enum(GeoContinents)).optional(),
|
});
|
||||||
|
|
||||||
|
// Geo-distributed check fields. Only http and ping support geo checks
|
||||||
|
// (GeoCheckSupportedTypes), so these live on those schemas rather than the base
|
||||||
|
// — that way each type's step count is exactly what its own fields declare.
|
||||||
|
const geoCheckFields = {
|
||||||
|
geoCheckEnabled: z.boolean().optional().register(monitorStepRegistry, { step: 2 }),
|
||||||
|
geoCheckLocations: z
|
||||||
|
.array(z.enum(GeoContinents))
|
||||||
|
.optional()
|
||||||
|
.register(monitorStepRegistry, { step: 2 }),
|
||||||
geoCheckInterval: z
|
geoCheckInterval: z
|
||||||
.number()
|
.number()
|
||||||
.min(300000, "Interval must be at least 5 minutes")
|
.min(300000, "Interval must be at least 5 minutes")
|
||||||
.optional(),
|
.optional()
|
||||||
});
|
.register(monitorStepRegistry, { step: 2 }),
|
||||||
|
};
|
||||||
|
|
||||||
// HTTP monitor schema
|
// HTTP monitor schema
|
||||||
const httpSchema = baseSchema.extend({
|
const httpSchema = baseSchema.extend({
|
||||||
type: z.literal("http"),
|
type: z.literal("http"),
|
||||||
url: urlSchema,
|
url: urlSchema,
|
||||||
ignoreTlsErrors: z.boolean(),
|
ignoreTlsErrors: z.boolean().register(monitorStepRegistry, { step: 2 }),
|
||||||
useAdvancedMatching: z.boolean(),
|
useAdvancedMatching: z.boolean().register(monitorStepRegistry, { step: 2 }),
|
||||||
matchMethod: z.enum(["equal", "include", "regex", ""]).optional(),
|
matchMethod: z
|
||||||
expectedValue: z.string().optional(),
|
.enum(["equal", "include", "regex", ""])
|
||||||
jsonPath: z.string().optional(),
|
.optional()
|
||||||
|
.register(monitorStepRegistry, { step: 2 }),
|
||||||
|
expectedValue: z.string().optional().register(monitorStepRegistry, { step: 2 }),
|
||||||
|
jsonPath: z.string().optional().register(monitorStepRegistry, { step: 2 }),
|
||||||
|
...geoCheckFields,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ping monitor schema
|
// Ping monitor schema
|
||||||
const pingSchema = baseSchema.extend({
|
const pingSchema = baseSchema.extend({
|
||||||
type: z.literal("ping"),
|
type: z.literal("ping"),
|
||||||
url: z.string().min(1, "Host is required"),
|
url: z.string().min(1, "Host is required"),
|
||||||
|
...geoCheckFields,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Port monitor schema
|
// Port monitor schema
|
||||||
@@ -84,7 +110,7 @@ const grpcSchema = baseSchema.extend({
|
|||||||
.min(1, "Port must be at least 1")
|
.min(1, "Port must be at least 1")
|
||||||
.max(65535, "Port must be at most 65535"),
|
.max(65535, "Port must be at most 65535"),
|
||||||
grpcServiceName: z.string().optional(),
|
grpcServiceName: z.string().optional(),
|
||||||
ignoreTlsErrors: z.boolean(),
|
ignoreTlsErrors: z.boolean().register(monitorStepRegistry, { step: 2 }),
|
||||||
});
|
});
|
||||||
|
|
||||||
// PageSpeed monitor schema
|
// PageSpeed monitor schema
|
||||||
@@ -122,7 +148,7 @@ const hardwareSchema = baseSchema.extend({
|
|||||||
const websocketSchema = baseSchema.extend({
|
const websocketSchema = baseSchema.extend({
|
||||||
type: z.literal("websocket"),
|
type: z.literal("websocket"),
|
||||||
url: z.string().min(1, "WebSocket URL is required"),
|
url: z.string().min(1, "WebSocket URL is required"),
|
||||||
ignoreTlsErrors: z.boolean(),
|
ignoreTlsErrors: z.boolean().register(monitorStepRegistry, { step: 2 }),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Hostname (FQDN) — labels of 1-63 alphanumerics/hyphens separated by dots,
|
// Hostname (FQDN) — labels of 1-63 alphanumerics/hyphens separated by dots,
|
||||||
@@ -164,6 +190,34 @@ export const monitorSchema = z.discriminatedUnion("type", [
|
|||||||
|
|
||||||
export type MonitorFormData = z.infer<typeof monitorSchema>;
|
export type MonitorFormData = z.infer<typeof monitorSchema>;
|
||||||
|
|
||||||
|
const optionForType = (type: MonitorType) =>
|
||||||
|
monitorSchema.options.find((o) => o.shape.type.value === type);
|
||||||
|
|
||||||
|
// The step each field of a type is validated on, from the per-field
|
||||||
|
// `monitorStepRegistry` metadata (unannotated fields default to step 0).
|
||||||
|
const fieldStepsFor = (
|
||||||
|
type: MonitorType
|
||||||
|
): [name: FieldPath<MonitorFormData>, step: number][] =>
|
||||||
|
Object.entries(optionForType(type)?.shape ?? {}).map(([name, field]) => [
|
||||||
|
name as FieldPath<MonitorFormData>,
|
||||||
|
monitorStepRegistry.get(field)?.step ?? 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// The selected type's fields that belong to a given wizard step. Derived from
|
||||||
|
// the schema so it can't drift from the field definitions.
|
||||||
|
export const stepFieldsFor = (
|
||||||
|
type: MonitorType,
|
||||||
|
step: number
|
||||||
|
): FieldPath<MonitorFormData>[] =>
|
||||||
|
fieldStepsFor(type)
|
||||||
|
.filter(([, fieldStep]) => fieldStep === step)
|
||||||
|
.map(([name]) => name);
|
||||||
|
|
||||||
|
// Number of wizard steps a type has, derived from the highest step its fields
|
||||||
|
// declare. A type with no step-2 fields is a 2-step form, and so on.
|
||||||
|
export const monitorStepCount = (type: MonitorType): number =>
|
||||||
|
Math.max(0, ...fieldStepsFor(type).map(([, step]) => step)) + 1;
|
||||||
|
|
||||||
// Type-specific schemas exported for individual use
|
// Type-specific schemas exported for individual use
|
||||||
export {
|
export {
|
||||||
httpSchema,
|
httpSchema,
|
||||||
|
|||||||
@@ -43,6 +43,8 @@
|
|||||||
"pause": "Pause",
|
"pause": "Pause",
|
||||||
"resume": "Resume",
|
"resume": "Resume",
|
||||||
"save": "Save",
|
"save": "Save",
|
||||||
|
"next": "Next",
|
||||||
|
"back": "Back",
|
||||||
"sendInvite": "Send invite",
|
"sendInvite": "Send invite",
|
||||||
"test": "Test",
|
"test": "Test",
|
||||||
"testNotifications": "Test notifications",
|
"testNotifications": "Test notifications",
|
||||||
|
|||||||
Reference in new issue
Block a user