Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { useCallback, useEffect, useRef } from "react";
import { useFetcher } from "react-router";
import type { LineagePayload } from "../utils";

type ExpandDirection = "up" | "down" | "both";

export function useExpandNode(
  onResult: (payload: LineagePayload, originId: string) => void
) {
  const fetcher = useFetcher<LineagePayload>();
  const pendingOriginRef = useRef<string | null>(null);

  useEffect(() => {
    if (fetcher.state === "idle" && fetcher.data && pendingOriginRef.current) {
      onResult(fetcher.data, pendingOriginRef.current);
      pendingOriginRef.current = null;
    }
  }, [fetcher.state, fetcher.data, onResult]);

  const expand = useCallback(
    (entityId: string, direction: ExpandDirection = "both", depth = 1) => {
      pendingOriginRef.current = entityId;
      const params = new URLSearchParams({
        trackedEntityId: entityId,
        direction,
        depth: String(depth)
      });
      fetcher.load(`/api/traceability/expand?${params.toString()}`);
    },
    [fetcher]
  );

  return { expand, isLoading: fetcher.state !== "idle" };
}