import React from "react";
import { tokens, spacing, typography } from "../tokens";
// ─── StatCard ──────────────────────────────────────────
// Extracted from the repeated KPI pattern in Dashboard, Outreach,
// Content Studio, Billing, Ticketing, Wallet, and Support pages.
//
// Before: Each page rebuilt this pattern from scratch with
// slightly different inline styles.
// After: } value="42" label="Total Guests" accentColor="var(--color-primary)" />
//
// Enhanced to support formatting (number | currency | percent),
// trend indicators, deltas, and an optional description.
export interface StatCardProps {
/** Icon element (typically from lucide-react) */
icon?: React.ReactNode;
/** Stat value (e.g. "42", "1.2K", "$1,250.00") */
value: string | number;
/** Stat label (e.g. "Total Guests") */
label: string;
/** Optional caption shown below the label */
description?: string;
/** CSS color for the icon and accent border */
accentColor?: string;
/** Alias for accentColor (kept for call-site compatibility) */
color?: string;
/** Value formatting: "number" | "currency" | "percent" */
format?: "number" | "currency" | "percent";
/** Trend direction: "up" | "down" | "neutral" */
trend?: "up" | "down" | "neutral";
/** Whether an "up" trend is good ("up") or bad ("down") */
goodWhen?: "up" | "down";
/** Previous-period value for delta comparison */
previous?: number | string;
/** Delta to show next to the trend indicator */
delta?: number;
/** Label/value pair shown alongside the trend */
trendLabels?: { label: string; value: string };
/** Period caption (e.g. "Monthly limit") */
period?: string;
/** Stagger animation flag (kept for call-site compatibility) */
animated?: boolean;
/** Stagger animation delay index (1-based) */
animationDelay?: number;
/** Click handler — makes the card interactive */
onClick?: () => void;
/** Additional CSS class */
className?: string;
/** Additional inline styles */
style?: React.CSSProperties;
}
function formatValue(value: string | number, format?: string): string {
const num = typeof value === "string" ? Number(value) : value;
if (format === "currency") {
return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(Number.isFinite(num) ? num : 0);
}
if (format === "percent") {
return `${Number.isFinite(num) ? num : 0}%`;
}
return String(value);
}
function trendColor(trend: string | undefined, goodWhen: string | undefined): string {
const isUp = trend === "up";
const isDown = trend === "down";
const good = goodWhen === "down" ? isDown : isUp;
const bad = goodWhen === "down" ? isUp : isDown;
if (good) return tokens.color.semantic.success;
if (bad) return tokens.color.semantic.error;
return tokens.color.text.tertiary;
}
export function StatCard({
icon,
value,
label,
description,
accentColor = "var(--color-primary)",
color,
format,
trend,
goodWhen,
previous,
delta,
trendLabels,
period,
animated,
animationDelay,
onClick,
className = "",
style,
}: StatCardProps) {
const resolvedColor = color || accentColor;
const displayValue = formatValue(value, format);
const hasTrend = trend !== undefined;
const hasDelta = delta !== undefined || previous !== undefined;
const showTrendRow = hasTrend || hasDelta || !!trendLabels || !!period;
const trendText =
trend === "up"
? "▲"
: trend === "down"
? "▼"
: "–";
return (
{icon && (
{icon}
)}
{displayValue}
{label}
{description && (
{description}
)}
{period && (
{period}
)}
{showTrendRow && (
{hasTrend && (
{trendText}
)}
{delta !== undefined && (
{delta >= 0 ? "+" : ""}{delta}
)}
{trendLabels && (
{trendLabels.label}: {trendLabels.value}
)}
)}
);
}
StatCard.displayName = "StatCard";