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.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 type { CreatableComboboxProps } from "@carbon/form";
import { CreatableCombobox } from "@carbon/form";
import { useLoaderQuery } from "@carbon/query";
import { useDisclosure } from "@carbon/react";
import { getLocalTimeZone } from "@internationalized/date";
import { useMemo, useRef, useState } from "react";
import { useUser } from "~/hooks";
import type { getLocationsList } from "~/modules/resources/resources.service";
import LocationForm from "~/modules/resources/ui/Locations/LocationForm";
import { path } from "~/utils/path";
import { Enumerable } from "../Enumerable";
import { useEmptyState } from "./emptyStates";

type LocationSelectProps = Omit<
  CreatableComboboxProps,
  "options" | "inline"
> & {
  inline?: boolean;
};

const LocationPreview = (
  value: string,
  options: { value: string; label: string | JSX.Element }[]
) => {
  const location = options.find((o) => o.value === value);
  if (!location) return null;
  return location?.label ?? null;
};

const Location = ({ inline = false, ...props }: LocationSelectProps) => {
  const newLocationModal = useDisclosure();
  const [created, setCreated] = useState<string>("");
  const triggerRef = useRef<HTMLButtonElement>(null);

  const options = useLocations();

  const { company } = useUser();

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

  return (
    <>
      <CreatableCombobox
        ref={triggerRef}
        options={options.map((o) => ({
          value: o.value,
          label: <Enumerable value={o.label} />
        }))}
        emptyMessage={emptyMessage}
        {...props}
        label={props?.label ?? "Location"}
        inline={inline ? LocationPreview : undefined}
        onCreateOption={(option) => {
          newLocationModal.onOpen();
          setCreated(option);
        }}
      />
      {newLocationModal.isOpen && (
        <LocationForm
          type="modal"
          onClose={() => {
            setCreated("");
            newLocationModal.onClose();
            triggerRef.current?.click();
          }}
          initialValues={{
            name: created,
            timezone: getLocalTimeZone(),
            addressLine1: "",
            addressLine2: "",
            city: "",
            stateProvince: "",
            postalCode: "",
            countryCode: company?.countryCode ?? ""
          }}
        />
      )}
    </>
  );
};

Location.displayName = "Location";

export default Location;

export const useLocations = () => {
  const locationFetcher = useLoaderQuery<
    Awaited<ReturnType<typeof getLocationsList>>
  >(path.to.api.locations);

  const options = useMemo(
    () =>
      locationFetcher.data?.data
        ? locationFetcher.data?.data.map((c) => ({
            value: c.id,
            label: c.name,
            timezone: c.timezone
          }))
        : [],
    [locationFetcher.data]
  );

  return options;
};