import React, { useState, useEffect } from 'react'; function Achievement() { const [stats, setStats] = useState({ total_sessions: 0, total_focus_time: 0, avg_focus_score: 0, streak_days: 0 }); const [systemStats, setSystemStats] = useState(null); const [badges, setBadges] = useState([]); const [loading, setLoading] = useState(true); // Format total focus time for display. const formatTime = (seconds) => { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); if (hours > 0) return `${hours}h ${minutes}m`; return `${minutes}m`; }; // Load summary statistics. useEffect(() => { fetch('/api/stats/summary') .then(res => res.json()) .then(data => { setStats(data); calculateBadges(data); setLoading(false); }) .catch(err => { console.error('Failed to load stats:', err); setLoading(false); }); }, []); // Derive unlocked badges from summary statistics. useEffect(() => { const fetchSystem = () => { fetch('/api/stats/system') .then(res => res.json()) .then(data => setSystemStats(data)) .catch(() => setSystemStats(null)); }; fetchSystem(); const interval = setInterval(fetchSystem, 3000); return () => clearInterval(interval); }, []); const calculateBadges = (data) => { const earnedBadges = []; // First-session badge if (data.total_sessions >= 1) { earnedBadges.push({ id: 'first-session', name: 'First Step', description: 'Complete your first focus session', icon: '๐ŸŽฏ', unlocked: true }); } // 10-session badge if (data.total_sessions >= 10) { earnedBadges.push({ id: 'ten-sessions', name: 'Getting Started', description: 'Complete 10 focus sessions', icon: 'โญ', unlocked: true }); } // 50-session badge if (data.total_sessions >= 50) { earnedBadges.push({ id: 'fifty-sessions', name: 'Dedicated', description: 'Complete 50 focus sessions', icon: '๐Ÿ†', unlocked: true }); } // Focus Master badge (average focus score > 80%) if (data.avg_focus_score >= 0.8 && data.total_sessions >= 5) { earnedBadges.push({ id: 'focus-master', name: 'Focus Master', description: 'Maintain 80%+ average focus score', icon: '๐Ÿง ', unlocked: true }); } // Streak badges if (data.streak_days >= 7) { earnedBadges.push({ id: 'week-streak', name: 'Week Warrior', description: '7 day streak', icon: '๐Ÿ”ฅ', unlocked: true }); } if (data.streak_days >= 30) { earnedBadges.push({ id: 'month-streak', name: 'Month Master', description: '30 day streak', icon: '๐Ÿ’Ž', unlocked: true }); } // Total focus time badge (10+ hours) if (data.total_focus_time >= 36000) { earnedBadges.push({ id: 'ten-hours', name: 'Endurance', description: '10+ hours total focus time', icon: 'โฑ๏ธ', unlocked: true }); } // Full badge catalog, including locked examples const allBadges = [ { id: 'first-session', name: 'First Step', description: 'Complete your first focus session', icon: '๐ŸŽฏ', unlocked: data.total_sessions >= 1 }, { id: 'ten-sessions', name: 'Getting Started', description: 'Complete 10 focus sessions', icon: 'โญ', unlocked: data.total_sessions >= 10 }, { id: 'fifty-sessions', name: 'Dedicated', description: 'Complete 50 focus sessions', icon: '๐Ÿ†', unlocked: data.total_sessions >= 50 }, { id: 'focus-master', name: 'Focus Master', description: 'Maintain 80%+ average focus score', icon: '๐Ÿง ', unlocked: data.avg_focus_score >= 0.8 && data.total_sessions >= 5 }, { id: 'week-streak', name: 'Week Warrior', description: '7 day streak', icon: '๐Ÿ”ฅ', unlocked: data.streak_days >= 7 }, { id: 'month-streak', name: 'Month Master', description: '30 day streak', icon: '๐Ÿ’Ž', unlocked: data.streak_days >= 30 }, { id: 'ten-hours', name: 'Endurance', description: '10+ hours total focus time', icon: 'โฑ๏ธ', unlocked: data.total_focus_time >= 36000 }, { id: 'hundred-sessions', name: 'Centurion', description: 'Complete 100 focus sessions', icon: '๐Ÿ‘‘', unlocked: data.total_sessions >= 100 } ]; setBadges(allBadges); }; return (

My Achievement

{loading ? (
Loading stats...
) : ( <> {systemStats && systemStats.cpu_percent != null && (
Server: CPU {systemStats.cpu_percent}% {' ยท '} RAM {systemStats.memory_percent}% {systemStats.memory_used_mb != null && ` (${systemStats.memory_used_mb}/${systemStats.memory_total_mb} MB)`}
)}
{stats.total_sessions}
Total Sessions
{formatTime(stats.total_focus_time)}
Total Focus Time
{(stats.avg_focus_score * 100).toFixed(1)}%
Average Focus
{stats.streak_days}
Day Streak

Badges

{badges.map(badge => (
{badge.unlocked ? badge.icon : '๐Ÿ”’'}
{badge.name}
{badge.description}
))}
)}
); } export default Achievement;