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.

14.5 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 { convertKbToString } from "@carbon/files";
import { supportedModelTypes } from "@carbon/files/cad";
import { ValidatedForm } from "@carbon/form";
import {
  Button,
  cn,
  Loading,
  ModalCard,
  ModalCardBody,
  ModalCardContent,
  ModalCardDescription,
  ModalCardFooter,
  ModalCardHeader,
  ModalCardProvider,
  ModalCardTitle,
  toast,
  VStack
} from "@carbon/react";
import { getFileSizeLimit, INPUT_FORMAT } from "@carbon/utils";
import { Trans, useLingui } from "@lingui/react/macro";
import type { PostgrestResponse } from "@supabase/supabase-js";
import { useEffect, useState } from "react";
import { flushSync } from "react-dom";
import { useDropzone } from "react-dropzone";
import { LuCloudUpload } from "react-icons/lu";
import { useFetcher } from "react-router";
import type { z } from "zod";
import {
  CustomFormFields,
  DefaultMethodType,
  Hidden,
  Input,
  InputControlled,
  ItemPostingGroup,
  Number,
  Select,
  Submit,
  TextArea,
  UnitOfMeasure
} from "~/components/Form";
import { ReplenishmentSystemIcon, TrackingTypeIcon } from "~/components/Icons";
import {
  InterruptedUploadNotice,
  UploadProgress
} from "~/components/UploadProgress";
import {
  useCompanySettings,
  useCurrencyDecimals,
  useModelUpload,
  useNextItemId,
  usePermissions,
  useUser
} from "~/hooks";
import { path } from "~/utils/path";
import {
  itemReplenishmentSystems,
  itemTrackingTypes,
  toolValidator
} from "../../items.models";
import ItemStorageFields from "../Item/ItemStorageFields";

type ToolFormProps = {
  initialValues: z.infer<typeof toolValidator> & { tags: string[] };
  type?: "card" | "modal";
  onClose?: () => void;
};

const SIZE_LIMIT = getFileSizeLimit("CAD_MODEL_UPLOAD");

function startsWithLetter(value: string) {
  return /^[A-Za-z]/.test(value);
}

const ToolForm = ({ initialValues, type = "card", onClose }: ToolFormProps) => {
  const { t } = useLingui();
  const { company } = useUser();
  const baseCurrency = company?.baseCurrencyCode ?? "USD";
  const currencyDecimals = useCurrencyDecimals(baseCurrency);

  const fetcher = useFetcher<PostgrestResponse<{ id: string }>>();

  const [modelUploadId, setModelUploadId] = useState<string | null>(null);
  const [modelIsUploading, setModelIsUploading] = useState(false);
  const [modelFile, setModelFile] = useState<File | null>(null);
  const { carbon } = useCarbon();
  const { upload, runUpload, interrupted, dismissInterrupted } =
    useModelUpload("new-tool");
  const {
    company: { id: companyId },
    id: userId
  } = useUser();

  const modelUpload = async (file: File) => {
    if (!carbon) return;
    flushSync(() => {
      setModelIsUploading(true);
    });

    // Resumable (TUS) upload — a standard buffered upload times out on multi-GB
    // CAD files. The record goes in after it: inserting it starts the
    // optimise, which reads the file. The id and path are the upload's: a
    // resumed upload keeps its own.
    const {
      error: uploadError,
      modelId,
      modelPath: fileName
    } = await runUpload(file);
    const recordInsert = uploadError
      ? null
      : await carbon.from("modelUpload").insert({
          id: modelId,
          modelPath: fileName,
          size: file.size,
          name: file.name,
          companyId: companyId,
          createdBy: userId
        });

    if (uploadError || !recordInsert || recordInsert.error) {
      toast.error(t`Failed to upload model`);
    } else {
      setModelUploadId(modelId);
      setModelFile(file);
      toast.success(t`Uploaded model`);
    }

    setModelIsUploading(false);
  };

  const removeModel = () => {
    setModelUploadId(null);
    setModelFile(null);
  };

  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    multiple: false,
    maxSize: SIZE_LIMIT.bytes,
    onDropAccepted: async (acceptedFiles) => {
      const file = acceptedFiles[0];

      const fileExtension = file.name.split(".").pop()?.toLowerCase();
      if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
        toast.error(t`File type not supported`);
        return;
      }

      if (file.size > SIZE_LIMIT.bytes) {
        toast.error(t`File size too big (max. ${SIZE_LIMIT.format()})`);
        return;
      }

      await modelUpload(file);
    },
    onDropRejected: (fileRejections) => {
      const { errors } = fileRejections[0];
      let message;
      if (errors[0].code === "file-too-large") {
        message = t`File size too big (max. ${SIZE_LIMIT.format()})`;
      } else if (errors[0].code === "file-invalid-type") {
        message = t`File type not supported`;
      } else {
        message = errors[0].message;
      }
      toast.error(message);
    }
  });

  useEffect(() => {
    if (type !== "modal") return;

    if (fetcher.state === "loading" && fetcher.data?.data) {
      onClose?.();
      toast.success(t`Created tool`);
    } else if (fetcher.state === "idle" && fetcher.data?.error) {
      toast.error(t`Failed to create tool: ${fetcher.data.error.message}`);
    }
  }, [fetcher.data, fetcher.state, onClose, type, t]);

  const { id, onIdChange, loading } = useNextItemId("Tool");
  const permissions = usePermissions();
  const allowLowercaseItemIds =
    useCompanySettings()?.allowLowercaseItemIds === true;
  const isEditing = !!initialValues.id;

  const translateItemTrackingType = (v: string) =>
    v === "Inventory"
      ? t`Inventory`
      : v === "Non-Inventory"
        ? t`Non-Inventory`
        : v === "Serial"
          ? t`Serial`
          : t`Batch`;

  const itemTrackingTypeOptions = itemTrackingTypes.map((itemTrackingType) => ({
    label: (
      <span className="flex items-center gap-2">
        <TrackingTypeIcon type={itemTrackingType} />
        {translateItemTrackingType(itemTrackingType)}
      </span>
    ),
    value: itemTrackingType
  }));

  const [replenishmentSystem, setReplenishmentSystem] = useState<string>(
    initialValues.replenishmentSystem ?? "Buy"
  );
  const [defaultMethodType, setDefaultMethodType] = useState<string>(
    initialValues.defaultMethodType ?? "Purchase to Order"
  );
  const itemReplenishmentSystemOptions =
    itemReplenishmentSystems.map((itemReplenishmentSystem) => ({
      label: (
        <span className="flex items-center gap-2">
          <ReplenishmentSystemIcon type={itemReplenishmentSystem} />
          {itemReplenishmentSystem === "Buy"
            ? t`Buy`
            : itemReplenishmentSystem === "Make"
              ? t`Make`
              : t`Buy and Make`}
        </span>
      ),
      value: itemReplenishmentSystem
    })) ?? [];

  return (
    <ModalCardProvider type={type}>
      <ModalCard onClose={onClose}>
        <ModalCardContent>
          <ValidatedForm
            action={isEditing ? undefined : path.to.newTool}
            method="post"
            validator={toolValidator}
            defaultValues={initialValues}
            fetcher={fetcher}
          >
            <ModalCardHeader>
              <ModalCardTitle>
                {isEditing ? (
                  <Trans>Tool Details</Trans>
                ) : (
                  <Trans>New Tool</Trans>
                )}
              </ModalCardTitle>
              {!isEditing && (
                <ModalCardDescription>
                  <Trans>
                    A tool is a physical item used to make a part that can be
                    used across multiple jobs
                  </Trans>
                </ModalCardDescription>
              )}
            </ModalCardHeader>
            <ModalCardBody>
              <Hidden name="type" value={type} />
              <Hidden name="modelUploadId" value={modelUploadId ?? ""} />
              <div
                className={cn(
                  "grid w-full gap-x-8 gap-y-4",
                  isEditing
                    ? "grid-cols-1 md:grid-cols-3"
                    : "grid-cols-1 md:grid-cols-2"
                )}
              >
                {isEditing ? (
                  <Input name="id" label={t`Tool ID`} isReadOnly />
                ) : (
                  <InputControlled
                    name="id"
                    label={t`Tool ID`}
                    helperText={
                      startsWithLetter(id)
                        ? t`Use ... to get the next tool ID`
                        : undefined
                    }
                    value={id}
                    onChange={onIdChange}
                    isDisabled={loading}
                    isUppercase={!allowLowercaseItemIds}
                    autoFocus
                  />
                )}
                <Input
                  name="revision"
                  label={t`Revision`}
                  isReadOnly={isEditing}
                />

                <Input
                  name="name"
                  label={t`Short Description`}
                  characterLimit={40}
                />

                <Select
                  name="replenishmentSystem"
                  label={t`Replenishment System`}
                  termId="replenishment-system"
                  options={itemReplenishmentSystemOptions}
                  onChange={(newValue) => {
                    setReplenishmentSystem(newValue?.value ?? "Buy");
                    if (newValue?.value === "Buy") {
                      setDefaultMethodType("Purchase to Order");
                    } else {
                      setDefaultMethodType("Make to Order");
                    }
                  }}
                />
                <Select
                  name="itemTrackingType"
                  label={t`Tracking Type`}
                  termId="item-tracking-type"
                  options={itemTrackingTypeOptions}
                />
                <DefaultMethodType
                  name="defaultMethodType"
                  label={t`Default Method Type`}
                  termId="item-default-method-type"
                  replenishmentSystem={replenishmentSystem}
                  value={defaultMethodType}
                  onChange={(newValue) =>
                    setDefaultMethodType(newValue?.value ?? "Purchase to Order")
                  }
                />

                <UnitOfMeasure
                  name="unitOfMeasureCode"
                  label={t`Unit of Measure`}
                />
                {!isEditing && (
                  <ItemPostingGroup
                    name="postingGroupId"
                    label={t`Item Group`}
                    termId="item-group"
                    isClearable
                  />
                )}
                {!isEditing && (
                  <Number
                    name="unitCost"
                    label={t`Unit Cost`}
                    formatOptions={INPUT_FORMAT.rate(
                      baseCurrency,
                      currencyDecimals
                    )}
                    minValue={0}
                    isReadOnly={replenishmentSystem === "Make"}
                  />
                )}

                <ItemStorageFields />

                <CustomFormFields table="tool" tags={initialValues.tags} />
              </div>
              <div className="mt-4 w-full">
                <TextArea name="description" label={t`Long Description`} />
              </div>
              <VStack spacing={2} className="mt-4 w-full">
                <label
                  htmlFor="model-upload"
                  className="text-xs font-medium text-muted-foreground"
                >
                  <Trans>CAD Model</Trans>
                </label>
                <div
                  {...getRootProps()}
                  className={`w-full border-2 border-dashed rounded-md p-6 text-center hover:border-primary hover:bg-primary/10 cursor-pointer ${
                    isDragActive
                      ? "border-primary bg-primary/10"
                      : "border-muted"
                  }`}
                >
                  <input id="model-upload" {...getInputProps()} />
                  {upload !== null ? (
                    <UploadProgress
                      label={t`Uploading model`}
                      description={t`Uploading the CAD file`}
                      percent={upload.percent}
                      uploaded={upload.uploaded}
                      total={upload.total}
                    />
                  ) : modelFile ? (
                    <>
                      <p className="text-sm font-semibold text-card-foreground">
                        {modelFile.name}
                      </p>
                      <p className="text-xs text-muted-foreground group-hover:text-foreground">
                        {convertKbToString(Math.ceil(modelFile.size / 1024))}
                      </p>
                      <Button
                        size="sm"
                        variant="secondary"
                        className="mt-2"
                        onClick={removeModel}
                      >
                        <Trans>Remove</Trans>
                      </Button>
                    </>
                  ) : (
                    <Loading isLoading={modelIsUploading}>
                      <LuCloudUpload className="mx-auto h-12 w-12 text-muted-foreground group-hover:text-primary-foreground" />
                      <p className="text-xs text-muted-foreground group-hover:text-foreground">
                        {t`Supports ${supportedModelTypes.join(", ")} files`}
                      </p>
                      {interrupted && (
                        <InterruptedUploadNotice
                          className="mt-3"
                          name={interrupted.name}
                          percent={interrupted.percent}
                          onDismiss={dismissInterrupted}
                        />
                      )}
                    </Loading>
                  )}
                </div>
              </VStack>
            </ModalCardBody>
            <ModalCardFooter>
              <Submit
                isLoading={fetcher.state !== "idle"}
                isDisabled={
                  isEditing
                    ? !permissions.can("update", "parts")
                    : !permissions.can("create", "parts")
                }
              >
                <Trans>Save</Trans>
              </Submit>
            </ModalCardFooter>
          </ValidatedForm>
        </ModalCardContent>
      </ModalCard>
    </ModalCardProvider>
  );
};

export default ToolForm;