"use client"; import { useState } from "react"; import useSWR from "swr"; import AppShell from "@/components/AppShell"; import { PageHeader } from "@/components/ui"; import { api, fetcher } from "@/lib/api"; import { useAuth } from "@/lib/auth"; type RulesResponse = { yaml: string; parsed: { rules?: { id: string; name: string; priority?: number }[] }; path_hint: string; }; export default function RulesPage() { const { user } = useAuth(); const isAdmin = user?.role === "admin"; const { data, mutate } = useSWR("/api/rules/troubleshooting", fetcher); const [yaml, setYaml] = useState(null); const [previewTitle, setPreviewTitle] = useState("Asterisk health status"); const [previewIssue, setPreviewIssue] = useState("Check voip pjsip registration on GeneseasX"); const [preview, setPreview] = useState<{ matched: Record | null } | null>(null); const [saving, setSaving] = useState(false); const [msg, setMsg] = useState(null); const content = yaml ?? data?.yaml ?? ""; const [msgKind, setMsgKind] = useState<"ok" | "err">("ok"); const save = async () => { setSaving(true); setMsg(null); try { await api("/api/rules/troubleshooting", { method: "PUT", body: JSON.stringify({ yaml: content }) }); setMsgKind("ok"); setMsg("Rules saved — next task run will use them."); setYaml(null); mutate(); } catch (err: unknown) { setMsgKind("err"); setMsg(err instanceof Error ? err.message : "Save failed"); } finally { setSaving(false); } }; const runPreview = async () => { setPreview(null); try { const res = await api<{ matched: Record | null }>("/api/rules/troubleshooting/preview", { method: "POST", body: JSON.stringify({ title: previewTitle, issue: previewIssue }), }); setPreview(res); } catch (err: unknown) { setMsg(err instanceof Error ? err.message : "Preview failed"); } }; return (

Rule preview

Test which rule matches a task title + issue.

setPreviewTitle(e.target.value)} placeholder="Task title" />