import { memo, useCallback, useEffect, useRef, useState } from "react"; import { Loader } from "../components/Loading.jsx"; import { Select } from "../components/Select.jsx"; import { KIND_LABEL, fmt, kindWords, relationWords, typeWords } from "../lib/constants.js"; import { useData } from "../lib/data.jsx"; import { useUi } from "../lib/ui.jsx"; import { CardHead, Toggle } from "../components/common.jsx"; import { Page } from "./Page.jsx"; import { tr, trx } from "../lib/i18n.js"; const VIEWS = ["network", "schema", "event"]; // rev changes when the feed gains events, so an event view refreshes as events arrive. function orbSrc(orb, rev) { const p = new URLSearchParams({ view: orb.view }); if (orb.view === "event") { p.set("event", orb.event || "all"); if (orb.run) p.set("run", orb.run); p.set("rev", rev); } return `/orb/?embed=1&bg=0b1437&data=${encodeURIComponent(`/api/knowledge_graph?${p}`)}`; } // Only reloads when its src changes, so typing in search or refreshing data leaves the graph alone. // The orb page posts {source: "orb", type: "ready" | "error"} when its layout is done. A new view shows // a loader until then (and gives up waiting after 20 s, in case a message is lost). A refresh of the // same view (new events landed) loads behind the current graph and swaps in when ready: no flash. // shown: this view is the one on screen. Hidden views stay loaded (switching back is instant) but are told // to stop drawing, as is any graph scrolled out of sight. onReady: the newest graph finished its layout. const OrbFrame = memo(function OrbFrame({ src, view, shown = true, onReady }) { const [frames, setFrames] = useState([{ src, view, state: "loading" }]); const refs = useRef({}); useEffect(() => { setFrames((fs) => { const live = fs[fs.length - 1]; if (live.src === src) return fs; // Same view with more data: keep the shown graph until the new one is ready. const keep = live.view === view && live.state === "ready" ? [live] : []; return [...keep, { src, view, state: "loading" }]; }); }, [src, view]); useEffect(() => { const on = (e) => { if (e.data?.source !== "orb") return; const hit = Object.entries(refs.current).find(([, el]) => el?.contentWindow === e.source)?.[0]; if (!hit) return; setFrames((fs) => { const next = fs.map((f) => (f.src === hit ? { ...f, state: e.data.type === "error" ? "error" : "ready" } : f)); const newest = next[next.length - 1]; return newest.state === "loading" ? next : [newest]; // the new graph is up: drop the old one }); }; window.addEventListener("message", on); return () => window.removeEventListener("message", on); }, []); useEffect(() => { const timer = setTimeout(() => setFrames((fs) => [{ ...fs[fs.length - 1], state: "ready" }]), 20000); return () => clearTimeout(timer); }, [src]); // Tell each graph how much of its bottom is hidden below the window, so its zoom buttons stay on // screen (bottom-right of what you can see) while the graph is taller than the window. const wrap = useRef(null); const shownRef = useRef(shown); shownRef.current = shown; const sendRef = useRef(() => {}); useEffect(() => { let raf = 0, onScreen = true; const send = () => { raf = 0; const box = wrap.current?.getBoundingClientRect(); if (!box) return; const bottom = Math.max(0, box.bottom - window.innerHeight); const on = shownRef.current && onScreen; for (const el of Object.values(refs.current)) { try { el?.contentWindow?.postMessage({ source: "dashboard", type: "inset", bottom }, location.origin); el?.contentWindow?.postMessage({ source: "dashboard", type: "active", on }, location.origin); } catch { /* frame gone */ } } }; sendRef.current = send; // Stop drawing while the graph is scrolled out of sight. const seen = new IntersectionObserver(([entry]) => { onScreen = entry.isIntersecting; send(); }); if (wrap.current) seen.observe(wrap.current); const soon = () => { if (!raf) raf = requestAnimationFrame(send); }; window.addEventListener("scroll", soon, { passive: true }); window.addEventListener("resize", soon); soon(); const timer = setInterval(send, 1000); // a new frame finishing its load also needs the number return () => { seen.disconnect(); window.removeEventListener("scroll", soon); window.removeEventListener("resize", soon); clearInterval(timer); if (raf) cancelAnimationFrame(raf); }; }, []); useEffect(() => { sendRef.current(); }, [shown]); const newest = frames[frames.length - 1]; useEffect(() => { if (newest.state !== "loading") onReady?.(); }, [newest.state]); // eslint-disable-line react-hooks/exhaustive-deps const covering = newest.state === "loading" && frames.length === 1; return (
{frames.map((f, i) => (