import { useContext, useState } from 'preact/hooks' import { SolutionStat } from '../../shared/model' import { sortByNumericKey, sortByStringKey } from '../../shared/utils' import { prependBaseUrl } from '../../shared/utils' import { Header } from '../components/Header' import { useResource, MetadataContext } from '../hooks' export const ScoresPage = () => { const metadata = useContext(MetadataContext) metadata.title = `PHC Problemi` metadata.description = 'Bacheca di problemi del PHC' const [stats] = useResource>(`/api/stats`, {}) type Column = 'student' | 'sent' | 'correct' type Order = 'ascending' | 'descending' type SortStateSpace = [Column, Order] const [[sortStateColumn, sortStateOrder], setSortState] = useState(['correct', 'descending']) const transitionColumn: (k: Column) => Partial SortStateSpace>>> = k => ({ [k]: { ascending: (c: Column) => (c === k ? [k, 'descending'] : [c, 'ascending']), descending: (c: Column) => (c === k ? [k, 'ascending'] : [c, 'ascending']), }, }) const transitionMap: Record SortStateSpace>> = { ...(transitionColumn('student') as Record<'student', Record SortStateSpace>>), ...(transitionColumn('sent') as Record<'sent', Record SortStateSpace>>), ...(transitionColumn('correct') as Record<'correct', Record SortStateSpace>>), } let orderedStats if (sortStateColumn === 'student') { orderedStats = sortByStringKey(Object.entries(stats), ([user, s]) => user.toLowerCase(), sortStateOrder === 'ascending') } else { orderedStats = sortByNumericKey( Object.entries(stats), // @ts-ignore ([user, s]) => s[sortStateColumn + 'SolutionsCount'], sortStateOrder === 'ascending' ) } return ( <>
Classifica
{orderedStats.length > 0 ? (
Studente setSortState(transitionMap[sortStateColumn][sortStateOrder]('student'))}> {sortStateColumn === 'student' ? sortStateOrder === 'ascending' ? 'expand_more' : 'expand_less' : 'unfold_more'}
Soluzioni Inviate setSortState(transitionMap[sortStateColumn][sortStateOrder]('sent'))}> {sortStateColumn === 'sent' ? sortStateOrder === 'ascending' ? 'expand_more' : 'expand_less' : 'unfold_more'}
Soluzioni Corrette setSortState(transitionMap[sortStateColumn][sortStateOrder]('correct'))}> {sortStateColumn === 'correct' ? sortStateOrder === 'ascending' ? 'expand_more' : 'expand_less' : 'unfold_more'}
{orderedStats.map(([user, s]) => ( <>
{s.sentSolutionsCount}
{s.correctSolutionsCount}
))}
) : ( <>

Nessuno ha ancora inviato soluzioni

)}
) }