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.

2.6 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 { cn } from "@carbon/react";
import { cva } from "class-variance-authority";
import { useState } from "react";
import { LuBlocks } from "react-icons/lu";
import { getPrivateUrl } from "~/utils/path";
import { MethodItemTypeIcon } from "./Icons";

interface ItemThumbnailProps {
  thumbnailPath?: string | null;
  size?: "sm" | "md" | "lg" | "xl";
  type?: "Part" | "Material" | "Tool" | "Service" | "Consumable" | "Fixture";
  onClick?: () => void;
}

const itemVariants = cva(
  "bg-gradient-to-bl from-muted to-muted/40 rounded-lg",
  {
    variants: {
      size: {
        sm: "w-7 h-7",
        md: "w-9 h-9",
        lg: "w-11 h-11",
        xl: "w-16 h-16"
      },
      withPadding: {
        true: "",
        false: "p-0"
      }
    },
    compoundVariants: [
      {
        withPadding: true,
        size: "sm",
        class: "p-1"
      },
      {
        withPadding: true,
        size: "md",
        class: "p-1.5"
      },
      {
        withPadding: true,
        size: "lg",
        class: "p-2"
      },
      {
        withPadding: true,
        size: "xl",
        class: "p-2.5"
      }
    ],
    defaultVariants: {
      size: "md",
      withPadding: true
    }
  }
);

const iconVariants = cva("text-[#AAAAAA] dark:text-[#444]", {
  variants: {
    size: {
      sm: "w-5 h-5",
      md: "w-6 h-6",
      lg: "w-7 h-7",
      xl: "w-11 h-11"
    }
  },
  defaultVariants: {
    size: "md"
  }
});

const ItemThumbnail = ({
  thumbnailPath,
  type,
  size = "md",
  onClick
}: ItemThumbnailProps) => {
  // Keyed by path, not a bare boolean — a virtualized row reused for another
  // item would otherwise stay stuck on the fallback.
  const [failedPath, setFailedPath] = useState<string | null>(null);

  return thumbnailPath && failedPath !== thumbnailPath ? (
    <img
      loading="lazy"
      decoding="async"
      alt="thumbnail"
      className={cn(
        itemVariants({ size, withPadding: false }),
        thumbnailPath && "cursor-pointer"
      )}
      key={thumbnailPath}
      onError={() => setFailedPath(thumbnailPath)}
      src={getPrivateUrl(thumbnailPath)}
      onClick={onClick}
    />
  ) : (
    <div className={cn(itemVariants({ size }))}>
      {type ? (
        <MethodItemTypeIcon className={iconVariants({ size })} type={type} />
      ) : (
        <LuBlocks className={iconVariants({ size })} />
      )}
    </div>
  );
};

export default ItemThumbnail;