forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKPICard.tsx
More file actions
229 lines (208 loc) · 6.49 KB
/
Copy pathKPICard.tsx
File metadata and controls
229 lines (208 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use client';
import { useEffect, useRef, useState, memo } from 'react';
import {
TrendingUp,
TrendingDown,
Minus,
FileText,
Users,
BarChart2,
Activity,
type LucideIcon,
} from 'lucide-react';
// ── Animated counter ──────────────────────────────────────────────────────────
function useCountAnimation(target: number, duration = 1200): number {
const [display, setDisplay] = useState(0);
const rafRef = useRef<number | null>(null);
const initialised = useRef(false);
useEffect(() => {
const start = initialised.current ? display : 0;
initialised.current = true;
const diff = target - start;
if (diff === 0) return;
const startTime = performance.now();
const step = (now: number) => {
const progress = Math.min((now - startTime) / duration, 1);
const eased = 1 - Math.pow(1 - progress, 3); // ease-out cubic
setDisplay(Math.round(start + diff * eased));
if (progress < 1) rafRef.current = requestAnimationFrame(step);
};
rafRef.current = requestAnimationFrame(step);
return () => {
if (rafRef.current != null) cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [target]);
return display;
}
// ── Types ─────────────────────────────────────────────────────────────────────
export interface KPICardProps {
title: string;
/** Raw numeric value to count up to */
value: number;
/** Percentage change vs previous period (positive = up, negative = down) */
change: number;
/** Custom display formatter — defaults to locale-formatted integer */
format?: (n: number) => string;
icon: LucideIcon;
accentColor?: string;
}
// ── KPICard ───────────────────────────────────────────────────────────────────
export const KPICard = memo(function KPICard({
title,
value,
change,
format = (n) => n.toLocaleString(),
icon: Icon,
accentColor = '#6366f1',
}: KPICardProps) {
const displayValue = useCountAnimation(value);
const isUp = change > 0;
const isFlat = change === 0;
const trendColor = isUp ? '#22c55e' : isFlat ? '#9ca3af' : '#ef4444';
const TrendIcon = isUp ? TrendingUp : isFlat ? Minus : TrendingDown;
const sign = isUp ? '+' : '';
return (
<div
style={{
borderRadius: 16,
padding: '24px 28px',
background: '#ffffff',
border: '1px solid #e5e7eb',
boxShadow: '0 1px 4px rgba(0,0,0,0.06)',
display: 'flex',
flexDirection: 'column',
gap: 14,
}}
>
{/* Header: label + icon */}
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
<span
style={{
fontSize: 12,
fontWeight: 700,
color: '#6b7280',
textTransform: 'uppercase',
letterSpacing: '0.08em',
}}
>
{title}
</span>
<span
style={{
width: 36,
height: 36,
borderRadius: 10,
background: `${accentColor}1a`,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: accentColor,
flexShrink: 0,
}}
>
<Icon size={18} strokeWidth={2} />
</span>
</div>
{/* Animated value */}
<div
style={{
fontSize: 40,
fontWeight: 800,
lineHeight: 1,
color: '#111827',
letterSpacing: '-0.03em',
}}
>
{format(displayValue)}
</div>
{/* Trend indicator */}
<div style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
<span
style={{
display: 'inline-flex',
alignItems: 'center',
gap: 3,
fontSize: 13,
fontWeight: 700,
color: trendColor,
}}
>
<TrendIcon size={14} strokeWidth={2.5} />
{sign}{Math.abs(change).toFixed(1)}%
</span>
<span style={{ fontSize: 12, color: '#9ca3af' }}>vs last month</span>
</div>
</div>
);
});
// ── Default KPI dataset ───────────────────────────────────────────────────────
const KPI_DATA: KPICardProps[] = [
{
title: 'Total Gists',
value: 1_284,
change: 12.5,
icon: FileText,
accentColor: '#6366f1',
},
{
title: 'Active Users',
value: 847,
change: 8.3,
icon: Users,
accentColor: '#3b82f6',
},
{
title: 'Growth Rate',
value: 23,
change: 5.1,
format: (n) => `${n}%`,
icon: BarChart2,
accentColor: '#22c55e',
},
{
title: 'Engagement',
value: 3_291,
change: -2.8,
icon: Activity,
accentColor: '#f59e0b',
},
];
// ── KPIGrid ───────────────────────────────────────────────────────────────────
export default function KPIGrid() {
return (
<>
<style>{`
@keyframes kpiFadeUp {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
.kpi-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
}
@media (max-width: 1024px) {
.kpi-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
.kpi-grid { grid-template-columns: 1fr; }
}
.kpi-card-wrapper {
animation: kpiFadeUp 0.45s ease both;
}
.kpi-card-wrapper:nth-child(1) { animation-delay: 0ms; }
.kpi-card-wrapper:nth-child(2) { animation-delay: 80ms; }
.kpi-card-wrapper:nth-child(3) { animation-delay: 160ms; }
.kpi-card-wrapper:nth-child(4) { animation-delay: 240ms; }
`}</style>
<div className="kpi-grid">
{KPI_DATA.map((kpi) => (
<div key={kpi.title} className="kpi-card-wrapper">
<KPICard {...kpi} />
</div>
))}
</div>
</>
);
}