import { type FormEvent, useState } from 'react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Switch } from '@/components/ui/switch'; import type { Monitor, MonitorInput, MonitorMethod } from '../../api/monitors'; import { useCreateMonitorMutation, useUpdateMonitorMutation } from '../../queries/monitors'; export const DEFAULT_MONITOR_INPUT: MonitorInput = { name: '', url: 'https://', method: 'GET', expectedStatus: 200, intervalSeconds: 300, timeoutMs: 10_000, enabled: true, }; export const INTERVAL_OPTIONS = [ { value: '300', label: '5 minutes' }, { value: '900', label: '15 minutes' }, { value: '1800', label: '30 minutes' }, { value: '3600', label: '1 hour' }, { value: '86400', label: '24 hours' }, ]; type MonitorFormDialogProps = { editing: Monitor | null; onClose: () => void; }; function errorMessage(error: unknown, fallback: string) { return error instanceof Error ? error.message : fallback; } function monitorInput(monitor: Monitor | null): MonitorInput { if (!monitor) return DEFAULT_MONITOR_INPUT; return { name: monitor.name, url: monitor.url, method: monitor.method, expectedStatus: monitor.expectedStatus, intervalSeconds: monitor.intervalSeconds, timeoutMs: monitor.timeoutMs, enabled: monitor.enabled, alertsEnabled: monitor.alertsEnabled, }; } export function MonitorFormDialog({ editing, onClose }: MonitorFormDialogProps) { const createMutation = useCreateMonitorMutation(); const updateMutation = useUpdateMonitorMutation(); const [form, setForm] = useState(() => monitorInput(editing)); const formMutation = editing ? updateMutation : createMutation; function closeForm() { if (formMutation.isPending) return; onClose(); } function handleSubmit(event: FormEvent) { event.preventDefault(); if (editing) { updateMutation.mutate({ id: editing.id, input: form }, { onSuccess: onClose }); } else { createMutation.mutate(form, { onSuccess: onClose }); } } return ( { if (!nextOpen) closeForm(); }} > { if (formMutation.isPending) event.preventDefault(); }} onInteractOutside={(event) => { if (formMutation.isPending) event.preventDefault(); }} >

Configuration

{editing ? `Edit ${editing.name}` : 'Add a monitor'} Checks run on the configured schedule, with a minimum interval of five minutes.
Method
Interval
setForm({ ...form, enabled })} />
setForm({ ...form, alertsEnabled })} />
{formMutation.isError && (

{errorMessage(formMutation.error, 'Unable to save monitor')}

)}
); }