11"use client" ;
22
3- import { useEffect } from "react" ;
3+ import { useEffect , useState } from "react" ;
44import { Card , CardContent , CardHeader , CardTitle } from "@/components/ui/card" ;
55import { Skeleton } from "@/components/ui/skeleton" ;
66import { useAuthStore } from "@/store/AuthStore/useAuthStore" ;
77import DashboardNavbar from "@/components/dashboardComponents/DashboardNavbar" ;
88import { 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 ) ;
913
1014function DashboardContent ( { authUser } : any ) {
1115 return (
@@ -25,7 +29,7 @@ function DashboardContent({ authUser }: any) {
2529 </ p >
2630 </ CardContent >
2731 </ Card >
28-
32+ { /*
2933 <Card>
3034 <CardHeader>
3135 <CardTitle>LeetCode Stats</CardTitle>
@@ -35,24 +39,65 @@ function DashboardContent({ authUser }: any) {
3539 LeetCode stats are coming soon!
3640 </p>
3741 </CardContent>
38- </ Card >
42+ </Card> */ }
3943 </ main >
4044 ) ;
4145}
4246
4347export default function Dashboard ( ) {
4448 const { authUser, fetchAuthUser, authUserLoading } = useAuthStore ( ) ;
49+ const [ leetcodeStats , setLeetcodeStats ] = useState < any > ( null ) ;
4550
4651 useEffect ( ( ) => {
4752 fetchAuthUser ( ) ;
4853 } , [ 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+
4984 return (
5085 < div className = "min-h-screen w-full" >
5186 < DashboardNavbar isLoading = { authUserLoading } userId = { authUser ?. id } />
5287 { authUserLoading ? (
5388 < DashboardContentSkeleton />
5489 ) : (
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+ </ >
56101 ) }
57102 </ div >
58103 ) ;
0 commit comments