servicepoint-tanks/tank-frontend/src/Scoreboard.tsx

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-04-17 23:09:01 +02:00
import {Player} from "./serverCalls.tsx";
2024-04-13 23:07:08 +02:00
import DataTable from "./components/DataTable.tsx";
2024-04-17 23:09:01 +02:00
import {useQuery} from "@tanstack/react-query";
2024-04-13 23:07:08 +02:00
function numberSorter(a: number, b: number) {
return b - a;
}
2024-04-13 23:07:08 +02:00
export default function Scoreboard({}: {}) {
2024-04-17 23:09:01 +02:00
const query = useQuery({
queryKey: ['scores'],
refetchInterval: 1000,
queryFn: async () => {
const url = new URL('/scores', import.meta.env.VITE_TANK_API);
const response = await fetch(url, {method: 'GET'});
if (!response.ok)
throw new Error(`response failed with code ${response.status} (${response.status})${await response.text()}`)
return await response.json() as Player[];
}
});
2024-04-13 23:07:08 +02:00
2024-04-17 23:09:01 +02:00
if (query.isError)
return <p>{query.error.message}</p>;
if (query.isPending)
return <p>loading...</p>;
2024-04-13 23:07:08 +02:00
2024-04-14 12:57:45 +02:00
return <DataTable
2024-04-17 23:09:01 +02:00
data={query.data}
2024-04-14 12:57:45 +02:00
columns={[
{field: 'name'},
{
field: 'kills',
visualize: p => p.scores.kills.toString(),
sorter: (a, b) => numberSorter(a.scores.kills, b.scores.kills)
},
{
field: 'deaths',
visualize: p => p.scores.deaths.toString(),
sorter: (a, b) => numberSorter(a.scores.deaths, b.scores.deaths)
},
{
field: 'ratio',
visualize: p => p.scores.ratio.toString(),
sorter: (a, b) => numberSorter(a.scores.ratio, b.scores.ratio)
}
2024-04-14 12:57:45 +02:00
]}/>
2024-04-13 23:07:08 +02:00
}