"use client";
import { ScaleIcon } from "lucide-react";
import { useState, type FormEvent } from "react";
import { toast } from "sonner";
import { EmptyState, ErrorState, PageHeader } from "@/components/shell/page";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Skeleton } from "@/components/ui/skeleton";
import { Textarea } from "@/components/ui/textarea";
import { api, ApiError } from "@/lib/api";
import { useAuth } from "@/lib/auth";
import { useI18n } from "@/lib/i18n";
import { useApi } from "@/lib/use-api";
import { cn } from "@/lib/utils";
import type { PolicyRuleOut } from "./types";
interface CompileError {
message: string;
errors?: string[];
question?: string | null;
}
function compileError(err: unknown): CompileError | null {
if (!(err instanceof ApiError)) return null;
const d = (err.body as { detail?: unknown } | undefined)?.detail;
if (d && typeof d === "object" && "message" in d) return d as CompileError;
return { message: err.detail };
}
function RuleCard({ rule, children }: { rule: PolicyRuleOut; children?: React.ReactNode }) {
const { t, m } = useI18n();
return (
{(m.nlrules.statuses as Record)[rule.status] ?? rule.status}
{t("nlrules.version", { n: rule.version })}
“{rule.text}”
{rule.plain}
{children}
);
}
function TestsTable({ rule }: { rule: PolicyRuleOut }) {
const { t } = useI18n();
return (
{t("nlrules.tests")}
| {t("nlrules.call")} |
{t("nlrules.actor")} |
{t("nlrules.outcome")} |
{rule.tests.map((x, i) => (
|
{x.tool}({Object.entries(x.args).map(([k, v]) => `${k}=${JSON.stringify(v)}`).join(", ")})
|
{x.actor} |
{x.matched ? `${t("nlrules.matched")}: ${x.outcome}` : t("nlrules.notMatched")}
|
))}
);
}
export function RulesView() {
const { t } = useI18n();
const { hasRole } = useAuth();
const owner = hasRole("owner");
const rules = useApi("/ext/rules");
const [text, setText] = useState("");
const [draft, setDraft] = useState(null);
const [problem, setProblem] = useState(null);
const [allowPermit, setAllowPermit] = useState(false);
const [busy, setBusy] = useState(false);
const compile = async (e: FormEvent) => {
e.preventDefault();
setBusy(true);
setProblem(null);
setDraft(null);
try {
setDraft(await api("/ext/rules/compile", { method: "POST", body: { text } }));
} catch (err) {
setProblem(compileError(err) ?? { message: t("common.error") });
} finally {
setBusy(false);
}
};
const confirm = async () => {
if (!draft) return;
try {
await api(`/ext/rules/${draft.id}/confirm`, { method: "POST", body: { allow_permit: allowPermit } });
toast.success(t("nlrules.confirmed"));
setDraft(null);
setText("");
rules.reload();
} catch (err) {
toast.error(compileError(err)?.message ?? t("common.error"));
}
};
const act = async (rule: PolicyRuleOut, action: "rollback" | "disable") => {
try {
await api(`/ext/rules/${rule.rule_key}/${action}`, { method: "POST" });
toast.success(action === "rollback" ? t("nlrules.rolledBack") : t("nlrules.disabledToast"));
rules.reload();
} catch (err) {
toast.error(compileError(err)?.message ?? t("common.error"));
}
};
const active = rules.data?.filter((r) => r.status === "active") ?? [];
const history = rules.data?.filter((r) => r.status !== "active" && r.status !== "draft") ?? [];
return (
{problem && (
{problem.question ? (
<>
{t("nlrules.question")}
{problem.question}
>
) : (
<>
{problem.message}
{problem.errors?.map((e) => (
{e}
))}
>
)}
)}
{draft && (
{t("nlrules.understood")}
{t("nlrules.policy")}
{draft.cedar}
{owner ? (
{draft.ast.effect === "permit" && (
)}
) : (
{t("nlrules.ownerOnly")}
)}
)}
{t("nlrules.active")}
{rules.error && }
{!rules.data && !rules.error && }
{rules.data && active.length === 0 && }
{active.map((r) => (
{owner && (
{r.version > 1 && (
)}
)}
))}
{history.length > 0 && (
{t("nlrules.history")}
{history.map((r) => (
-
{r.status} {t("nlrules.version", { n: r.version })} — {r.plain}
))}
)}
);
}