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.

21.7 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,
  downloadUrl,
  getDocumentType,
  storage
} from "@carbon/files";
import { getLogger } from "@carbon/logger";
import { useRevalidator } from "@carbon/query";
import {
  Card,
  CardAction,
  CardContent,
  CardHeader,
  CardTitle,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuSub,
  DropdownMenuSubContent,
  DropdownMenuSubTrigger,
  DropdownMenuTrigger,
  File,
  HStack,
  IconButton,
  MENU_ITEM_SHORTCUTS,
  Table,
  Tbody,
  Td,
  Th,
  Thead,
  Tr,
  toast,
  useViewport,
  VStack
} from "@carbon/react";
import { MODEL_RAW_KEEP_MAX_BYTES } from "@carbon/utils";
import { Trans, useLingui } from "@lingui/react/macro";
import type { FileObject } from "@supabase/storage-js";
import type { ChangeEvent } from "react";
import { useCallback } from "react";
import { LuEllipsisVertical, LuUpload } from "react-icons/lu";
import { Link, useFetchers, useSubmit } from "react-router";
import {
  DateTime,
  DocumentPreview,
  FileDropzone,
  Hyperlink,
  ModelOptimizedIndicator
} from "~/components";
import DocumentIcon from "~/components/DocumentIcon";
import { Enumerable } from "~/components/Enumerable";
import { useFileUpload, usePermissions, useUser } from "~/hooks";
import type { OptimisticFileObject } from "~/modules/shared/types";
import type { ModelUpload } from "~/types";
import { downloadModelFile } from "~/utils/download";
import { path } from "~/utils/path";
import { stripSpecialCharacters } from "~/utils/string";

const logger = getLogger("erp", "production", "job-documents");

const useJobDocuments = ({
  jobId,
  itemId
}: {
  jobId: string;
  itemId?: string | null;
}) => {
  const permissions = usePermissions();
  const { t } = useLingui();
  const revalidator = useRevalidator();
  const { carbon } = useCarbon();
  const { company } = useUser();
  const submit = useSubmit();

  const canDelete = permissions.can("delete", "production");
  const canUpdate = permissions.can("update", "production");

  const getPath = useCallback(
    (file: { name: string }, bucket: "job" | "parts" = "job") => {
      if (bucket === "parts" && itemId) {
        return `${company.id}/parts/${itemId}/${stripSpecialCharacters(
          file.name
        )}`;
      }
      return `${company.id}/job/${jobId}/${stripSpecialCharacters(file.name)}`;
    },
    [company.id, jobId, itemId]
  );

  const deleteFile = useCallback(
    async (file: FileObject & { bucket?: string }) => {
      const bucket = file.bucket === "parts" ? "parts" : "job";
      if (!carbon) {
        toast.error("Error deleting file");
        return;
      }
      const { error } = await storage(carbon)
        .company(company.id)
        .remove([getPath(file, bucket as "job" | "parts")]);

      if (error) {
        toast.error(error.message || "Error deleting file");
        return;
      }

      toast.success(t`${file.name} deleted successfully`);
      revalidator.revalidate();
    },
    [getPath, carbon, company.id, revalidator, t]
  );

  const deleteModel = useCallback(async () => {
    if (!carbon) return;

    const { error } = await carbon
      .from("job")
      .update({ modelUploadId: null })
      .eq("id", jobId);
    if (error) {
      toast.error(t`Error removing model from job`);
      return;
    }
    toast.success(t`Model removed from job`);
    revalidator.revalidate();
  }, [carbon, jobId, revalidator, t]);

  const downloadModel = useCallback(
    async (model: ModelUpload) => {
      const result = await downloadModelFile(model);
      if (result === "unavailable") {
        toast.error(t`The original model file is no longer available`);
      } else if (result === "error") {
        toast.error(t`Error downloading file`);
      }
    },

    [t]
  );

  // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
  const download = useCallback(
    async (file: FileObject & { bucket?: string }) => {
      const bucket = file.bucket === "parts" ? "parts" : "job";
      const url = path.to.file.previewFile(
        `private/${getPath(file, bucket as "job" | "parts")}`
      );
      try {
        await downloadUrl(url, file.name);
      } catch (error) {
        toast.error(t`Error downloading file`);
        logger.error("Failed to process file operation", { error });
      }
    },

    []
  );

  const getModelPath = useCallback((model: ModelUpload) => {
    if (!model?.modelId) {
      return "";
    }
    return path.to.file.cadModel(model.modelId);
  }, []);

  const createDocumentRecord = useCallback(
    ({
      path: filePath,
      name,
      size,
      bucket = "job"
    }: {
      path: string;
      name: string;
      size: number;
      bucket?: "job" | "parts";
    }) => {
      const formData = new FormData();
      formData.append("path", filePath);
      formData.append("name", name);
      formData.append("size", Math.round(size / 1024).toString());
      formData.append("sourceDocument", "Job");
      formData.append("sourceDocumentId", jobId);

      submit(formData, {
        method: "post",
        action: path.to.newDocument,
        navigate: false,
        fetcherKey: `job:${name}`
      });
    },
    [jobId, submit]
  );

  const { upload: uploadFiles } = useFileUpload();
  const upload = useCallback(
    async (files: File[], bucket: "job" | "parts" = "job") => {
      if (bucket === "parts" && !itemId) {
        toast.error(t`Cannot upload to parts bucket without item ID`);
        return;
      }

      await uploadFiles(files, {
        getPath: (file) => getPath(file, bucket),
        onSuccess: (file, uploadedPath) =>
          createDocumentRecord({
            path: uploadedPath,
            name: file.name,
            size: file.size,
            bucket
          })
      });
      revalidator.revalidate();
    },
    [uploadFiles, getPath, createDocumentRecord, revalidator, itemId, t]
  );

  const moveFile = useCallback(
    async (
      file: FileObject & { bucket?: string },
      targetBucket: "job" | "parts"
    ) => {
      if (!carbon) {
        toast.error(t`Carbon client not available`);
        return;
      }

      if (targetBucket === "parts" && !itemId) {
        toast.error(t`Cannot move to parts bucket without item ID`);
        return;
      }

      const currentBucket = file.bucket === "parts" ? "parts" : "job";

      if (currentBucket === targetBucket) {
        toast.error(t`File is already in the selected bucket`);
        return;
      }

      try {
        // Download the file first
        const sourcePath = getPath(file, currentBucket);
        const { data: downloadData } = await storage(carbon)
          .company(company.id)
          .download(sourcePath);

        if (!downloadData) {
          toast.error(t`Failed to download file for moving`);
          return;
        }

        // Upload to new location
        const targetPath = getPath(file, targetBucket);
        const { error: uploadError } = await storage(carbon)
          .company(company.id)
          .upload(targetPath, downloadData, {
            cacheControl: `${12 * 60 * 60}`,
            upsert: true
          });

        if (uploadError) {
          toast.error(t`Failed to upload file to new location`);
          return;
        }

        // Delete from old location
        const { error: deleteError } = await storage(carbon)
          .company(company.id)
          .remove([sourcePath]);

        if (deleteError) {
          toast.error(t`Failed to delete file from old location`);
          return;
        }

        toast.success(
          `Moved ${file.name} to ${
            targetBucket === "parts" ? "Item Master" : "Job"
          } bucket`
        );
        revalidator.revalidate();
      } catch (error) {
        toast.error(t`Error moving file`);
        logger.error("Failed to process file operation", { error });
      }
    },
    [carbon, itemId, getPath, revalidator, t, company.id]
  );

  return {
    canDelete,
    canUpdate,
    deleteFile,
    deleteModel,
    download,
    downloadModel,
    getPath,
    getModelPath,
    moveFile,
    upload
  };
};

type JobDocumentsProps = {
  files: FileObject[];
  jobId: string;
  itemId?: string | null;
  bucket?: "job" | "parts";
  modelUpload?: ModelUpload;
  isReadOnly?: boolean;
};

const JobDocuments = ({
  files,
  jobId,
  itemId,
  modelUpload,
  bucket = "job",
  isReadOnly
}: JobDocumentsProps) => {
  const { t } = useLingui();
  const {
    canDelete,
    canUpdate,
    download,
    downloadModel,
    deleteFile,
    deleteModel,
    getPath,
    getModelPath,
    moveFile,
    upload
  } = useJobDocuments({
    jobId,
    itemId
  });

  const onDrop = useCallback(
    (acceptedFiles: File[]) => {
      upload(acceptedFiles);
    },
    [upload]
  );

  const attachmentsByName = new Map<string, FileObject | OptimisticFileObject>(
    files.map((file) => [file.name, file])
  );
  const pendingItems = usePendingItems();
  for (let pendingItem of pendingItems) {
    let item = attachmentsByName.get(pendingItem.name);
    let merged = item ? { ...item, ...pendingItem } : pendingItem;
    attachmentsByName.set(pendingItem.name, merged);
  }

  const allFiles = Array.from(attachmentsByName.values()).sort((a, b) =>
    a.name.localeCompare(b.name)
  ) as FileObject[];

  // Phones: an empty card is one line; the header's New uploads.
  const { isPhone } = useViewport();
  const isEmpty = allFiles.length === 0 && !modelUpload?.modelName;

  return (
    <>
      <Card className="flex-grow">
        <HStack className="justify-between items-start">
          <CardHeader>
            <CardTitle>
              <Trans>Files</Trans>
            </CardTitle>
          </CardHeader>
          <CardAction>
            {!isReadOnly && (
              <JobDocumentForm jobId={jobId} itemId={itemId} bucket={bucket} />
            )}
          </CardAction>
        </HStack>
        <CardContent>
          {isPhone && isEmpty ? (
            <p className="text-sm text-muted-foreground">
              <Trans>No files</Trans>
            </p>
          ) : (
            <Table>
              <Thead>
                <Tr>
                  <Th>Name</Th>
                  <Th>Size</Th>
                  <Th>Bucket</Th>
                  <Th>Created</Th>
                  <Th />
                </Tr>
              </Thead>
              <Tbody>
                {modelUpload?.modelName &&
                  (modelUpload.modelSize ?? 0) <= MODEL_RAW_KEEP_MAX_BYTES && (
                    <Tr>
                      <Td>
                        <HStack>
                          <DocumentIcon type="Model" />
                          <VStack>
                            <Hyperlink
                              target="_blank"
                              to={getModelPath(modelUpload)}
                            >
                              {modelUpload.modelName}
                            </Hyperlink>
                          </VStack>
                          <ModelOptimizedIndicator
                            modelPath={modelUpload.modelPath}
                          />
                        </HStack>
                      </Td>
                      <Td>
                        {modelUpload.modelSize
                          ? convertKbToString(
                              Math.floor((modelUpload.modelSize ?? 0) / 1024)
                            )
                          : "--"}
                      </Td>
                      <Td>
                        <Enumerable value="Job" />
                      </Td>
                      <Td className="text-xs font-mono">--</Td>
                      <Td>
                        <div className="flex justify-end w-full">
                          <DropdownMenu>
                            <DropdownMenuTrigger asChild>
                              <IconButton
                                aria-label={t`More`}
                                icon={<LuEllipsisVertical />}
                                variant="secondary"
                              />
                            </DropdownMenuTrigger>
                            <DropdownMenuContent>
                              <DropdownMenuItem
                                shortcut={MENU_ITEM_SHORTCUTS.view}
                                asChild
                              >
                                <Link to={getModelPath(modelUpload)}>
                                  <Trans>View</Trans>
                                </Link>
                              </DropdownMenuItem>
                              <DropdownMenuItem
                                shortcut={MENU_ITEM_SHORTCUTS.download}
                                onClick={() => downloadModel(modelUpload)}
                              >
                                Download
                              </DropdownMenuItem>
                              <DropdownMenuItem
                                destructive
                                disabled={!canDelete || isReadOnly}
                                onClick={() => deleteModel()}
                              >
                                Delete
                              </DropdownMenuItem>
                            </DropdownMenuContent>
                          </DropdownMenu>
                        </div>
                      </Td>
                    </Tr>
                  )}
                {allFiles.map((file) => {
                  const type = getDocumentType(file.name);
                  return (
                    <Tr key={file.id}>
                      <Td>
                        <HStack>
                          <DocumentIcon type={type} />
                          <span
                            className="font-medium cursor-pointer"
                            onClick={() => {
                              if (["PDF", "Image"].includes(type)) {
                                const bucket =
                                  (file as any).bucket === "parts"
                                    ? "parts"
                                    : "job";
                                window.open(
                                  path.to.file.previewFile(
                                    `${"private"}/${getPath(
                                      file,
                                      bucket as "job" | "parts"
                                    )}`
                                  ),
                                  "_blank"
                                );
                              } else {
                                download(file);
                              }
                            }}
                          >
                            {["PDF", "Image"].includes(type) ? (
                              <DocumentPreview
                                bucket="private"
                                pathToFile={getPath(
                                  file,
                                  (file as any).bucket === "parts"
                                    ? "parts"
                                    : "job"
                                )}
                                // @ts-expect-error
                                type={type}
                              >
                                {file.name}
                              </DocumentPreview>
                            ) : (
                              file.name
                            )}
                          </span>
                        </HStack>
                      </Td>
                      <Td>
                        {convertKbToString(
                          Math.floor((file.metadata?.size ?? 0) / 1024)
                        )}
                      </Td>
                      <Td>
                        <Enumerable
                          value={
                            (file as any).bucket === "parts"
                              ? "Item"
                              : (file as any).bucket === "opportunity-line"
                                ? "Opportunity"
                                : "Job"
                          }
                        />
                      </Td>
                      <Td className="text-xs font-mono">
                        <DateTime
                          value={file.created_at}
                          variant="date"
                          fallback="--"
                        />
                      </Td>
                      <Td>
                        <div className="flex justify-end w-full">
                          <DropdownMenu>
                            <DropdownMenuTrigger asChild>
                              <IconButton
                                aria-label={t`More`}
                                icon={<LuEllipsisVertical />}
                                variant="secondary"
                              />
                            </DropdownMenuTrigger>
                            <DropdownMenuContent>
                              <DropdownMenuItem
                                shortcut={MENU_ITEM_SHORTCUTS.download}
                                onClick={() => download(file)}
                              >
                                Download
                              </DropdownMenuItem>
                              {itemId &&
                                (file as any).bucket !== "opportunity-line" && (
                                  <DropdownMenuSub>
                                    <DropdownMenuSubTrigger
                                      disabled={!canUpdate || isReadOnly}
                                    >
                                      Move to
                                    </DropdownMenuSubTrigger>
                                    <DropdownMenuSubContent>
                                      <DropdownMenuRadioGroup
                                        value={
                                          (file as any).bucket === "parts"
                                            ? "parts"
                                            : "job"
                                        }
                                        onValueChange={(value) =>
                                          moveFile(
                                            file,
                                            value as "job" | "parts"
                                          )
                                        }
                                      >
                                        <DropdownMenuRadioItem value="job">
                                          Job
                                        </DropdownMenuRadioItem>
                                        <DropdownMenuRadioItem value="parts">
                                          Item
                                        </DropdownMenuRadioItem>
                                      </DropdownMenuRadioGroup>
                                    </DropdownMenuSubContent>
                                  </DropdownMenuSub>
                                )}
                              <DropdownMenuItem
                                destructive
                                disabled={!canDelete || isReadOnly}
                                onClick={() => deleteFile(file)}
                              >
                                Delete
                              </DropdownMenuItem>
                            </DropdownMenuContent>
                          </DropdownMenu>
                        </div>
                      </Td>
                    </Tr>
                  );
                })}
                {allFiles.length === 0 && !modelUpload && (
                  <Tr>
                    <Td
                      colSpan={5}
                      className="py-8 text-muted-foreground text-center"
                    >
                      No files
                    </Td>
                  </Tr>
                )}
              </Tbody>
            </Table>
          )}
          {!isReadOnly && !(isPhone && isEmpty) && (
            <FileDropzone onDrop={onDrop} />
          )}
        </CardContent>
      </Card>
    </>
  );
};

export default JobDocuments;

type JobDocumentFormProps = {
  jobId: string;
  itemId?: string | null;
  bucket?: "job" | "parts";
};

const JobDocumentForm = ({
  jobId,
  itemId,
  bucket = "job"
}: JobDocumentFormProps) => {
  const permissions = usePermissions();
  const { upload } = useJobDocuments({ jobId, itemId });

  const uploadFiles = async (e: ChangeEvent<HTMLInputElement>) => {
    if (e.target.files) {
      upload(Array.from(e.target.files), bucket);
    }
  };

  return (
    <File
      isDisabled={!permissions.can("update", "production")}
      leftIcon={<LuUpload />}
      onChange={(e) => uploadFiles(e)}
      multiple
    >
      New
    </File>
  );
};

const usePendingItems = () => {
  type PendingItem = ReturnType<typeof useFetchers>[number] & {
    formData: FormData;
  };

  return useFetchers()
    .filter((fetcher): fetcher is PendingItem => {
      return fetcher.formAction === path.to.newDocument;
    })
    .reduce<OptimisticFileObject[]>((acc, fetcher) => {
      const path = fetcher.formData.get("path") as string;
      const name = fetcher.formData.get("name") as string;
      const size = parseInt(fetcher.formData.get("size") as string, 10) * 1024;

      if (path && name && size) {
        const newItem: OptimisticFileObject = {
          id: path,
          name: name,
          bucket_id: "private",
          bucket: "private",
          metadata: {
            size,
            mimetype: getDocumentType(name)
          }
        };
        return [...acc, newItem];
      }
      return acc;
    }, []);
};