-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsection-cards-live.tsx
More file actions
131 lines (123 loc) · 3.36 KB
/
section-cards-live.tsx
File metadata and controls
131 lines (123 loc) · 3.36 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
"use client";
import { useEffect, useState, useCallback } from "react";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { FileVideo, Flag, Handshake, DollarSign, RefreshCw } from "lucide-react";
import { Button } from "@/components/ui/button";
import { POLL_INTERVAL_MS, type DashboardMetrics } from "@/lib/types/dashboard";
export function SectionCardsLive({
initialMetrics,
}: {
initialMetrics?: DashboardMetrics;
}) {
const [metrics, setMetrics] = useState<DashboardMetrics>(
initialMetrics ?? {
videosPublished: 0,
flaggedForReview: 0,
sponsorPipeline: 0,
revenue: null,
},
);
const [lastUpdated, setLastUpdated] = useState<Date>(new Date());
const [isRefreshing, setIsRefreshing] = useState(false);
const [secondsAgo, setSecondsAgo] = useState(0);
const fetchMetrics = useCallback(async () => {
try {
setIsRefreshing(true);
const res = await fetch("/api/dashboard/metrics");
if (res.ok) {
const data = await res.json();
setMetrics(data);
setLastUpdated(new Date());
setSecondsAgo(0);
}
} catch (error) {
console.error("Failed to refresh metrics:", error);
} finally {
setIsRefreshing(false);
}
}, []);
useEffect(() => {
fetchMetrics();
const interval = setInterval(fetchMetrics, POLL_INTERVAL_MS);
return () => clearInterval(interval);
}, [fetchMetrics]);
useEffect(() => {
const tick = setInterval(() => {
setSecondsAgo(Math.floor((Date.now() - lastUpdated.getTime()) / 1000));
}, 1000);
return () => clearInterval(tick);
}, [lastUpdated]);
const timeAgo =
secondsAgo < 5
? "just now"
: secondsAgo < 60
? `${secondsAgo}s ago`
: `${Math.floor(secondsAgo / 60)}m ago`;
const cards = [
{
title: "Videos Published",
value: String(metrics.videosPublished),
description: "Total automated videos published",
icon: FileVideo,
},
{
title: "Flagged for Review",
value: String(metrics.flaggedForReview),
description: "Content needing attention",
icon: Flag,
},
{
title: "Sponsor Pipeline",
value: String(metrics.sponsorPipeline),
description: "Active sponsor leads",
icon: Handshake,
},
{
title: "Revenue",
value: metrics.revenue != null ? `$${metrics.revenue}` : "\u2014",
description: "Monthly sponsor revenue",
icon: DollarSign,
},
];
return (
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<p className="text-xs text-muted-foreground">Updated {timeAgo}</p>
<Button
variant="ghost"
size="sm"
className="h-6 gap-1 px-2 text-xs text-muted-foreground"
onClick={fetchMetrics}
disabled={isRefreshing}
>
<RefreshCw
className={`h-3 w-3 ${isRefreshing ? "animate-spin" : ""}`}
/>
Refresh
</Button>
</div>
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
{cards.map((card) => (
<Card key={card.title}>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">
{card.title}
</CardTitle>
<card.icon className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{card.value}</div>
<CardDescription>{card.description}</CardDescription>
</CardContent>
</Card>
))}
</div>
</div>
);
}