Spaces:
Sleeping

MicProtocol / packages /ui /src /components /StatCard.tsx
Benaah's picture
Upload folder using huggingface_hub
43842af verified
Raw History Blame Contribute Delete
7.4 kB
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: <StatCard icon={<Users />} 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 (
<div
className={`stat-card ${animationDelay ? `animate-reveal animate-reveal-delay-${animationDelay}` : ""} ${onClick ? "card-interactive" : ""} ${className}`}
style={{
...{
background: "var(--bg-elevated)",
border: "1px solid var(--border-color-subtle)",
borderRadius: tokens.borderRadius.lg,
padding: spacing[6],
display: "flex",
alignItems: "center",
gap: spacing[4],
transition: "box-shadow var(--transition-fast), border-color var(--transition-fast)",
position: "relative",
},
...(onClick ? { cursor: "pointer" } : {}),
...style,
}}
onClick={onClick}
role={onClick ? "button" : undefined}
tabIndex={onClick ? 0 : undefined}
aria-label={`${label}: ${displayValue}`}
>
{icon && (
<div
style={{
width: 40,
height: 40,
borderRadius: tokens.borderRadius.lg,
background: `color-mix(in srgb, ${resolvedColor} 10%, var(--bg-tertiary))`,
display: "flex",
alignItems: "center",
justifyContent: "center",
border: `1px solid color-mix(in srgb, ${resolvedColor} 16%, var(--border-color))`,
flexShrink: 0,
}}
>
<span style={{ color: resolvedColor }}>{icon}</span>
</div>
)}
<div style={{ flex: 1, minWidth: 0 }}>
<div
style={{
fontSize: typography.fontSize["2xl"],
fontWeight: 700,
fontFamily: "var(--font-display)",
letterSpacing: "-0.02em",
lineHeight: 1,
color: "var(--text-primary)",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
}}
>
{displayValue}
</div>
<div
style={{
fontSize: "var(--text-xs)",
color: "var(--text-tertiary)",
letterSpacing: "0.02em",
marginTop: 2,
}}
>
{label}
</div>
{description && (
<div
style={{
fontSize: "var(--text-xs)",
color: "var(--text-tertiary)",
marginTop: 1,
}}
>
{description}
</div>
)}
{period && (
<div
style={{
fontSize: "var(--text-xs)",
color: "var(--text-tertiary)",
marginTop: 1,
}}
>
{period}
</div>
)}
</div>
{showTrendRow && (
<div
style={{
display: "flex",
alignItems: "center",
gap: "var(--space-2)",
flexShrink: 0,
}}
>
{hasTrend && (
<span
style={{
fontSize: "var(--text-xs)",
fontWeight: 600,
color: trendColor(trend, goodWhen),
}}
>
{trendText}
</span>
)}
{delta !== undefined && (
<span
style={{
fontSize: "var(--text-xs)",
color: "var(--text-secondary)",
}}
>
{delta >= 0 ? "+" : ""}{delta}
</span>
)}
{trendLabels && (
<span
style={{
fontSize: "var(--text-xs)",
color: "var(--text-tertiary)",
}}
>
{trendLabels.label}: {trendLabels.value}
</span>
)}
</div>
)}
</div>
);
}
StatCard.displayName = "StatCard";