forked from Priyanshu-byte-coder/devtrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeeklySummaryCard.tsx
More file actions
265 lines (250 loc) · 10.5 KB
/
Copy pathWeeklySummaryCard.tsx
File metadata and controls
265 lines (250 loc) · 10.5 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
// @ts-nocheck
"use client";
import { ChevronDown } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { useAccount } from "@/components/AccountContext";
import { Skeleton } from "@/components/ui/skeleton";
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
interface WeeklySummaryData {
commits: {
current: number;
previous: number;
delta: number;
trend: "up" | "down" | "same";
};
prs: {
thisWeek: { opened: number; merged: number };
lastWeek: { opened: number; merged: number };
};
activeDays: { thisWeek: number; lastWeek: number };
streak: number;
topRepo: string | null;
}
export default function WeeklySummaryCard() {
const { selectedAccount } = useAccount();
const [summary, setSummary] = useState<WeeklySummaryData | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isCollapsed, setIsCollapsed] = useState(false);
const maxCommits = summary?.commits
? Math.max(summary.commits.current, summary.commits.previous, 1)
: 1;
const maxPRs = summary?.prs
? Math.max(summary.prs.thisWeek.merged, summary.prs.lastWeek.merged, 1)
: 1;
const maxActiveDays = summary?.activeDays
? Math.max(summary.activeDays.thisWeek, summary.activeDays.lastWeek, 1)
: 1;
const fetchSummary = useCallback(() => {
setLoading(true);
setError(null);
const url = selectedAccount !== null
? `/api/metrics/weekly-summary?accountId=${encodeURIComponent(selectedAccount)}`
: "/api/metrics/weekly-summary";
fetch(url)
.then((r) => {
if (!r.ok) throw new Error("API error");
return r.json();
})
.then((data: WeeklySummaryData) => setSummary(data))
.catch(() =>
setError(
"We couldn't load your weekly summary right now. Please try again in a moment."
)
)
.finally(() => setLoading(false));
}, [selectedAccount]);
useEffect(() => {
fetchSummary();
}, [fetchSummary]);
return (
<div className="rounded-xl border border-[var(--border)] bg-[var(--card)] p-6 shadow-sm transition-all duration-300 hover:shadow-md hover:-translate-y-1">
<div className="flex items-center justify-between">
<h2 className="text-lg font-semibold text-[var(--card-foreground)]">
This Week
</h2>
<button
type="button"
onClick={() => setIsCollapsed((value) => !value)}
className="text-sm text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
aria-expanded={!isCollapsed}
aria-label={
isCollapsed ? "Expand weekly summary" : "Collapse weekly summary"
}
suppressHydrationWarning
>
{isCollapsed ? ">" : "v"}
</Button>
</CardHeader>
{!isCollapsed &&
(loading ? (
<div
role="status"
aria-live="polite"
aria-busy="true"
className="mt-4 space-y-3"
>
<span className="sr-only">Loading weekly summary</span>
{[1, 2, 3, 4, 5].map((i) => (
<div
key={i}
aria-hidden="true"
className="h-14 rounded-lg skeleton-shimmer"
/>
))}
</div>
) : error ? (
<CardContent>
<div className="mt-4 rounded-lg border border-[var(--destructive)]/20 bg-[var(--destructive)]/10 p-4 text-sm text-[var(--destructive)]">
{error}
</div>
</CardContent>
) : summary && summary.commits && summary.prs && summary.activeDays ? (
<CardContent>
<div className="space-y-4 pt-2">
{/* Commits Comparison */}
<div className="rounded-lg bg-[var(--control)] p-4 stat-cell">
<div className="mb-3 flex items-center justify-between">
<span className="text-sm text-[var(--muted-foreground)]">
Commits
</span>
<span className="text-base font-semibold text-[var(--card-foreground)]">
{summary.commits.current}
{summary.commits.trend !== "same" && (
<span
className="ml-2 text-sm font-medium"
style={{
color: summary.commits.trend === "up" ? "var(--success)" : "var(--destructive)",
}}
>
{summary.commits.trend === "up" ? "+" : "-"}
{Math.abs(summary.commits.delta)}
</span>
)}
</span>
</div>
<div className="space-y-1.5">
<div className="flex items-center gap-2">
<span className="w-16 text-xs text-[var(--muted-foreground)]">Last week</span>
<div className="flex-1">
<div className="h-2 rounded bg-[var(--border)] overflow-hidden">
<div
className="h-full bg-[var(--muted-foreground)] progress-fill"
style={{
width: `${((summary.commits.previous / maxCommits) * 100).toFixed(0)}%`,
}}
/>
</div>
</div>
</div>
<div className="flex items-center gap-2">
<span className="w-16 text-xs text-[var(--muted-foreground)]">This week</span>
<div className="flex-1">
<div className="h-2 rounded bg-[var(--border)] overflow-hidden">
<div
className="h-full bg-[var(--success)] progress-fill"
style={{
width: `${((summary.commits.current / maxCommits) * 100).toFixed(0)}%`,
}}
/>
</div>
</div>
</div>
</div>
</div>
{/* PRs Comparison */}
<div className="rounded-lg bg-[var(--control)] p-4 stat-cell">
<div className="mb-3 flex items-center justify-between">
<span className="text-sm text-[var(--muted-foreground)]">PRs Merged</span>
<span className="text-base font-semibold text-[var(--card-foreground)]">
{summary.prs.thisWeek.merged}
</span>
</div>
<div className="space-y-1.5">
<div className="flex items-center gap-2">
<span className="w-16 text-xs text-[var(--muted-foreground)]">Last week</span>
<div className="flex-1">
<div className="h-2 rounded bg-[var(--border)] overflow-hidden">
<div
className="h-full bg-[var(--muted-foreground)]"
style={{
width: `${((summary.prs.lastWeek.merged / maxPRs) * 100).toFixed(0)}%`,
}}
/>
</div>
</div>
</div>
<div className="flex items-center gap-2">
<span className="w-16 text-xs text-[var(--muted-foreground)]">This week</span>
<div className="flex-1">
<div className="h-2 rounded bg-[var(--border)] overflow-hidden">
<div
className="h-full bg-[var(--success)]"
style={{
width: `${((summary.prs.thisWeek.merged / maxPRs) * 100).toFixed(0)}%`,
}}
/>
</div>
</div>
</div>
</div>
</div>
{/* Active Days Comparison */}
<div className="rounded-lg bg-[var(--control)] p-4 stat-cell">
<div className="mb-3 flex items-center justify-between">
<span className="text-sm text-[var(--muted-foreground)]">Active Days</span>
<span className="text-base font-semibold text-[var(--card-foreground)]">
{summary.activeDays.thisWeek} / 7
</span>
</div>
<div className="space-y-1.5">
<div className="flex items-center gap-2">
<span className="w-16 text-xs text-[var(--muted-foreground)]">Last week</span>
<div className="flex-1">
<div className="h-2 rounded bg-[var(--border)] overflow-hidden">
<div
className="h-full bg-[var(--muted-foreground)]"
style={{
width: `${((summary.activeDays.lastWeek / maxActiveDays) * 100).toFixed(0)}%`,
}}
/>
</div>
</div>
</div>
<div className="flex items-center gap-2">
<span className="w-16 text-xs text-[var(--muted-foreground)]">This week</span>
<div className="flex-1">
<div className="h-2 rounded bg-[var(--border)] overflow-hidden">
<div
className="h-full bg-[var(--success)]"
style={{
width: `${((summary.activeDays.thisWeek / maxActiveDays) * 100).toFixed(0)}%`,
}}
/>
</div>
</div>
</div>
</div>
</div>
{/* Streak & Top Repo */}
<div className="space-y-3">
<div className="flex items-center justify-between rounded-lg bg-[var(--control)] p-4 stat-cell">
<span className="text-sm text-[var(--muted-foreground)]">Streak</span>
<span className="text-base font-semibold text-[var(--card-foreground)]">
{summary.streak} day streak
</span>
</div>
<div className="flex items-center justify-between rounded-lg bg-[var(--control)] p-4 stat-cell">
<span className="text-sm text-[var(--muted-foreground)]">Top repo</span>
<span className="text-base font-semibold text-[var(--card-foreground)]">
{summary.topRepo ?? "-"}
</span>
</div>
</div>
</div>
</CardContent>
) : null)}
</Card>
);
}