"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import LinkEntityModal from "@/components/LinkEntityModal"; import { text } from "@/lib/format"; import type { YsterbaardDirector } from "@/lib/types"; interface JunctionEntry { id: string; director_id: string; director: YsterbaardDirector; } interface Props { companyId: string; companyName: string; initial: JunctionEntry[]; } export default function LinkedDirectorsList({ companyId, companyName, initial }: Props) { const router = useRouter(); const [showLinkModal, setShowLinkModal] = useState(false); const [unlinking, setUnlinking] = useState(null); const [linked, setLinked] = useState(initial); const [error, setError] = useState(null); const alreadyLinkedIds = linked.map((j) => j.director_id); async function handleUnlink(junctionId: string, directorName: string) { if (!confirm(`Remove link to "${directorName}"? The director will remain in PocketBase.`)) return; setUnlinking(junctionId); setError(null); try { const res = await fetch("/api/pb/records/link", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "unlink", company: companyId, director: linked.find((j) => j.id === junctionId)?.director_id, }), }); if (!res.ok) { const err = await res.json(); throw new Error(err.error ?? "Unlink failed"); } setLinked((prev) => prev.filter((j) => j.id !== junctionId)); window.location.reload(); } catch (e: unknown) { setError(e instanceof Error ? e.message : "Unlink failed"); } finally { setUnlinking(null); } } return ( <>

Linked directors

{linked.length} director{linked.length === 1 ? "" : "s"}

{error && (
{error}
)} {linked.length === 0 ? (
No directors linked to this company.
) : (
{linked.map((j) => { const d = j.director; const name = `${text(d.first_names)} ${text(d.surname)}`.trim(); return (

{name || "—"}

ID {text(d.id_no)}

View director profile →
); })}
)}
setShowLinkModal(false)} onLinked={() => router.refresh()} /> ); }