import { useState, useEffect } from 'react'; import { MdArrowUpward, MdArrowDownward } from 'react-icons/md'; import Stats from '../api/stats'; import variables from 'config/variables'; const SessionStats = () => { const [sessions, setSessions] = useState([]); const [orderBy, setOrderBy] = useState('startTime'); const [order, setOrder] = useState('desc'); useEffect(() => { const loadSessions = async () => { const stats = await Stats.getAllSessionStats(); setSessions(stats); }; loadSessions(); // Optional: Set up periodic refresh if needed const refreshInterval = setInterval(loadSessions, 30000); // Refresh every 30 seconds return () => clearInterval(refreshInterval); }, []); const formatDuration = (ms) => { const seconds = Math.floor(ms / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); if (hours > 0) { return `${hours}h ${minutes % 60}m`; } if (minutes > 0) { return `${minutes}m ${seconds % 60}s`; } return `${seconds}s`; }; const formatDate = (dateString) => { return new Date(dateString).toLocaleString(); }; const handleSort = (property) => { const isAsc = orderBy === property && order === 'asc'; setOrder(isAsc ? 'desc' : 'asc'); setOrderBy(property); }; const sortedSessions = [...sessions].sort((a, b) => { const multiplier = order === 'asc' ? 1 : -1; switch (orderBy) { case 'startTime': return multiplier * (new Date(a.startTime) - new Date(b.startTime)); case 'duration': return multiplier * (a.duration - b.duration); case 'eventCount': return multiplier * (a.eventCount - b.eventCount); case 'totalXp': return multiplier * (a.totalXp - b.totalXp); default: return 0; } }); const SortButton = ({ active, direction, onClick, children }) => ( ); return (
{sortedSessions.length === 0 ? ( ) : ( sortedSessions.map((session) => ( )) )}
handleSort('startTime')} > Session Start handleSort('duration')} > Duration handleSort('eventCount')} > Events handleSort('totalXp')} > XP Gained
No sessions recorded yet
{formatDate(session.startTime)} {formatDuration(session.duration)} {session.eventCount} {session.totalXp}
); }; export default SessionStats;