import React from 'react'; import { Link } from 'react-router-dom'; import { useApp } from '../store/AppContext'; import Card from '../components/ui/Card'; import Button from '../components/ui/Button'; import Tag from '../components/ui/Tag'; import { storage } from '../lib/storage'; import { getAssignedTopic } from '../lib/learningService'; import { getTestResult } from '../lib/testService'; const Dashboard = () => { const { state } = useApp(); const { currentUser, weekNumber } = state; const topic = getAssignedTopic(currentUser?.id, weekNumber); const learnDone = storage.get(`user:${currentUser?.id}:week:${weekNumber}:learn_done`, false); const testResult = getTestResult(currentUser?.id, weekNumber); const leaderboard = storage.get('leaderboard:current', []).sort((a, b) => b.points - a.points); const top3 = leaderboard.slice(0, 3); const myRank = leaderboard.findIndex(u => u.userId === currentUser?.id) + 1; const myPoints = leaderboard.find(u => u.userId === currentUser?.id)?.points || 0; // Gather recent activity from past weeks const activity = []; for (let w = weekNumber; w >= Math.max(1, weekNumber - 3); w--) { const pastLearn = storage.get(`user:${currentUser?.id}:week:${w}:learn_done`, false); const pastTest = getTestResult(currentUser?.id, w); const pastTopic = getAssignedTopic(currentUser?.id, w); if (pastTest) { activity.push({ type: 'test', week: w, topic: pastTopic?.label, score: pastTest.percentage, points: pastTest.pointsEarned }); } if (pastLearn) { activity.push({ type: 'learn', week: w, topic: pastTopic?.label }); } } return (

Welcome, {currentUser?.name}

Here is your overview for week {weekNumber}.

Learning

Your topic this week:

{learnDone ? Completed : To Do}

{topic ? topic.label : 'No topic assigned'}

Testing

Weekly test — 10 questions

{testResult ? Score: {testResult.percentage}% : To Do}
{!learnDone ? 'Complete your learning session first' : testResult ? 'Test completed for this week' : 'Ready to test your knowledge'}
{learnDone && !testResult ? ( ) : ( )}

Recent Activity

{activity.length === 0 ? (
No recent activity.
) : ( activity.slice(0, 5).map((act, i) => (
{act.type === 'test' ? 'T' : 'L'}

{act.type === 'test' ? 'Test completed' : 'Learning session'}: {act.topic}

Week {act.week} {act.type === 'test' && `· Score: ${act.score}%`}

{act.points > 0 &&
+{act.points} pts
}
)) )}

Mini Leaderboard

{top3.length === 0 ? (
No points yet
) : ( top3.map((u, i) => (
{i + 1}. {u.name} {u.userId === currentUser?.id && '(You)'}
{u.points} pts
)) )} {myRank > 3 && (
{myRank}. You
{myPoints} pts
)}
); }; export default Dashboard;