Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.5 KB
Raw Blame
import "@editor/billing/MeterBar.css";
import type { ReactNode } from "react";
import type { MeterState } from "@editor/billing/format";
import { StatusBadge, type StatusTone } from "@editor/ui/StatusBadge";

// Meter state → the shared StatusBadge tone (contrast-tuned per theme).
const STATE_TONE: Record<MeterState, StatusTone> = {
  FULL: "success",
  WARNED: "warning",
  DEGRADED: "danger",
};

interface MeterBarProps {
  state: MeterState;
  /** Fill percentage 0–100. */
  pct: number;
  /** The big figure — a used count or a spend amount ("120", "$45"). */
  figure: ReactNode;
  /** Suffix after the figure ("/ 500 free PDFs", "/ $1,000 cap", "no cap"). */
  capSuffix: ReactNode;
  /** Status-chip content; null/undefined hides the chip. */
  statusLabel?: ReactNode;
  /** Footer meta line; null/undefined hides the line entirely. */
  meta?: ReactNode;
  /** Hide the fill bar (e.g. uncapped). Shown by default. */
  showBar?: boolean;
  /** Accessible name for the fill bar — what the meter measures ("Spend limit"). */
  barLabel: string;
}

/**
 * The usage-meter bar (the {@code paygf-meter} block) shared by the editor cloud
 * surface and the admin portal. Callers own the copy — the editor passes i18n
 * strings, the portal passes literals — so this carries no i18n dependency.
 * Styling comes from each app's own {@code paygf-meter}/{@code payg-bar}/{@code
 * payg-status} CSS.
 */
export function MeterBar({
  state,
  pct,
  figure,
  capSuffix,
  statusLabel,
  meta,
  showBar = true,
  barLabel,
}: MeterBarProps) {
  return (
    <div className="paygf-meter" data-state={state}>
      <div className="paygf-meter__top">
        <div className="paygf-meter__figure">
          <span className="paygf-meter__num">{figure}</span>
          <span className="paygf-meter__cap">{capSuffix}</span>
        </div>
        {statusLabel != null && (
          <StatusBadge tone={STATE_TONE[state]} size="sm" showDot>
            {statusLabel}
          </StatusBadge>
        )}
      </div>
      {showBar && (
        <div
          className="payg-bar"
          role="progressbar"
          aria-valuenow={Math.round(pct)}
          aria-valuemin={0}
          aria-valuemax={100}
          aria-label={barLabel}
        >
          <div
            className="payg-bar__fill"
            data-state={state}
            style={{ width: `${pct}%` }}
          />
        </div>
      )}
      {meta != null && <div className="paygf-meter__meta">{meta}</div>}
    </div>
  );
}