export function useAllNeighbors()

in packages/graph-explorer/src/core/StateProvider/neighbors.ts [103:154]


export function useAllNeighbors() {
  const vertices = useDisplayVerticesInCanvas();
  const vertexIds = useMemo(() => vertices.keys().toArray(), [vertices]);

  const fetchedNeighbors = useAtomValue(allFetchedNeighborsSelector(vertexIds));
  const query = useAllNeighborCountsQuery(vertexIds);

  const { enqueueNotification, clearNotification } = useNotification();

  // Show loading notification
  useEffect(() => {
    if (!query.pending) {
      return;
    }
    const notificationId = enqueueNotification({
      title: "Updating Neighbors",
      message: `Updating neighbor counts for new nodes`,
      autoHideDuration: null,
    });
    return () => clearNotification(notificationId);
  }, [clearNotification, query.pending, enqueueNotification]);

  // Show error notification
  useEffect(() => {
    if (query.pending || !query.hasErrors) {
      return;
    }
    const notificationId = enqueueNotification({
      title: "Some Errors Occurred",
      message: `While requesting counts for neighboring nodes, some errors occurred.`,
      type: "error",
    });
    return () => clearNotification(notificationId);
  }, [clearNotification, query.pending, query.hasErrors, enqueueNotification]);

  return new Map(
    query.data
      .values()
      .filter(d => d != null)
      .map(data => {
        const neighbors = fetchedNeighbors.get(data.nodeId) ?? [];
        return [
          data.nodeId,
          calculateNeighbors(
            data.totalCount,
            new Map(Object.entries(data.counts)),
            neighbors
          ),
        ];
      })
  );
}