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"; 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), 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("Name is required"), undefined; if (type !== "push" && type !== "tcp" && type !== "ping" && type !== "dns" && !f.url?.trim()) return setErr("URL is required"), undefined; if ((type === "tcp" || type === "ping" || type === "dns") && !f.hostname?.trim() && !f.url?.trim()) return setErr("Hostname is required"), 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 ?? "", }; 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("save failed"); } finally { setBusy(false); } } return ( {editing ? "Edit monitor" : "New monitor"} Uptime check on a schedule.
{(type === "http" || type === "keyword" || type === "json") && ( <>
{type === "keyword" && (
)} {type === "json" && (
)} )} {(type === "tcp" || type === "ping" || type === "dns") && (
{type === "tcp" ? (
) : type === "dns" ? (
) :
}
)} {type === "dns" && (
)} {type === "push" && (

After saving, your jobs call the shown push URL on a schedule. If it goes quiet for 2× the interval, the monitor flips down.

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

{err}

: null}
); }