|
| 1 | +"use client"; |
| 2 | +import React, { useEffect, useState } from "react"; |
| 3 | +import { useProgressStore } from "@/stores/progress-store"; |
| 4 | +import ControllerIcon from "@/components/ui/controller-icon"; |
| 5 | +import { Progress } from "@/components/ui/progress"; |
| 6 | +import { Trophy, BookOpen } from "lucide-react"; |
| 7 | + |
| 8 | +const UserProgressWidget = ({ |
| 9 | + totalGames, |
| 10 | + totalArticles, |
| 11 | +}: { |
| 12 | + totalGames: number; |
| 13 | + totalArticles: number; |
| 14 | +}) => { |
| 15 | + const [completedCounts, setCompletedCounts] = useState({ |
| 16 | + games: 0, |
| 17 | + articles: 0, |
| 18 | + }); |
| 19 | + const [animatedGameProgress, setAnimatedGameProgress] = useState(0); |
| 20 | + const [animatedArticleProgress, setAnimatedArticleProgress] = useState(0); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + const getCompletedCounts = useProgressStore.getState().getCompletedCounts; |
| 24 | + setCompletedCounts(getCompletedCounts()); |
| 25 | + |
| 26 | + const unsubscribe = useProgressStore.subscribe(() => |
| 27 | + setCompletedCounts(getCompletedCounts()) |
| 28 | + ); |
| 29 | + |
| 30 | + return () => unsubscribe(); |
| 31 | + }, []); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + const gameProgress = (completedCounts.games / totalGames) * 100; |
| 35 | + const articleProgress = (completedCounts.articles / totalArticles) * 100; |
| 36 | + |
| 37 | + const animateProgress = (current: number, target: number, setter: any) => { |
| 38 | + if (current < target) { |
| 39 | + const next = Math.min(current + 1, target); |
| 40 | + setter(next); |
| 41 | + setTimeout(() => animateProgress(next, target, setter), 20); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + animateProgress( |
| 46 | + animatedGameProgress, |
| 47 | + gameProgress, |
| 48 | + setAnimatedGameProgress |
| 49 | + ); |
| 50 | + animateProgress( |
| 51 | + animatedArticleProgress, |
| 52 | + articleProgress, |
| 53 | + setAnimatedArticleProgress |
| 54 | + ); |
| 55 | + }, [completedCounts, totalGames, totalArticles, animatedArticleProgress, animatedGameProgress]); |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="bg-muted p-4 rounded-xl shadow-md mt-3 md:my-3"> |
| 59 | + <div className="text-xl font-bold opacity-35 align-middle justify-end flex w-full"> |
| 60 | + {" "} |
| 61 | + Your Progress |
| 62 | + <Trophy className="ml-2 text-yellow-500" /> |
| 63 | + </div> |
| 64 | + <div className="space-y-2 grid grid-cols-1 md:grid-cols-2 xl:gap-9"> |
| 65 | + <div className="self-end"> |
| 66 | + <div className="flex justify-between items-center mb-2"> |
| 67 | + <span className="flex items-center"> |
| 68 | + <ControllerIcon /> |
| 69 | + Games Completed |
| 70 | + </span> |
| 71 | + <span className=" ml-2 font-semibold"> |
| 72 | + {completedCounts.games} / {totalGames} |
| 73 | + </span> |
| 74 | + </div> |
| 75 | + <Progress value={animatedGameProgress} className="h-2" /> |
| 76 | + </div> |
| 77 | + <div className="self-end"> |
| 78 | + <div className="flex justify-between items-center mb-2"> |
| 79 | + <span className="flex items-center"> |
| 80 | + <BookOpen className="mr-2 text-blue-500" /> |
| 81 | + Articles Read |
| 82 | + </span> |
| 83 | + <span className="font-semibold"> |
| 84 | + {completedCounts.articles} / {totalArticles} |
| 85 | + </span> |
| 86 | + </div> |
| 87 | + <Progress value={animatedArticleProgress} className="h-2" /> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default UserProgressWidget; |
0 commit comments