"use client";
import { useCallback, useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { buttonVariants } from "@/components/ui/button";
// THE PROJECT'S LINK POLICY (UI notes 2026-10-30, #10): "cite via site X;
// these channels direct" -- `provenance.linkPolicy`, through the one writer
// (PUT /api/report/link-policy; `umtool link-policy
` from a terminal).
// New clips are written by it; the clips already in the cut that break it are
// the check's clip-link-policy rows, each with its one-press fix.
type View = {
policy: { via: string | null; direct: string[] } | null;
siteOrigin: string | null;
channels: { slug: string; clips: number }[];
sites: { origin: string; siteId: string; title: string | null }[];
token: string | null;
};
const DEFAULT = "";
export default function LinkPolicyPanel({ project }: { project: string }) {
const router = useRouter();
const [view, setView] = useState(null);
const [via, setVia] = useState(DEFAULT);
const [direct, setDirect] = useState([]);
const [busy, setBusy] = useState(false);
const [said, setSaid] = useState(null);
const [err, setErr] = useState(null);
const load = useCallback(async () => {
const r = await fetch(`/api/report/link-policy?project=${encodeURIComponent(project)}`, { cache: "no-store" });
const j = (await r.json().catch(() => null)) as View | null;
if (!r.ok || !j) return;
setView(j);
setVia(j.policy?.via ?? DEFAULT);
setDirect(j.policy?.direct ?? []);
}, [project]);
useEffect(() => {
void load();
}, [load]);
if (!view || !view.channels.length) return null;
const options = [...view.sites];
if (view.policy?.via && !options.some((s) => s.origin === view.policy?.via)) {
options.push({ origin: view.policy.via, siteId: new URL(view.policy.via).host, title: null });
}
const dirty = via !== (view.policy?.via ?? DEFAULT) || direct.join(",") !== (view.policy?.direct ?? []).join(",");
const save = async () => {
setBusy(true);
setErr(null);
setSaid(null);
try {
const policy = via || direct.length ? { ...(via ? { via } : {}), ...(direct.length ? { direct } : {}) } : null;
const r = await fetch("/api/report/link-policy", {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ project, token: view.token, policy }),
});
const j = (await r.json().catch(() => ({}))) as { error?: string; changed?: boolean };
if (!r.ok) {
setErr(j.error ?? `HTTP ${r.status}`);
return;
}
setSaid(j.changed ? "saved — the check names every clip that breaks it" : "unchanged");
await load();
router.refresh();
} catch (e) {
setErr(e instanceof Error ? e.message : String(e));
} finally {
setBusy(false);
}
};
const defaultHost = (() => {
try {
return view.siteOrigin ? new URL(view.siteOrigin).host : null;
} catch {
return view.siteOrigin;
}
})();
return (
Link policy
These channels link their platform’s own page directly: