"use client"; import { useMemo, useState } from "react"; import type { Entity } from "@/uhm/types/entities"; import type { WikiSnapshot } from "@/uhm/types/wiki"; import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections"; type EntityChoice = { id: string; name: string }; type WikiChoice = { id: string; title: string; operation?: string }; type Props = { entities: EntityChoice[]; wikis: WikiSnapshot[]; links: EntityWikiLinkSnapshot[]; setLinks: React.Dispatch>; }; function wikiTitle(w: WikiSnapshot): string { const t = String(w.title || "").trim(); return t.length ? t : "Untitled wiki"; } export default function EntityWikiBindingsPanel({ entities, wikis, links, setLinks }: Props) { const [activeEntityId, setActiveEntityId] = useState(""); const wikiChoices: WikiChoice[] = useMemo( () => (wikis || []) .filter((w) => w && typeof w.id === "string" && w.id.trim().length > 0) .map((w) => ({ id: w.id, title: wikiTitle(w), operation: w.operation })), [wikis] ); const entityChoices = useMemo(() => { const cleaned = (entities || []).filter((e) => e && typeof e.id === "string" && e.id.trim().length > 0); cleaned.sort((a, b) => a.name.localeCompare(b.name)); return cleaned; }, [entities]); const activeLinks = useMemo(() => { const set = new Set(); for (const l of links || []) { if (!l || l.entity_id !== activeEntityId) continue; if (l.operation === "delete") continue; set.add(l.wiki_id); } return set; }, [activeEntityId, links]); const toggle = (wikiId: string) => { if (!activeEntityId) return; const id = String(wikiId || "").trim(); if (!id) return; setLinks((prev) => { const next = [...prev]; const idx = next.findIndex((l) => l.entity_id === activeEntityId && l.wiki_id === id); if (idx >= 0) { const existing = next[idx]; const currentlyOn = existing.operation !== "delete"; next[idx] = { ...existing, operation: currentlyOn ? "delete" : "reference", }; return next; } next.push({ entity_id: activeEntityId, wiki_id: id, operation: "reference", }); return next; }); }; return (
Entity ↔ Wiki
{links.length}
Entity
Wikis
{!wikiChoices.length ? (
No wiki in project yet.
) : !activeEntityId ? (
Pick an entity to bind wikis.
) : (
{wikiChoices.slice(0, 12).map((w) => { const checked = activeLinks.has(w.id); const isRefWiki = wikis.find((x) => x.id === w.id)?.source === "ref"; return ( ); })} {wikiChoices.length > 12 ? (
+{wikiChoices.length - 12} more…
) : null}
)}
); }