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.

2.1 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 { VStack } from "@carbon/react";
import { Trans } from "@lingui/react/macro";
import { useParams } from "react-router";
import { useRouteData } from "~/hooks";
import type { ChangeNotice } from "~/modules/items/types";
import type { AffectedItemDraft } from "~/modules/items/ui/ChangeNotice";
import { useChangeNoticeEngineeringLock } from "~/modules/items/ui/ChangeNotice";
import AffectedItemDetail from "~/modules/items/ui/ChangeNotice/AffectedItemDetail";
import { path } from "~/utils/path";

// The selected affected item's line detail, addressed by the URL (not client
// state) — mirrors the sales order line route `$orderId.$lineId.details`. Its
// data comes from the parent $id loader (useRouteData); the URL only decides
// which affected item to show, so refresh + back/forward reselect it.
export default function ChangeNoticeAffectedItemRoute() {
  const { id, affectedId } = useParams();
  if (!id) throw new Error("Could not find id");

  const routeData = useRouteData<{
    changeNotice: ChangeNotice;
    affectedItems: AffectedItemDraft[];
  }>(path.to.changeNotice(id));
  const { isDisabled, reason: disabledReason } =
    useChangeNoticeEngineeringLock(id);

  const changeNotice = routeData?.changeNotice;
  if (!changeNotice) throw new Error("Could not find change notice data");

  const affected =
    routeData?.affectedItems.find((a) => a.affectedItem.id === affectedId) ??
    null;

  if (!affected) {
    return (
      <div className="flex items-center justify-center h-full text-sm text-muted-foreground">
        <Trans>Affected item not found.</Trans>
      </div>
    );
  }

  return (
    <VStack spacing={4} className="p-4">
      <AffectedItemDetail
        key={affected.affectedItem.id}
        changeOrderId={id}
        affected={affected}
        isDisabled={isDisabled}
        disabledReason={disabledReason}
      />
    </VStack>
  );
}