import { useThree } from '@react-three/fiber'; import Graph from 'graphology'; import { useCallback, useEffect, useMemo, useRef } from 'react'; import type { PerspectiveCamera } from 'three'; import { getVisibleEntities } from './collapse'; import type { LayoutOverrides, LayoutStrategy, LayoutTypes } from './layout'; import { layoutProvider } from './layout'; import { tick } from './layout/layoutUtils'; import type { SizingType } from './sizing'; import type { DragReferences } from './store'; import { useStore } from './store'; import type { GraphEdge, GraphNode, InternalGraphNode } from './types'; import { calculateClusters } from './utils/cluster'; import { buildGraph, transformGraph } from './utils/graph'; import type { LabelVisibilityType } from './utils/visibility'; import { calcLabelVisibility, isLabelVisibilityCameraDependent } from './utils/visibility'; export interface GraphInputs { nodes: GraphNode[]; edges: GraphEdge[]; collapsedNodeIds?: string[]; layoutType?: LayoutTypes; sizingType?: SizingType; labelType?: LabelVisibilityType; sizingAttribute?: string; selections?: string[]; actives?: string[]; clusterAttribute?: string; defaultNodeSize?: number; minNodeSize?: number; maxNodeSize?: number; constrainDragging?: boolean; layoutOverrides?: LayoutOverrides; } export const useGraph = ({ layoutType, sizingType, labelType, sizingAttribute, clusterAttribute, selections, nodes, edges, actives, collapsedNodeIds, defaultNodeSize, maxNodeSize, minNodeSize, layoutOverrides, constrainDragging }: GraphInputs) => { const graph = useStore(state => state.graph); const clusters = useStore(state => state.clusters); const storedNodes = useStore(state => state.nodes); const setClusters = useStore(state => state.setClusters); const stateCollapsedNodeIds = useStore(state => state.collapsedNodeIds); const setEdges = useStore(state => state.setEdges); const stateNodes = useStore(state => state.nodes); const setNodes = useStore(state => state.setNodes); const setSelections = useStore(state => state.setSelections); const setActives = useStore(state => state.setActives); const drags = useStore(state => state.drags); const setDrags = useStore(state => state.setDrags); const setCollapsedNodeIds = useStore(state => state.setCollapsedNodeIds); const layoutMounted = useRef(false); const layout = useRef(null); const camera = useThree(state => state.camera) as PerspectiveCamera; const dragRef = useRef(drags); const clustersRef = useRef([]); const prevLabelType = useRef(labelType); // When a new node is added, remove the dragged position of the cluster nodes to put new node in the right place useEffect(() => { if (!clusterAttribute) { return; } const existedNodesIds = storedNodes.map(n => n.id); const newNode = nodes.find(n => !existedNodesIds.includes(n.id)); if (newNode) { const clusterName = newNode.data[clusterAttribute]; const cluster = clusters.get(clusterName); const drags = { ...dragRef.current }; cluster?.nodes?.forEach(node => (drags[node.id] = undefined)); dragRef.current = drags; setDrags(drags); } }, [storedNodes, nodes, clusterAttribute, clusters, setDrags]); // Lazily build (and cache) the FULL graph used for collapse traversal. // The store graph can't be used here — it holds the post-collapse visible // subgraph. Caching by nodes/edges identity means repeated collapse/expand // interactions reuse one graph instead of rebuilding it per click, and // graphs that never collapse anything never build it at all. const collapseGraphRef = useRef<{ nodes: GraphNode[]; edges: GraphEdge[]; graph: Graph; } | null>(null); const getCollapseGraph = useCallback(() => { const cached = collapseGraphRef.current; if (!cached || cached.nodes !== nodes || cached.edges !== edges) { collapseGraphRef.current = { nodes, edges, graph: buildGraph(new Graph({ multi: true }), nodes, edges) }; } return collapseGraphRef.current.graph; }, [nodes, edges]); // Calculate the visible entities const { visibleEdges, visibleNodes } = useMemo( () => getVisibleEntities({ collapsedIds: stateCollapsedNodeIds, nodes, edges, graph: stateCollapsedNodeIds?.length ? getCollapseGraph() : undefined }), [stateCollapsedNodeIds, nodes, edges, getCollapseGraph] ); // Store node positions inside drags state const updateDrags = useCallback( (nodes: InternalGraphNode[]) => { const drags = { ...dragRef.current }; nodes.forEach(node => (drags[node.id] = node)); dragRef.current = drags; setDrags(drags); }, [setDrags] ); const updateLayout = useCallback( async (curLayout?: any) => { // Cache the layout provider layout.current = curLayout || layoutProvider({ ...layoutOverrides, type: layoutType, graph, drags: dragRef.current, clusters: clustersRef?.current, clusterAttribute }); // Run the layout await tick(layout.current); // Transform the graph const result = transformGraph({ graph, layout: layout.current, sizingType, labelType, sizingAttribute, maxNodeSize, minNodeSize, defaultNodeSize, clusterAttribute }); // Calculate clusters const newClusters = calculateClusters({ nodes: result.nodes, clusterAttribute }); // Do not decrease the cluster size is the number of nodes is the same if (constrainDragging) { newClusters.forEach(cluster => { const prevCluster = clustersRef.current.get(cluster.label); if (prevCluster?.nodes.length === cluster.nodes.length) { cluster.position = clustersRef.current?.get(cluster.label)?.position ?? cluster.position; } }); } // Set our store outputs setEdges(result.edges); setNodes(result.nodes); setClusters(newClusters); if (clusterAttribute) { // Set drag positions for nodes to prevent them from being moved by the layout update updateDrags(result.nodes); } }, // eslint-disable-next-line react-hooks/exhaustive-deps [ layoutOverrides, layoutType, clusterAttribute, sizingType, labelType, sizingAttribute, maxNodeSize, minNodeSize, defaultNodeSize, setEdges, setNodes, setClusters ] ); // Transient updates useEffect(() => { dragRef.current = drags; }, [drags, clusterAttribute, updateLayout]); // Transient cluster state useEffect(() => { clustersRef.current = clusters; }, [clusters]); useEffect(() => { const labelTypeChanged = prevLabelType.current !== labelType; prevLabelType.current = labelType; // Camera position/zoom only affects label visibility in 'auto' mode // (every other mode is always-on, always-off, or shape-gated and so is // camera-independent). For those modes we still need to recompute when // the labelType prop itself changes (e.g. 'all' -> 'none' at runtime), // but we can skip the work on pure camera moves. This avoids an O(n) // pass over every node on every camera frame. if (!isLabelVisibilityCameraDependent(labelType) && !labelTypeChanged) { return; } // First compute just the booleans (cheap) and only allocate a fresh // node array when something actually changed — this effect fires on // every camera move, so avoiding N object spreads per frame matters // a lot on large graphs. const getVisibility = calcLabelVisibility({ nodeCount: stateNodes?.length, labelType, camera }); let isVisibilityUpdated = false; const nextVisibility = new Array(stateNodes.length); for (let i = 0; i < stateNodes.length; i++) { const node = stateNodes[i]; const labelVisible = getVisibility('node', node?.size, node?.position); nextVisibility[i] = labelVisible; if (labelVisible !== node.labelVisible) { isVisibilityUpdated = true; } } // Update the nodes if the label visibility has changed if (isVisibilityUpdated) { setNodes( stateNodes.map((node, i) => ({ ...node, labelVisible: nextVisibility[i] })) ); } }, [camera, camera.zoom, camera.position.z, setNodes, stateNodes, labelType]); useEffect(() => { // Let's set the store selections so its easier to access if (layoutMounted.current) { setSelections(selections); } }, [selections, setSelections]); useEffect(() => { // Let's set the store actives so its easier to access if (layoutMounted.current) { setActives(actives); } }, [actives, setActives]); // Create the nggraph graph object useEffect(() => { async function update() { layoutMounted.current = false; buildGraph(graph, visibleNodes, visibleEdges); await updateLayout(); // rqf to prevent race condition requestAnimationFrame(() => (layoutMounted.current = true)); } update(); // eslint-disable-next-line }, [visibleNodes, visibleEdges]); useEffect(() => { // Let's set the store collapsedNodeIds so its easier to access if (layoutMounted.current) { setCollapsedNodeIds(collapsedNodeIds); } }, [collapsedNodeIds, setCollapsedNodeIds]); // Update layout on type changes useEffect(() => { if (layoutMounted.current) { // When a update is changed, discard all the previous drag positions // NOTE: This sets the transient and the state dragRef.current = {}; setDrags({}); // Recalculate the layout updateLayout(); } }, [layoutType, updateLayout, setDrags]); // Update layout on size, label changes useEffect(() => { if (layoutMounted.current) { updateLayout(layout.current); } }, [sizingType, sizingAttribute, labelType, updateLayout]); return { updateLayout }; };