export function useAddToGraph()

in packages/graph-explorer/src/hooks/useAddToGraph.ts [22:90]


export function useAddToGraph() {
  const setVertices = useSetAtom(nodesAtom);
  const setEdges = useSetAtom(edgesAtom);
  const setActiveSchema = useSetAtom(activeSchemaSelector);
  const updateGraphStorage = useUpdateGraphSession();
  const materializeVertices = useMaterializeVertices();

  return useCallback(
    async (entities: { vertices?: Vertex[]; edges?: Edge[] }) => {
      const vertices = toNodeMap(entities.vertices ?? []);
      const edges = toEdgeMap(entities.edges ?? []);

      // Add fragment vertices from the edges if they are missing
      for (const edge of edges.values()) {
        if (!vertices.has(edge.source)) {
          vertices.set(
            edge.source,
            createVertex({ id: edge.source, types: edge.sourceTypes })
          );
        }

        if (!vertices.has(edge.target)) {
          vertices.set(
            edge.target,
            createVertex({ id: edge.target, types: edge.targetTypes })
          );
        }
      }

      // Ensure all fragments are materialized
      const newVerticesMap = await materializeVertices(vertices);

      // Ensure there is something to add
      if (newVerticesMap.size === 0 && edges.size === 0) {
        return;
      }

      // Add new vertices to the graph
      if (newVerticesMap.size > 0) {
        setVertices(prev => new Map([...prev, ...newVerticesMap]));
      }

      // Add new edges to the graph
      if (edges.size > 0) {
        setEdges(prev => new Map([...prev, ...edges]));
      }

      // Update the schema with any new vertex or edge types or attributes
      setActiveSchema(prev => {
        if (!prev) {
          return prev;
        }
        return updateSchemaFromEntities(
          { nodes: newVerticesMap, edges: edges },
          prev
        );
      });

      await updateGraphStorage();
    },
    [
      materializeVertices,
      setActiveSchema,
      setEdges,
      setVertices,
      updateGraphStorage,
    ]
  );
}