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.

6.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.

"use client";

import { useCallback, useEffect, useRef, useState } from "react";
import { cn } from "./utils/cn";

const BAR_WIDTH = 2;
const BAR_HEIGHT = 14;
const BAR_GAP = 3;

/**
 * Interpolates between two [r,g,b] colors at a given ratio (0-1).
 */
function lerpColor(
  a: [number, number, number],
  b: [number, number, number],
  t: number
): string {
  const r = Math.round(a[0] + (b[0] - a[0]) * t);
  const g = Math.round(a[1] + (b[1] - a[1]) * t);
  const bl = Math.round(a[2] + (b[2] - a[2]) * t);
  return `rgb(${r},${g},${bl})`;
}

type GradientStop = { pos: number; color: [number, number, number] };

// red -> yellow -> green
const GRADIENT_STOPS: GradientStop[] = [
  { pos: 0, color: [239, 68, 68] }, // red-500
  { pos: 0.5, color: [234, 179, 8] }, // yellow-500
  { pos: 0.8, color: [34, 197, 94] }, // green-500
  { pos: 1, color: [34, 197, 94] } // green-500
];

// green -> yellow -> red (inverted)
const GRADIENT_STOPS_INVERTED: GradientStop[] = [
  { pos: 0, color: [34, 197, 94] }, // green-500
  { pos: 0.2, color: [34, 197, 94] }, // green-500
  { pos: 0.5, color: [234, 179, 8] }, // yellow-500
  { pos: 0.8, color: [239, 68, 68] }, // red-500
  { pos: 1, color: [239, 68, 68] } // red-500
];

function getGradientColor(ratio: number, stops: GradientStop[]): string {
  const clamped = Math.min(Math.max(ratio, 0), 1);
  for (let i = 0; i < stops.length - 1; i++) {
    const curr = stops[i]!;
    const next = stops[i + 1]!;
    if (clamped >= curr.pos && clamped <= next.pos) {
      const t = (clamped - curr.pos) / (next.pos - curr.pos);
      return lerpColor(curr.color, next.color, t);
    }
  }
  return lerpColor(stops[0]!.color, stops[0]!.color, 0);
}

type Segment = {
  value: number;
  className: string;
};

interface BarProgressProps {
  /** Numeric progress value */
  progress: number;
  /** Maximum progress value (default: 100) */
  max?: number;
  /** Label displayed above-left of the bar */
  label?: string;
  /** Value string displayed above-right of the bar */
  value?: string;
  /** Enable a red-yellow-green gradient across active bars */
  gradient?: boolean;
  /** Invert the gradient to green-yellow-red */
  invertGradient?: boolean;
  /** Multi-segment progress (renders segments in order, ignores progress/gradient/activeClassName) */
  segments?: Segment[];
  /** Additional class names for the outer wrapper */
  className?: string;
  /** Class name for the active (filled) bars (ignored when gradient is true) */
  activeClassName?: string;
  /** Class name for the inactive (unfilled) bars */
  inactiveClassName?: string;
  /** Phones: label above the bar, value under it, value not truncated. */
  stackOnPhone?: boolean;
}

export function BarProgress({
  progress,
  max = 100,
  label,
  value,
  gradient = false,
  invertGradient = false,
  segments,
  className,
  activeClassName = "bg-emerald-500",
  inactiveClassName = "bg-muted",
  stackOnPhone = false
}: BarProgressProps) {
  const containerRef = useRef<HTMLDivElement>(null);
  const [barCount, setBarCount] = useState(0);

  const calculateBars = useCallback(() => {
    if (!containerRef.current) return;
    const width = containerRef.current.clientWidth;
    const count = Math.floor((width + BAR_GAP) / (BAR_WIDTH + BAR_GAP));
    setBarCount(Math.max(count, 1));
  }, []);

  useEffect(() => {
    const el = containerRef.current;
    if (!el) return;

    calculateBars();

    const observer = new ResizeObserver(calculateBars);
    observer.observe(el);
    return () => observer.disconnect();
  }, [calculateBars]);

  const clampedProgress = Math.min(Math.max(progress, 0), max);
  const percentage = (clampedProgress / max) * 100;
  const activeBars = Math.round((clampedProgress / max) * barCount);

  const hasHeader = label || value;

  const getBarClassName = (i: number): string => {
    if (segments && barCount > 0) {
      let cumulative = 0;
      for (const seg of segments) {
        const segBars = Math.round((seg.value / max) * barCount);
        cumulative += segBars;
        if (i < cumulative) return seg.className;
      }
      return inactiveClassName;
    }

    const isActive = i < activeBars;
    if (gradient && isActive) return "";
    if (isActive) return activeClassName;
    return inactiveClassName;
  };

  const getBarStyle = (i: number): React.CSSProperties | undefined => {
    if (segments) return undefined;
    const isActive = i < activeBars;
    if (isActive && gradient && barCount > 0) {
      return {
        backgroundColor: getGradientColor(
          i / (barCount - 1 || 1),
          invertGradient ? GRADIENT_STOPS_INVERTED : GRADIENT_STOPS
        )
      };
    }
    return undefined;
  };

  return (
    <div className={cn("w-full", className)}>
      {hasHeader && (
        <div className="mb-0.5 flex items-baseline justify-between gap-2">
          {label ? (
            <span
              className={cn(
                "shrink-0 text-sm font-medium text-foreground",
                stackOnPhone && "max-md:min-w-0 max-md:truncate max-md:text-xs"
              )}
            >
              {label}
            </span>
          ) : (
            <div />
          )}
          {value && (
            <span
              title={value}
              className={cn(
                "min-w-0 truncate text-xs font-mono tabular-nums text-muted-foreground",
                stackOnPhone && "max-md:hidden"
              )}
            >
              {value}
            </span>
          )}
        </div>
      )}
      <div
        ref={containerRef}
        role="progressbar"
        aria-valuenow={clampedProgress}
        aria-valuemin={0}
        aria-valuemax={max}
        aria-label={label ?? "Progress"}
        className="flex w-full items-center gap-[3px]"
      >
        {barCount > 0 &&
          Array.from({ length: barCount }, (_, i) => (
            <span
              key={i}
              aria-hidden="true"
              className={cn(
                "shrink-0 rounded-[2px] transition-colors duration-200",
                getBarClassName(i)
              )}
              style={{
                width: BAR_WIDTH,
                height: BAR_HEIGHT,
                ...getBarStyle(i)
              }}
            />
          ))}
        <span className="sr-only">{Math.round(percentage)}%</span>
      </div>
      {stackOnPhone && value ? (
        <span className="mt-1 hidden break-words text-xs font-mono tabular-nums text-muted-foreground max-md:block">
          {value}
        </span>
      ) : null}
    </div>
  );
}