1
1
"use client" ;
2
2
3
- import { useEffect } from "react" ;
3
+ import { useEffect , useState } from "react" ;
4
4
import { Card , CardContent , CardHeader , CardTitle } from "@/components/ui/card" ;
5
5
import { Skeleton } from "@/components/ui/skeleton" ;
6
6
import { useAuthStore } from "@/store/AuthStore/useAuthStore" ;
7
7
import DashboardNavbar from "@/components/dashboardComponents/DashboardNavbar" ;
8
8
import { DashboardContentSkeleton } from "@/components/dashboardComponents/DashboardContentSkeleton" ;
9
+ import { Bar } from "react-chartjs-2" ;
10
+ import { Chart as ChartJS , CategoryScale , LinearScale , BarElement } from "chart.js" ;
11
+
12
+ ChartJS . register ( CategoryScale , LinearScale , BarElement ) ;
9
13
10
14
function DashboardContent ( { authUser } : any ) {
11
15
return (
@@ -25,7 +29,7 @@ function DashboardContent({ authUser }: any) {
25
29
</ p >
26
30
</ CardContent >
27
31
</ Card >
28
-
32
+ { /*
29
33
<Card>
30
34
<CardHeader>
31
35
<CardTitle>LeetCode Stats</CardTitle>
@@ -35,24 +39,65 @@ function DashboardContent({ authUser }: any) {
35
39
LeetCode stats are coming soon!
36
40
</p>
37
41
</CardContent>
38
- </ Card >
42
+ </Card> */ }
39
43
</ main >
40
44
) ;
41
45
}
42
46
43
47
export default function Dashboard ( ) {
44
48
const { authUser, fetchAuthUser, authUserLoading } = useAuthStore ( ) ;
49
+ const [ leetcodeStats , setLeetcodeStats ] = useState < any > ( null ) ;
45
50
46
51
useEffect ( ( ) => {
47
52
fetchAuthUser ( ) ;
48
53
} , [ fetchAuthUser ] ) ;
54
+
55
+ // Fetch and store stats once we have the user
56
+ useEffect ( ( ) => {
57
+ if ( authUser ?. leetcodeUsername && authUser ?. id ) {
58
+ const fetchStats = async ( ) => {
59
+ const res = await fetch (
60
+ `/api/leetcode?username=${ authUser . leetcodeUsername } &id=${ authUser . id } ` ,
61
+ { method : "POST" }
62
+ ) ;
63
+ if ( res . ok ) {
64
+ const data = await res . json ( ) ;
65
+ setLeetcodeStats ( data . stats ) ;
66
+ }
67
+ } ;
68
+ fetchStats ( ) ;
69
+ }
70
+ } , [ authUser ] ) ;
71
+
72
+ // Simple bar chart config
73
+ const chartData = {
74
+ labels : [ "Easy" , "Medium" , "Hard" ] ,
75
+ datasets : [
76
+ {
77
+ label : "Solved" ,
78
+ data : leetcodeStats ?. submitStats . acSubmissionNum ?. map ( ( i : any ) => i . count ) || [ 0 , 0 , 0 ] ,
79
+ backgroundColor : [ "#4ade80" , "#fbbf24" , "#ef4444" ] ,
80
+ } ,
81
+ ] ,
82
+ } ;
83
+
49
84
return (
50
85
< div className = "min-h-screen w-full" >
51
86
< DashboardNavbar isLoading = { authUserLoading } userId = { authUser ?. id } />
52
87
{ authUserLoading ? (
53
88
< DashboardContentSkeleton />
54
89
) : (
55
- < DashboardContent authUser = { authUser } />
90
+ < >
91
+ < DashboardContent authUser = { authUser } />
92
+ { leetcodeStats && (
93
+ < div className = "container mx-auto mt-6" >
94
+ < h2 className = "text-xl font-semibold mb-4" > Your LeetCode Progress</ h2 >
95
+ < div className = "max-w-xl" >
96
+ < Bar data = { chartData } />
97
+ </ div >
98
+ </ div >
99
+ ) }
100
+ </ >
56
101
) }
57
102
</ div >
58
103
) ;
0 commit comments