import { cn } from '@szl-holdings/shared-ui/utils'; import { useEffect, useMemo, useRef, useState } from 'react'; import { ENTITY_TYPE_CONFIG, TIER_CONFIG, type EntityNode, type EntityNodeType, type RelationshipEdge, } from '@/data/sanctions-network-data'; interface SimNode extends EntityNode { x: number; y: number; vx: number; vy: number; r: number; fixed?: boolean; } interface Props { nodes: EntityNode[]; edges: RelationshipEdge[]; selectedId: string | null; onSelect: (node: EntityNode | null) => void; height?: number; } function radiusFor(n: EntityNode): number { switch (n.type) { case 'vessel': return 18; case 'beneficial_owner': return 14; case 'registered_owner': return 13; case 'ship_manager': return 12; case 'counterparty': return 12; case 'charterer': return 11; case 'insurer': return 10; case 'flag_state': return 10; case 'port_agent': return 9; default: return 10; } } function nodeColor(n: EntityNode): string { if (n.sanctioned) return '#ef4444'; return ENTITY_TYPE_CONFIG[n.type]?.color ?? 'var(--gi-text-muted)'; } function tierRingColor(tier: EntityNode['riskTier']): string { return TIER_CONFIG[tier]?.dot ?? 'var(--gi-text-muted)'; } const SANCTION_LIST_ABBR: Record = { OFAC_SDN: 'SDN', EU_CONSOLIDATED: 'EU', UK_OFSI: 'OFSI', UN_SECURITY_COUNCIL: 'UN', }; export function DarkFleetNetworkGraph({ nodes, edges, selectedId, onSelect, height = 480, }: Props) { const wrapRef = useRef(null); const simRef = useRef([]); const alphaRef = useRef(1); const rafRef = useRef(null); const [width, setWidth] = useState(800); const [, force] = useState(0); const [transform, setTransform] = useState({ k: 1, tx: 0, ty: 0 }); const [hoverNode, setHoverNode] = useState(null); const [tooltip, setTooltip] = useState<{ node: EntityNode; x: number; y: number } | null>(null); const dragRef = useRef<{ id: string; pointerId: number } | null>(null); const panRef = useRef<{ x: number; y: number; tx: number; ty: number } | null>(null); const visibleEdges = useMemo(() => { const ids = new Set(nodes.map((n) => n.id)); return edges.filter((e) => ids.has(e.source) && ids.has(e.target)); }, [nodes, edges]); useEffect(() => { if (!wrapRef.current) return; const ro = new ResizeObserver((entries) => { for (const e of entries) setWidth(e.contentRect.width); }); ro.observe(wrapRef.current); return () => ro.disconnect(); }, []); useEffect(() => { const cx = width / 2; const cy = height / 2; const ring = Math.min(width, height) * 0.32; const prev = new Map(simRef.current.map((n) => [n.id, n] as const)); simRef.current = nodes.map((n, i) => { const existing = prev.get(n.id); if (existing) return { ...existing, ...n, r: radiusFor(n) }; const isVessel = n.type === 'vessel'; const angle = (i / Math.max(nodes.length, 1)) * Math.PI * 2; return { ...n, x: isVessel ? cx : cx + Math.cos(angle) * ring * (0.7 + Math.random() * 0.4), y: isVessel ? cy : cy + Math.sin(angle) * ring * (0.7 + Math.random() * 0.4), vx: 0, vy: 0, r: radiusFor(n), fixed: isVessel, }; }); alphaRef.current = 1; force((x) => x + 1); }, [nodes, width, height]); useEffect(() => { let running = true; const tick = () => { if (!running) return; const sim = simRef.current; const alpha = alphaRef.current; if (alpha > 0.01 && sim.length > 0) { const map = new Map(sim.map((n) => [n.id, n] as const)); for (const e of visibleEdges) { const a = map.get(e.source); const b = map.get(e.target); if (!a || !b) continue; const dx = b.x - a.x; const dy = b.y - a.y; const d = Math.sqrt(dx * dx + dy * dy) || 1; const target = 130; const f = ((d - target) / d) * alpha * 0.035; if (!a.fixed) { a.vx += dx * f; a.vy += dy * f; } if (!b.fixed) { b.vx -= dx * f; b.vy -= dy * f; } } for (let i = 0; i < sim.length; i++) { const a = sim[i]!; for (let j = i + 1; j < sim.length; j++) { const b = sim[j]!; const dx = b.x - a.x; const dy = b.y - a.y; const d2 = dx * dx + dy * dy || 1; const d = Math.sqrt(d2); const k = (200 * alpha) / d2; const fx = (dx / d) * k; const fy = (dy / d) * k; if (!a.fixed) { a.vx -= fx; a.vy -= fy; } if (!b.fixed) { b.vx += fx; b.vy += fy; } } } const cx = width / 2; const cy = height / 2; for (const n of sim) { if (n.fixed) continue; n.vx += (cx - n.x) * alpha * 0.003; n.vy += (cy - n.y) * alpha * 0.003; n.vx *= 0.88; n.vy *= 0.88; n.x += n.vx; n.y += n.vy; } alphaRef.current *= 0.97; force((x) => x + 1); } rafRef.current = requestAnimationFrame(tick); }; rafRef.current = requestAnimationFrame(tick); return () => { running = false; if (rafRef.current !== null) cancelAnimationFrame(rafRef.current); }; }, [visibleEdges, width, height]); const simNodes = simRef.current; const nodeMap = new Map(simNodes.map((n) => [n.id, n] as const)); function toSvg(cx: number, cy: number) { return { x: (cx - transform.tx) / transform.k, y: (cy - transform.ty) / transform.k, }; } function onPointerDown(e: React.PointerEvent) { const tgt = e.target as SVGElement; const nodeId = tgt.closest('[data-nodeid]')?.getAttribute('data-nodeid'); if (nodeId) { e.currentTarget.setPointerCapture(e.pointerId); dragRef.current = { id: nodeId, pointerId: e.pointerId }; return; } panRef.current = { x: e.clientX, y: e.clientY, tx: transform.tx, ty: transform.ty }; e.currentTarget.setPointerCapture(e.pointerId); } function onPointerMove(e: React.PointerEvent) { if (dragRef.current) { const node = simRef.current.find((n) => n.id === dragRef.current!.id); if (node) { const rect = e.currentTarget.getBoundingClientRect(); const sx = (e.clientX - rect.left - transform.tx) / transform.k; const sy = (e.clientY - rect.top - transform.ty) / transform.k; node.x = sx; node.y = sy; node.vx = 0; node.vy = 0; alphaRef.current = 0.3; force((x) => x + 1); } return; } if (panRef.current) { const dx = e.clientX - panRef.current.x; const dy = e.clientY - panRef.current.y; setTransform((t) => ({ ...t, tx: panRef.current!.tx + dx, ty: panRef.current!.ty + dy })); } } function onPointerUp(e: React.PointerEvent) { if (dragRef.current) { const node = simRef.current.find((n) => n.id === dragRef.current!.id); if (node) node.fixed = false; dragRef.current = null; } panRef.current = null; } function onWheel(e: React.WheelEvent) { e.preventDefault(); const factor = e.deltaY < 0 ? 1.1 : 0.9; const rect = e.currentTarget.getBoundingClientRect(); const mx = e.clientX - rect.left; const my = e.clientY - rect.top; setTransform((t) => { const newK = Math.max(0.3, Math.min(3, t.k * factor)); const scale = newK / t.k; return { k: newK, tx: mx - (mx - t.tx) * scale, ty: my - (my - t.ty) * scale, }; }); } function onNodeClick(node: EntityNode) { onSelect(selectedId === node.id ? null : node); } function onNodeHover(e: React.MouseEvent, node: EntityNode | null) { if (node) { setHoverNode(node.id); setTooltip({ node, x: e.clientX, y: e.clientY }); } else { setHoverNode(null); setTooltip(null); } } const edgeTypeStyle: Record = { owned_by: { stroke: '#818cf8', dash: '' }, managed_by: { stroke: '#34d399', dash: '4 2' }, flagged_under: { stroke: '#94a3b8', dash: '2 4' }, contracted_with: { stroke: '#fb923c', dash: '6 3' }, insured_by: { stroke: '#06b6d4', dash: '3 3' }, chartered_to: { stroke: '#f59e0b', dash: '5 2' }, crewed_by: { stroke: 'var(--gi-text-muted)', dash: '2 2' }, agent_in: { stroke: 'var(--gi-text-muted)', dash: '2 2' }, }; return (
{Object.entries(edgeTypeStyle).map(([type, s]) => ( ))} {visibleEdges.map((e, i) => { const a = nodeMap.get(e.source); const b = nodeMap.get(e.target); if (!a || !b) return null; const dx = b.x - a.x; const dy = b.y - a.y; const d = Math.sqrt(dx * dx + dy * dy) || 1; const ux = dx / d; const uy = dy / d; const x1 = a.x + ux * a.r; const y1 = a.y + uy * a.r; const x2 = b.x - ux * (b.r + 8); const y2 = b.y - uy * (b.r + 8); const mx = (x1 + x2) / 2; const my = (y1 + y2) / 2 - 18; const style = edgeTypeStyle[e.type] ?? { stroke: '#475569', dash: '' }; const isSelected = selectedId === e.source || selectedId === e.target; return ( {e.label} {e.confidence < 1 ? ` (${Math.round(e.confidence * 100)}%)` : ''} ); })} {simNodes.map((n) => { const cfg = ENTITY_TYPE_CONFIG[n.type as EntityNodeType] ?? { color: 'var(--gi-text-muted)', abbr: '?' }; const fill = nodeColor(n); const ring = tierRingColor(n.riskTier); const isSelected = selectedId === n.id; const isHover = hoverNode === n.id; return ( onNodeClick(n)} onMouseEnter={(e) => onNodeHover(e, n)} onMouseLeave={(e) => onNodeHover(e, null)} style={{ cursor: 'pointer' }} > {(n.riskTier !== 'clear' || isSelected) && ( )} {n.sanctioned && ( )} {!n.sanctioned && ( {cfg.abbr} )} {n.label.length > 22 ? n.label.slice(0, 20) + '…' : n.label} {n.sanctionLists && n.sanctionLists.length > 0 && ( {n.sanctionLists.map((l) => SANCTION_LIST_ABBR[l] ?? l).join(' · ')} )} ); })} {tooltip && (
{tooltip.node.label}
Type:{' '} {ENTITY_TYPE_CONFIG[tooltip.node.type]?.label ?? tooltip.node.type}
Country: {tooltip.node.country}
Risk:{' '} {TIER_CONFIG[tooltip.node.riskTier]?.label}
Confidence:{' '} {Math.round(tooltip.node.confidence * 100)}%
{tooltip.node.sanctioned && (
⚠ SANCTIONED —{' '} {tooltip.node.sanctionLists?.map((l) => SANCTION_LIST_ABBR[l] ?? l).join(', ')}
)} {tooltip.node.details && (
{tooltip.node.details}
)}
)}
Drag nodes · Scroll to zoom · Click to inspect
); } export function NetworkLegend({ nodes }: { nodes: EntityNode[] }) { const presentTypes = [...new Set(nodes.map((n) => n.type as EntityNodeType))]; return (
{presentTypes.map((t) => { const cfg = ENTITY_TYPE_CONFIG[t]; return (
{cfg.label}
); })}
Sanctioned Entity
); }