forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveGistCounter.tsx
More file actions
326 lines (290 loc) · 8.83 KB
/
Copy pathLiveGistCounter.tsx
File metadata and controls
326 lines (290 loc) · 8.83 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'use client';
import { useState, useEffect, useRef, useCallback } from 'react';
const LOCATIONS = ['Abuja', 'Lagos', 'Kano', 'Ibadan', 'Port Harcourt', 'Enugu', 'Kaduna'];
const CATEGORIES = ['Events', 'Food', 'Safety', 'Tips', 'News', 'Transit', 'Markets', 'Other'];
const CATEGORY_COLORS: Record<string, string> = {
Events: '#6366f1',
Food: '#fb923c',
Safety: '#ef4444',
Tips: '#22c55e',
News: '#3b82f6',
Transit: '#a855f7',
Markets: '#eab308',
Other: '#9ca3af',
};
interface RecentGist {
id: string;
location: string;
category: string;
timestamp: number;
isNew: boolean;
}
function pick<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)];
}
function makeGist(): RecentGist {
return {
id: Math.random().toString(36).slice(2, 9),
location: pick(LOCATIONS),
category: pick(CATEGORIES),
timestamp: Date.now(),
isNew: true,
};
}
/** Returns a human-readable elapsed string, updates every second. */
function useElapsed(timestamp: number): string {
const [, tick] = useState(0);
useEffect(() => {
const id = setInterval(() => tick((n) => n + 1), 1000);
return () => clearInterval(id);
}, []);
const elapsed = Math.floor((Date.now() - timestamp) / 1000);
if (elapsed < 5) return 'Just now';
if (elapsed < 60) return `${elapsed}s ago`;
return `${Math.floor(elapsed / 60)}m ago`;
}
function GistRow({ gist }: { gist: RecentGist }) {
const elapsed = useElapsed(gist.timestamp);
const color = CATEGORY_COLORS[gist.category] ?? '#9ca3af';
return (
<li
style={{
display: 'flex',
alignItems: 'center',
gap: 10,
padding: '8px 12px',
borderRadius: 8,
marginBottom: 6,
background: gist.isNew ? 'rgba(99,102,241,0.08)' : 'transparent',
animation: gist.isNew ? 'gistSlideIn 0.35s ease' : undefined,
transition: 'background 0.6s ease',
listStyle: 'none',
}}
>
{/* Colored category dot */}
<span
style={{
width: 10,
height: 10,
borderRadius: '50%',
background: color,
flexShrink: 0,
animation: gist.isNew ? 'dotPulse 0.8s ease' : undefined,
}}
/>
{/* Category badge */}
<span
style={{
fontSize: 12,
fontWeight: 600,
color,
minWidth: 56,
}}
>
{gist.category}
</span>
{/* Location */}
<span style={{ fontSize: 13, flex: 1, color: '#374151' }}>
{gist.location}
</span>
{/* Timestamp */}
<span style={{ fontSize: 11, color: '#9ca3af', whiteSpace: 'nowrap' }}>
{elapsed}
</span>
</li>
);
}
/** Animates `displayCount` toward `target` over ~500 ms. */
function useCountAnimation(target: number): number {
const [display, setDisplay] = useState(target);
const rafRef = useRef<number | null>(null);
useEffect(() => {
const start = display;
const diff = target - start;
if (diff === 0) return;
const duration = 500;
const startTime = performance.now();
const step = (now: number) => {
const progress = Math.min((now - startTime) / duration, 1);
// ease-out cubic
const eased = 1 - Math.pow(1 - progress, 3);
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;
}
const INITIAL_COUNT = 1_284;
export default function LiveGistCounter() {
const [count, setCount] = useState(INITIAL_COUNT);
const [gists, setGists] = useState<RecentGist[]>([]);
const [flash, setFlash] = useState(false);
const [pulsing, setPulsing] = useState(false);
const displayCount = useCountAnimation(count);
/** Fire 1–5 gists at a random interval (12 s – 60 s). */
const scheduleNext = useCallback(() => {
// 12 000 ms = 5/min, 60 000 ms = 1/min
const delay = Math.floor(Math.random() * (60_000 - 12_000) + 12_000);
const id = setTimeout(() => {
const batch = Math.floor(Math.random() * 5) + 1;
const newGists = Array.from({ length: batch }, makeGist);
setCount((c) => c + batch);
setGists((prev) => {
const next = [...newGists, ...prev].slice(0, 5);
return next;
});
// Flash the counter
setFlash(true);
setTimeout(() => setFlash(false), 600);
// Pulse the live indicator
setPulsing(true);
setTimeout(() => setPulsing(false), 800);
// Clear isNew flag after animation
setTimeout(() => {
setGists((prev) =>
prev.map((g) => ({ ...g, isNew: false }))
);
}, 700);
scheduleNext();
}, delay);
return id;
}, []);
useEffect(() => {
// Seed with a couple of old gists so the list isn't empty on load
const seed: RecentGist[] = [
{ ...makeGist(), timestamp: Date.now() - 43_000, isNew: false },
{ ...makeGist(), timestamp: Date.now() - 91_000, isNew: false },
];
setGists(seed);
const id = scheduleNext();
return () => clearTimeout(id);
}, [scheduleNext]);
return (
<>
{/* Keyframe definitions */}
<style>{`
@keyframes flashBg {
0% { background: rgba(99,102,241,0.25); }
100% { background: rgba(99,102,241,0.06); }
}
@keyframes dotPulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(2.2); opacity: 0.6; }
100% { transform: scale(1); opacity: 1; }
}
@keyframes gistSlideIn {
from { opacity: 0; transform: translateY(-6px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes livePulse {
0%, 100% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.6); opacity: 0.5; }
}
@keyframes countFlash {
0% { color: #6366f1; }
100% { color: #111827; }
}
`}</style>
<div
style={{
maxWidth: 420,
margin: '0 auto',
fontFamily: 'inherit',
}}
>
{/* Counter card */}
<div
style={{
borderRadius: 16,
padding: '28px 32px',
background: flash ? undefined : 'rgba(99,102,241,0.06)',
animation: flash ? 'flashBg 0.6s ease forwards' : undefined,
border: '1px solid rgba(99,102,241,0.18)',
textAlign: 'center',
marginBottom: 20,
}}
>
{/* Live indicator */}
<div
style={{
display: 'inline-flex',
alignItems: 'center',
gap: 6,
marginBottom: 12,
}}
>
<span
style={{
display: 'inline-block',
width: 8,
height: 8,
borderRadius: '50%',
background: '#22c55e',
animation: pulsing ? 'livePulse 0.8s ease' : 'livePulse 2s ease infinite',
}}
/>
<span style={{ fontSize: 11, fontWeight: 600, color: '#6b7280', letterSpacing: '0.08em', textTransform: 'uppercase' }}>
Live
</span>
</div>
{/* The big number */}
<div
style={{
fontSize: 56,
fontWeight: 800,
lineHeight: 1,
color: flash ? undefined : '#111827',
animation: flash ? 'countFlash 0.6s ease forwards' : undefined,
letterSpacing: '-0.03em',
marginBottom: 6,
}}
>
{displayCount.toLocaleString()}
</div>
<div style={{ fontSize: 14, color: '#6b7280' }}>
total gists created
</div>
</div>
{/* Recent gists list */}
<div>
<div
style={{
fontSize: 12,
fontWeight: 600,
color: '#9ca3af',
letterSpacing: '0.07em',
textTransform: 'uppercase',
marginBottom: 8,
paddingLeft: 12,
}}
>
Recent activity
</div>
<ul style={{ margin: 0, padding: 0 }}>
{gists.map((g) => (
<GistRow key={g.id} gist={g} />
))}
{gists.length === 0 && (
<li
style={{
textAlign: 'center',
color: '#d1d5db',
fontSize: 13,
padding: 16,
listStyle: 'none',
}}
>
Waiting for gists…
</li>
)}
</ul>
</div>
</div>
</>
);
}