2022-01-25 20:47:25 +00:00
|
|
|
import clsx from 'clsx'
|
2022-02-06 06:13:13 +00:00
|
|
|
import { Avatar } from './avatar'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { Row } from './layout/row'
|
|
|
|
import { SiteLink } from './site-link'
|
2022-10-13 18:23:42 +00:00
|
|
|
import { Table } from './table'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { Title } from './title'
|
|
|
|
|
2022-09-22 19:40:27 +00:00
|
|
|
interface LeaderboardEntry {
|
|
|
|
username: string
|
|
|
|
name: string
|
|
|
|
avatarUrl?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Leaderboard<T extends LeaderboardEntry>(props: {
|
2022-01-21 23:21:46 +00:00
|
|
|
title: string
|
2022-09-22 19:40:27 +00:00
|
|
|
entries: T[]
|
2022-01-21 23:21:46 +00:00
|
|
|
columns: {
|
|
|
|
header: string
|
2022-09-22 19:40:27 +00:00
|
|
|
renderCell: (entry: T) => any
|
2022-01-21 23:21:46 +00:00
|
|
|
}[]
|
2022-01-25 20:47:25 +00:00
|
|
|
className?: string
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow?: number
|
2022-01-21 23:21:46 +00:00
|
|
|
}) {
|
2022-03-21 01:07:45 +00:00
|
|
|
// TODO: Ideally, highlight your own entry on the leaderboard
|
2022-07-13 21:11:22 +00:00
|
|
|
const { title, columns, className } = props
|
2022-09-22 19:40:27 +00:00
|
|
|
const maxToShow = props.maxToShow ?? props.entries.length
|
|
|
|
const entries = props.entries.slice(0, maxToShow)
|
2022-01-21 23:21:46 +00:00
|
|
|
return (
|
2022-01-25 20:47:25 +00:00
|
|
|
<div className={clsx('w-full px-1', className)}>
|
2022-02-03 05:34:35 +00:00
|
|
|
<Title text={title} className="!mt-0" />
|
2022-09-22 19:40:27 +00:00
|
|
|
{entries.length === 0 ? (
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="ml-2 text-gray-500">None yet</div>
|
2022-01-28 18:43:35 +00:00
|
|
|
) : (
|
|
|
|
<div className="overflow-x-auto">
|
2022-10-13 18:23:42 +00:00
|
|
|
<Table>
|
2022-01-28 18:43:35 +00:00
|
|
|
<thead>
|
2022-10-13 18:23:42 +00:00
|
|
|
<tr>
|
2022-01-28 18:43:35 +00:00
|
|
|
<th>#</th>
|
|
|
|
<th>Name</th>
|
2022-01-21 23:21:46 +00:00
|
|
|
{columns.map((column) => (
|
2022-01-28 18:43:35 +00:00
|
|
|
<th key={column.header}>{column.header}</th>
|
2022-01-21 23:21:46 +00:00
|
|
|
))}
|
|
|
|
</tr>
|
2022-01-28 18:43:35 +00:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
2022-09-22 19:40:27 +00:00
|
|
|
{entries.map((entry, index) => (
|
|
|
|
<tr key={index}>
|
2022-01-28 18:43:35 +00:00
|
|
|
<td>{index + 1}</td>
|
2022-08-26 21:23:06 +00:00
|
|
|
<td className="max-w-[190px]">
|
2022-09-22 19:40:27 +00:00
|
|
|
<SiteLink className="relative" href={`/${entry.username}`}>
|
2022-01-28 18:43:35 +00:00
|
|
|
<Row className="items-center gap-4">
|
2022-09-22 19:40:27 +00:00
|
|
|
<Avatar avatarUrl={entry.avatarUrl} size={8} />
|
|
|
|
<div className="truncate">{entry.name}</div>
|
2022-01-28 18:43:35 +00:00
|
|
|
</Row>
|
|
|
|
</SiteLink>
|
|
|
|
</td>
|
|
|
|
{columns.map((column) => (
|
2022-09-22 19:40:27 +00:00
|
|
|
<td key={column.header}>{column.renderCell(entry)}</td>
|
2022-01-28 18:43:35 +00:00
|
|
|
))}
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
2022-10-13 18:23:42 +00:00
|
|
|
</Table>
|
2022-01-28 18:43:35 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-01-21 23:21:46 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|