import { useEffect, useState } from "react"; import { api } from "@/api"; import type { Monitor, MonitorInput } from "@/types"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { t } from "@/i18n"; const TYPES = ["http", "tcp", "ping", "dns", "keyword", "json", "push"] as const; type MonType = (typeof TYPES)[number]; // One dialog for add + edit. The type picker drives which fields matter — // same pattern as ServiceDialog. export function MonitorDialog({ open, onOpenChange, monitor, onSaved, }: { open: boolean; onOpenChange: (o: boolean) => void; monitor?: Monitor | undefined; onSaved: () => void; }) { const editing = Boolean(monitor); const [f, setF] = useState>({}); const [busy, setBusy] = useState(false); const [err, setErr] = useState(""); useEffect(() => { if (!open) return; setErr(""); setF({ name: monitor?.name ?? "", type: monitor?.type ?? "http", url: monitor?.url ?? "", hostname: monitor?.hostname ?? "", port: monitor?.port ? String(monitor.port) : "", method: monitor?.method ?? "GET", keyword: monitor?.keyword ?? "", keywordInvert: monitor?.keywordInvert ? "1" : "", jsonQuery: monitor?.jsonQuery ?? "", expected: monitor?.expected ?? "", dnsType: monitor?.dnsType ?? "A", intervalS: String(monitor?.intervalS ?? 60), timeoutS: String(monitor?.timeoutS ?? 10), retries: String(monitor?.retries ?? 0), consecFails: String(monitor?.alerts?.consecutiveFailures ?? ""), latencyWarn: String(monitor?.alerts?.latencyWarnMs ?? ""), mute: monitor?.alerts?.mute ? "1" : "", notes: monitor?.notes ?? "", }); }, [open, monitor]); const set = (k: string) => (e: React.ChangeEvent) => setF((s) => ({ ...s, [k]: e.target.value })); // SAFETY: f.type only ever receives values from the TYPES select or a // persisted monitor row, both constrained to MonType. const type = (f.type || "http") as MonType; async function submit() { if (!f.name?.trim()) return setErr(t("monitors.nameRequired")), undefined; if (type !== "push" && type !== "tcp" && type !== "ping" && type !== "dns" && !f.url?.trim()) return setErr(t("monitors.urlRequired")), undefined; if ((type === "tcp" || type === "ping" || type === "dns") && !f.hostname?.trim() && !f.url?.trim()) return setErr(t("monitors.hostnameRequired")), undefined; setBusy(true); const body: MonitorInput = { name: f.name.trim(), type, url: f.url?.trim() ?? "", hostname: f.hostname?.trim() ?? "", port: parseInt(f.port ?? "0") || 0, method: f.method || "GET", keyword: f.keyword ?? "", keywordInvert: f.keywordInvert === "1", jsonQuery: f.jsonQuery ?? "", expected: f.expected ?? "", dnsType: f.dnsType || "A", intervalS: Math.max(10, parseInt(f.intervalS ?? "60") || 60), timeoutS: Math.max(1, parseInt(f.timeoutS ?? "10") || 10), retries: Math.max(0, parseInt(f.retries ?? "0") || 0), notes: f.notes ?? "", alerts: { consecutiveFailures: Math.max(1, parseInt(f.consecFails ?? "") || 1), latencyWarnMs: Math.max(0, parseInt(f.latencyWarn ?? "") || 0), mute: f.mute === "1", }, }; try { if (editing && monitor) { await api.PATCH("/api/monitors/{id}", { params: { path: { id: monitor.id! } }, body }); } else { await api.POST("/api/monitors", { body }); } onSaved(); onOpenChange(false); } catch { setErr(t("monitors.saveFailed")); } finally { setBusy(false); } } return ( {editing ? t("monitors.editTitle") : t("monitors.newTitle")} {t("monitors.desc")}
{(type === "http" || type === "keyword" || type === "json") && ( <>
{type === "keyword" && (
)} {type === "json" && (
)} )} {(type === "tcp" || type === "ping" || type === "dns") && (
{type === "tcp" ? (
) : type === "dns" ? (
) :
}
)} {type === "dns" && (
)} {type === "push" && (

{t("monitors.pushHint")}

)}
{monitor?.pushToken ? (
{location.origin}/api/push/{monitor.pushToken}
) : null} {err ?

{err}

: null}
); }