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.6 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 type { ComboboxProps } from "@carbon/form";
import { CreatableCombobox } from "@carbon/form";
import { useDisclosure } from "@carbon/react";
import { useMemo, useRef, useState } from "react";
import type { ItemReplenishmentSystem } from "~/modules/items/types";
import PartForm from "~/modules/items/ui/Parts/PartForm";
import { useParts } from "~/stores";
import { ItemLifecycleBadge } from "../ItemLifecycleBadge";
import { useEmptyState } from "./emptyStates";

type PartSelectProps = Omit<ComboboxProps, "options"> & {
  itemReplenishmentSystem?: ItemReplenishmentSystem;
};

const Part = ({ itemReplenishmentSystem, ...props }: PartSelectProps) => {
  const parts = useParts();
  const newPartsModal = useDisclosure();
  const [created, setCreated] = useState<string>("");
  const triggerRef = useRef<HTMLButtonElement>(null);

  const options = useMemo(
    () =>
      parts.map((part) => ({
        value: part.id,
        label: part.supersessionMode ? (
          <span className="flex items-center gap-1.5">
            {part.id}
            <ItemLifecycleBadge mode={part.supersessionMode} />
          </span>
        ) : (
          part.id
        ),
        helper: part.name
      })) ?? [],
    [parts]
  );

  const emptyMessage = useEmptyState("part", {
    onCreate: () => newPartsModal.onOpen()
  });

  return (
    <>
      <CreatableCombobox
        ref={triggerRef}
        options={options}
        emptyMessage={emptyMessage}
        {...props}
        label={props?.label ?? "Part"}
        onCreateOption={(option) => {
          newPartsModal.onOpen();
          setCreated(option);
        }}
      />
      {newPartsModal.isOpen && (
        <PartForm
          type="modal"
          onClose={() => {
            setCreated("");
            newPartsModal.onClose();
            triggerRef.current?.click();
          }}
          initialValues={{
            id: "",
            revision: "0",
            name: created,
            description: "",
            itemTrackingType: "Inventory" as "Inventory",
            replenishmentSystem: itemReplenishmentSystem ?? "Buy and Make",
            defaultMethodType: "Pull from Inventory",
            unitOfMeasureCode: "EA",
            unitCost: 0,
            lotSize: 0,
            shelfLifeCalculateFromBom: false
          }}
        />
      )}
    </>
  );
};

Part.displayName = "Part";

export default Part;