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.

1.8 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 { getDocumentType } from "@carbon/files";
import { Badge, cn, HStack } from "@carbon/react";
import { LuDownload } from "react-icons/lu";
import { DocumentPreview } from "~/components";
import DocumentIcon from "~/components/DocumentIcon";
import type { ItemType } from "~/modules/shared/types";
import type { ItemFile } from "../../types";
import { useItemDocuments } from "./ItemDocuments";

type FileBadgeProps = {
  file: ItemFile;
  itemId: string;
  itemType: ItemType;
  className?: string;
};

export function FileBadge({
  file,
  itemId,
  itemType,
  className
}: FileBadgeProps) {
  const { getPath, download } = useItemDocuments({ itemId, type: itemType });
  const type = getDocumentType(file.name);
  return (
    <HStack className="group" spacing={1}>
      {["PDF", "Image"].includes(type) ? (
        <DocumentPreview
          bucket="private"
          pathToFile={getPath(file)}
          // @ts-expect-error
          type={type}
        >
          <Badge variant="secondary" className={cn("max-w-[240px]", className)}>
            <DocumentIcon type={type} className="flex-shrink-0 w-3 h-3 mr-1" />
            {file.name}
          </Badge>
        </DocumentPreview>
      ) : (
        <Badge variant="secondary" className={cn("max-w-[240px]", className)}>
          <DocumentIcon type={type} className="flex-shrink-0 w-3 h-3 mr-1" />
          {file.name}
        </Badge>
      )}

      <LuDownload
        onClick={() => download(file)}
        className="cursor-pointer group-hover:opacity-100 md:opacity-0 transition-opacity duration-200 w-4 h-4 text-foreground"
      />
    </HStack>
  );
}