2026-01-07 09:31:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
import { Card } from '../ui/card';
|
|
|
|
|
import { ArrowUp, ArrowDown } from 'lucide-react';
|
|
|
|
|
|
|
|
|
|
interface StatCardProps {
|
|
|
|
|
label: string;
|
|
|
|
|
value: string | number;
|
|
|
|
|
icon: React.ReactNode;
|
|
|
|
|
trend?: string | number; // String like "+12%" or raw number
|
|
|
|
|
color?: 'cyan' | 'magenta' | 'lime' | 'gold' | 'red';
|
|
|
|
|
sparklineData?: number[];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
export const StatCard: React.FC<StatCardProps> = ({
|
|
|
|
|
label,
|
|
|
|
|
value,
|
|
|
|
|
icon,
|
|
|
|
|
trend,
|
|
|
|
|
color = 'cyan',
|
|
|
|
|
sparklineData,
|
|
|
|
|
}) => {
|
2026-02-07 18:34:45 +00:00
|
|
|
/* Semantic tokens (audit P3: unify kodo-* → primary/muted/success/warning/destructive) */
|
2026-01-07 09:31:02 +00:00
|
|
|
const colorMap = {
|
2026-02-07 18:34:45 +00:00
|
|
|
cyan: 'text-primary',
|
|
|
|
|
magenta: 'text-secondary',
|
|
|
|
|
lime: 'text-success',
|
|
|
|
|
gold: 'text-warning',
|
|
|
|
|
red: 'text-destructive',
|
2026-01-07 09:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const bgMap = {
|
2026-02-07 18:34:45 +00:00
|
|
|
cyan: 'bg-primary/10',
|
|
|
|
|
magenta: 'bg-secondary/10',
|
|
|
|
|
lime: 'bg-success/10',
|
|
|
|
|
gold: 'bg-warning/10',
|
|
|
|
|
red: 'bg-destructive/10',
|
2026-01-07 09:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderSparkline = (data: number[]) => {
|
2026-01-13 18:47:57 +00:00
|
|
|
if (!data || data.length < 2) return null;
|
|
|
|
|
const min = Math.min(...data);
|
|
|
|
|
const max = Math.max(...data);
|
|
|
|
|
const range = max - min || 1;
|
|
|
|
|
const width = 100;
|
|
|
|
|
const height = 40;
|
|
|
|
|
|
2026-01-26 13:12:17 +00:00
|
|
|
const getPathData = (d: number[]) => {
|
|
|
|
|
const points = d.map((val, i) => ({
|
|
|
|
|
x: (i / (d.length - 1)) * width,
|
|
|
|
|
y: height - ((val - min) / range) * height,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let pathStr = `M ${points[0].x},${points[0].y}`;
|
|
|
|
|
for (let i = 0; i < points.length - 1; i++) {
|
|
|
|
|
const curr = points[i];
|
|
|
|
|
const next = points[i + 1];
|
|
|
|
|
const mx = (curr.x + next.x) / 2;
|
|
|
|
|
pathStr += ` C ${mx},${curr.y} ${mx},${next.y} ${next.x},${next.y}`;
|
|
|
|
|
}
|
|
|
|
|
return pathStr;
|
|
|
|
|
};
|
2026-01-07 09:31:02 +00:00
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
return (
|
|
|
|
|
<svg
|
|
|
|
|
width="100%"
|
|
|
|
|
height={height}
|
|
|
|
|
viewBox={`0 0 ${width} ${height}`}
|
2026-01-26 13:12:17 +00:00
|
|
|
className="opacity-60 overflow-visible"
|
2026-01-13 18:47:57 +00:00
|
|
|
>
|
2026-01-26 13:12:17 +00:00
|
|
|
<path
|
|
|
|
|
d={getPathData(data)}
|
2026-01-13 18:47:57 +00:00
|
|
|
fill="none"
|
|
|
|
|
stroke="currentColor"
|
2026-01-26 13:12:17 +00:00
|
|
|
strokeWidth="2.5"
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
2026-01-13 18:47:57 +00:00
|
|
|
vectorEffect="non-scaling-stroke"
|
2026-01-26 13:12:17 +00:00
|
|
|
className="drop-shadow-[0_0_8px_currentColor]"
|
2026-01-13 18:47:57 +00:00
|
|
|
/>
|
|
|
|
|
</svg>
|
|
|
|
|
);
|
2026-01-07 09:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
const isPositive =
|
|
|
|
|
typeof trend === 'string' ? !trend.startsWith('-') : (trend || 0) >= 0;
|
2026-01-07 09:31:02 +00:00
|
|
|
const trendValue = typeof trend === 'number' ? `${Math.abs(trend)}%` : trend;
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-13 18:47:57 +00:00
|
|
|
<Card
|
|
|
|
|
variant="default"
|
aesthetic-improvements: align spacing to 8px grid (Action 11.2.1.3)
- Created automated script (scripts/align-8px-grid.py) to align all spacing to 8px grid
- Replaced non-8px-aligned spacing: gap-3/p-3/m-3 (12px) → gap-4/p-4/m-4 (16px), gap-5/p-5/m-5 (20px) → gap-6/p-6/m-6 (24px), gap-10/p-10/m-10 (40px) → gap-12/p-12/m-12 (48px), gap-20/p-20/m-20 (80px) → gap-24/p-24/m-24 (96px)
- Preserved: 4px values (gap-1, p-1, m-1) as they may be intentional fine-tuning, responsive breakpoints (sm:, md:, lg:), test files, documentation
- Modified files across all components to ensure consistent 8px grid alignment
- Action 11.2.1.3: Align all elements to 8px grid - COMPLETE
2026-01-16 10:50:46 +00:00
|
|
|
className="flex flex-col justify-between h-full p-6 hover:border-opacity-100 transition-all relative overflow-hidden"
|
2026-01-13 18:47:57 +00:00
|
|
|
>
|
2026-01-07 09:31:02 +00:00
|
|
|
<div className="flex justify-between items-start mb-2 relative z-10">
|
2026-01-13 18:47:57 +00:00
|
|
|
<div>
|
2026-02-07 18:34:45 +00:00
|
|
|
<p className="text-xs font-mono text-muted-foreground uppercase tracking-widest mb-1">
|
2026-01-13 18:47:57 +00:00
|
|
|
{label}
|
|
|
|
|
</p>
|
|
|
|
|
<h3 className="text-2xl font-display font-bold text-white">
|
|
|
|
|
{value}
|
|
|
|
|
</h3>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`p-2 rounded-lg ${bgMap[color]} ${colorMap[color]}`}>
|
|
|
|
|
{icon}
|
|
|
|
|
</div>
|
2026-01-07 09:31:02 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="relative z-10 flex items-end justify-between mt-2">
|
2026-01-13 18:47:57 +00:00
|
|
|
{trend && (
|
|
|
|
|
<div
|
2026-02-07 18:34:45 +00:00
|
|
|
className={`flex items-center gap-1 text-xs font-bold ${isPositive ? 'text-success' : 'text-destructive'}`}
|
2026-01-13 18:47:57 +00:00
|
|
|
>
|
|
|
|
|
{isPositive ? (
|
|
|
|
|
<ArrowUp className="w-3 h-3" />
|
|
|
|
|
) : (
|
|
|
|
|
<ArrowDown className="w-3 h-3" />
|
|
|
|
|
)}
|
|
|
|
|
{trendValue}{' '}
|
2026-02-07 18:34:45 +00:00
|
|
|
<span className="text-muted-foreground font-normal">vs last period</span>
|
2026-01-13 18:47:57 +00:00
|
|
|
</div>
|
|
|
|
|
)}
|
2026-01-07 09:31:02 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{sparklineData && (
|
2026-01-13 18:47:57 +00:00
|
|
|
<div
|
|
|
|
|
className={`absolute bottom-0 left-0 right-0 h-12 ${colorMap[color]} opacity-20 pointer-events-none`}
|
|
|
|
|
>
|
|
|
|
|
{renderSparkline(sparklineData)}
|
|
|
|
|
</div>
|
2026-01-07 09:31:02 +00:00
|
|
|
)}
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
};
|