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.

12.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 { useCarbon } from "@carbon/auth";
import { useChangedRows } from "@carbon/query";
import { toast, useDisclosure, useInterval, useViewport } from "@carbon/react";
import {
  getLocalTimeZone,
  now,
  parseAbsolute,
  parseDate,
  today,
  toZoned
} from "@internationalized/date";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useParams } from "react-router";
import { useRealtime, useUrlParams, useUser } from "~/hooks";
import { isSerialEntityIncompleteForOperation } from "~/services/operations.service";
import { shouldAdvanceToNextSerialUnit } from "~/services/serial-advancement";
import type {
  JobMaterial,
  JobOperationParameter,
  JobOperationStep,
  OperationWithDetails,
  ProductionEvent,
  TrackedEntity
} from "~/services/types";
import { path } from "~/utils/path";

export function useOperation({
  operation,
  events,
  trackedEntities,
  isFirstOperation,
  requiresSerialTracking,
  pauseInterval,
  procedure,
  batchId,
  onAdvanceToUnit
}: {
  operation: OperationWithDetails;
  events: ProductionEvent[];
  trackedEntities: TrackedEntity[];
  // The first operation in the routing has no printed labels yet, so units are
  // auto-selected; later operations require the operator to scan/select.
  isFirstOperation: boolean;
  // The scan/select serial flow only applies to serial-tracked parts. Batch and
  // non-tracked make-methods must never see the serial picker, even though a
  // batch make-method carries a seed trackedEntity row.
  requiresSerialTracking: boolean;
  pauseInterval: boolean;
  procedure: Promise<{
    attributes: JobOperationStep[];
    parameters: JobOperationParameter[];
  }>;
  // In batch mode, subscribe to every productionEvent for the batch (all
  // members' timers) rather than just this operation's, so the shared running
  // timer stays live regardless of which member started it.
  batchId?: string;
  // Auto-select the next serial unit (used on the first operation, where there
  // are no printed labels to scan yet). Provided by JobOperation.
  onAdvanceToUnit?: (entity: TrackedEntity) => void;
}) {
  const [params] = useUrlParams();
  const trackedEntityParam = params.get("trackedEntityId");
  // biome-ignore lint/correctness/noUnusedVariables: suppressed due to migration
  const { carbon, accessToken } = useCarbon();
  const user = useUser();

  const actionsSheet = useDisclosure();
  const scrapModal = useDisclosure();
  const reworkModal = useDisclosure();
  const completeModal = useDisclosure();
  const finishModal = useDisclosure();
  const issueModal = useDisclosure();
  const serialModal = useDisclosure();

  // we do this to avoid re-rendering when the modal is open
  const isAnyModalOpen =
    pauseInterval ||
    actionsSheet.isOpen ||
    scrapModal.isOpen ||
    reworkModal.isOpen ||
    completeModal.isOpen ||
    finishModal.isOpen ||
    issueModal.isOpen ||
    serialModal.isOpen;

  const [selectedMaterial, setSelectedMaterial] = useState<JobMaterial | null>(
    null
  );

  const [activeTab, setActiveTab] = useState("details");
  const { isPhone } = useViewport();
  // Phones open on the timer that runs, so Pause shows for it. A Setup or
  // Labor timer counts only when it is this user's; Machine counts for anyone
  // (the same rule as `activeEvents` below). Else: Setup, Machine, Labor.
  const [eventType, setEventType] = useState<string>(() => {
    const isRunning = (type: string) =>
      events.some(
        (e) =>
          e.type === type &&
          e.endTime === null &&
          (type === "Machine" || e.employeeId === user.id)
      );
    const running =
      isPhone && (["Setup", "Machine", "Labor"] as const).find(isRunning);
    if (running) {
      return running;
    }
    if (operation.setupDuration > 0) {
      return "Setup";
    }
    if (operation.machineDuration > 0) {
      return "Machine";
    }
    return "Labor";
  });

  const [operationState, setOperationState] = useState(operation);

  const [eventState, setEventState] = useState<ProductionEvent[]>(events);

  useEffect(() => {
    setEventState(events);
  }, [events]);

  useEffect(() => {
    setOperationState(operation);
  }, [operation]);

  useRealtime("job", `id=eq.${operation.jobId}`);

  useChangedRows<ProductionEvent>({
    companyId: user.company.id,
    table: "productionEvent",
    filter: batchId
      ? `jobOperationBatchId=eq.${batchId}`
      : `jobOperationId=eq.${operation.id}`,
    onChange: ({ op, ids, rows }) => {
      if (op === "DELETE") {
        setEventState((prevEvents) =>
          prevEvents.filter((event) => !ids.includes(event.id))
        );
        return;
      }
      const mine = rows.filter((row) =>
        batchId
          ? row.jobOperationBatchId === batchId
          : row.jobOperationId === operation.id
      );
      setEventState((prevEvents) => {
        let next = prevEvents;
        for (const row of mine) {
          next = next.some((event) => event.id === row.id)
            ? next.map((event) =>
                event.id === row.id ? { ...event, ...row } : event
              )
            : [...next, row];
        }
        return next;
      });
    }
  });

  useChangedRows<{
    id: string;
    status?: OperationWithDetails["operationStatus"];
  }>({
    companyId: user.company.id,
    table: "jobOperation",
    filter: `id=eq.${operation.id}`,
    onChange: ({ op, ids, rows }) => {
      if (op === "DELETE") {
        if (ids.includes(operation.id)) {
          toast.error("This operation has been deleted");
          window.location.href = path.to.operations;
        }
        return;
      }
      const updated = rows.find((row) => row.id === operation.id);
      if (!updated) return;
      setOperationState((prev) => ({
        ...prev,
        ...updated,
        operationStatus: updated.status ?? prev.operationStatus
      }));
    }
  });

  const getProgress = useCallback(() => {
    const timeNow = now(getLocalTimeZone());
    return eventState.reduce(
      (acc, event) => {
        if (event.endTime && event.type) {
          acc[event.type.toLowerCase() as keyof typeof acc] +=
            (event.duration ?? 0) * 1000;
        } else if (event.startTime && event.type) {
          const startTime = toZoned(
            parseAbsolute(event.startTime, getLocalTimeZone()),
            getLocalTimeZone()
          );

          const difference = timeNow.compare(startTime);

          if (difference > 0) {
            acc[event.type.toLowerCase() as keyof typeof acc] += difference;
          }
        }
        return acc;
      },
      {
        setup: 0,
        labor: 0,
        machine: 0
      }
    );
  }, [eventState]);

  const [progress, setProgress] = useState<{
    setup: number;
    labor: number;
    machine: number;
  }>(getProgress);

  const activeEvents = useMemo(() => {
    return {
      setupProductionEvent: events.find(
        (e) =>
          e.type === "Setup" && e.endTime === null && e.employeeId === user.id
      ),
      laborProductionEvent: events.find(
        (e) =>
          e.type === "Labor" && e.endTime === null && e.employeeId === user.id
      ),
      machineProductionEvent: eventState.find(
        (e) => e.type === "Machine" && e.endTime === null
      )
    };
  }, [eventState, events, user.id]);

  const active = useMemo(() => {
    return {
      setup: !!activeEvents.setupProductionEvent,
      labor: !!activeEvents.laborProductionEvent,
      machine: !!activeEvents.machineProductionEvent
    };
  }, [activeEvents]);

  useInterval(
    () => {
      setProgress(getProgress());
    },
    (active.setup || active.labor || active.machine) && !isAnyModalOpen
      ? 1000
      : null
  );

  const { operationId } = useParams();
  const [availableEntities, setAvailableEntities] = useState<TrackedEntity[]>(
    []
  );
  // Later operations prompt scan/select once on arrival — even if a stale or
  // auto-seeded ?trackedEntityId is already in the URL. This ref makes that a
  // once-per-mount prompt so we don't re-open the picker while the operator is
  // working a scanned unit.
  const arrivalPromptedRef = useRef(false);
  // The unit the operator last held. The re-prompt is edge-triggered off this:
  // the picker reopens when the held unit *becomes* complete, never merely
  // because nothing is selected. `trackedEntities` gets a fresh identity on
  // every revalidation (realtime job updates, any start/stop/record fetcher), so
  // a level-triggered "no unit selected → open" would re-open the picker on each
  // one and the operator could never dismiss it.
  const heldEntityRef = useRef<string | null>(null);
  // Select / advance the serial unit the operator works on. This is the single
  // advancement authority — complete.tsx no longer redirects to a next unit.
  //  - First operation: no labels have been printed yet, so there is nothing to
  //    scan — auto-select the next unit and flow through #1..#N.
  //  - Later operations: every unit already carries a printed label, so the
  //    operator scans/selects. Prompt once on arrival (even if a stale param sits
  //    in the URL) and again after each completion.
  // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
  useEffect(() => {
    // Only serial-tracked parts have per-unit labels to scan/select. Batch and
    // non-tracked make-methods must never surface the serial picker.
    if (!requiresSerialTracking) return;
    const uncompletedEntities = trackedEntities.filter((entity) =>
      isSerialEntityIncompleteForOperation(entity, operationId ?? "")
    );
    setAvailableEntities(uncompletedEntities);
    if (uncompletedEntities.length === 0) return;
    const selectedIsIncomplete =
      !!trackedEntityParam &&
      uncompletedEntities.some((entity) => entity.id === trackedEntityParam);

    if (isFirstOperation) {
      // Auto-advance is edge-triggered off the held unit: advance on arrival
      // with nothing selected, or when the held unit itself completes — never
      // over an explicit selection of an already-complete unit (going back to
      // review/re-print is allowed).
      if (
        shouldAdvanceToNextSerialUnit({
          selectedEntityId: trackedEntityParam,
          selectedIsIncomplete,
          heldEntityId: heldEntityRef.current
        })
      ) {
        heldEntityRef.current = null;
        onAdvanceToUnit?.(uncompletedEntities[0]);
      } else {
        heldEntityRef.current = selectedIsIncomplete
          ? trackedEntityParam
          : null;
      }
      return;
    }

    // Prompt scan/select once on arrival — even when a stale/auto-seeded
    // ?trackedEntityId is present, which would otherwise read as "already working
    // a unit".
    if (!arrivalPromptedRef.current) {
      arrivalPromptedRef.current = true;
      heldEntityRef.current = selectedIsIncomplete ? trackedEntityParam : null;
      serialModal.onOpen();
      return;
    }

    if (selectedIsIncomplete) {
      heldEntityRef.current = trackedEntityParam;
      return;
    }

    // The held unit just completed — prompt for the next one. If the operator is
    // holding nothing (they dismissed the picker), stay quiet; the Scan button in
    // the Serial Numbers section reopens it on demand.
    if (heldEntityRef.current) {
      heldEntityRef.current = null;
      serialModal.onOpen();
    }
    // causes an infinite loop on navigation
  }, [
    trackedEntities,
    trackedEntityParam,
    operationId,
    isFirstOperation,
    requiresSerialTracking
  ]);

  return {
    active,
    availableEntities,
    hasActiveEvents:
      progress.setup > 0 || progress.labor > 0 || progress.machine > 0,
    ...activeEvents,
    progress,
    operation: operationState,

    actionsSheet,
    activeTab,
    eventType,
    scrapModal,
    reworkModal,
    completeModal,
    finishModal,
    issueModal,
    serialModal,
    // The JOB's deadline. The operation's own `operationDueDate` is the
    // scheduler's need-by target for this step, shown separately.
    isOverdue: operation.jobDueDate
      ? parseDate(operation.jobDueDate.slice(0, 10)).compare(
          today(getLocalTimeZone())
        ) < 0
      : false,
    selectedMaterial,
    setSelectedMaterial,
    setActiveTab,
    setEventType
  };
}