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.

3.0 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 { ValueOptionsLoader } from "@carbon/utils";
import { useMemo } from "react";
import { useCountries } from "~/components/Form/Country";
import { useCustomerStatuses } from "~/components/Form/CustomerStatus";
import { useCustomerTypes } from "~/components/Form/CustomerType";
import { useItemPostingGroups } from "~/components/Form/ItemPostingGroup";
import { useLocations } from "~/components/Form/Location";
import { useStorageTypes } from "~/components/Form/StorageTypes";
import { inventoryItemTypes } from "~/modules/inventory/inventory.models";
import {
  itemReplenishmentSystems,
  itemTrackingTypes
} from "~/modules/items/items.models";

export type ValueOption = { value: string; label: string };
// Partial: not every loader has a flat option list in the builder UI. The
// `storageUnits` loader exists only for the server-side message resolver
// ({condition[n].name} → bin name); the builder uses the hierarchical
// StorageUnitValuePicker for that field, so no flat options are supplied here.
export type ValueOptionsByLoader = Partial<
  Record<ValueOptionsLoader, ValueOption[]>
>;

const enumOptions = (arr: readonly string[]): ValueOption[] =>
  arr.map((v) => ({ value: v, label: v }));

// Module-level constants — stable refs, never re-allocated. (The `itemTypes`
// key returned below is the persisted field-registry loader name from
// @carbon/utils ValueOptionsLoader — only the option SOURCE is renamed.)
const ITEM_TYPES_OPTIONS = enumOptions(inventoryItemTypes);
const ITEM_TRACKING_TYPES_OPTIONS = enumOptions(itemTrackingTypes);
const REPLENISHMENT_SYSTEMS_OPTIONS = enumOptions(itemReplenishmentSystems);

export function useValueOptions(
  opts: { includeCustomerFields?: boolean } = {}
): ValueOptionsByLoader {
  const { includeCustomerFields = false } = opts;
  const locations = useLocations();
  const storageTypes = useStorageTypes();
  const itemPostingGroups = useItemPostingGroups();
  // Sales-rule loaders (customer-context fields). `countries` values are
  // alpha-2 codes, matching the persisted `customer.location.countryCode`.
  // Only fetched when the builder's field pool actually contains customer
  // fields — a storage-rule builder never shows them.
  const customerTypes = useCustomerTypes(includeCustomerFields);
  const customerStatuses = useCustomerStatuses(includeCustomerFields);
  const countries = useCountries(includeCustomerFields);

  return useMemo<ValueOptionsByLoader>(
    () => ({
      locations,
      storageTypes,
      itemPostingGroups,
      itemTypes: ITEM_TYPES_OPTIONS,
      itemTrackingTypes: ITEM_TRACKING_TYPES_OPTIONS,
      replenishmentSystems: REPLENISHMENT_SYSTEMS_OPTIONS,
      customerTypes,
      customerStatuses,
      countries
    }),
    [
      locations,
      storageTypes,
      itemPostingGroups,
      customerTypes,
      customerStatuses,
      countries
    ]
  );
}