From f6ce7ee45efb37d169db519d0b8e752cfd95beae Mon Sep 17 00:00:00 2001 From: Francisco Salgueiro Date: Mon, 13 Nov 2023 22:40:02 +0000 Subject: [PATCH] fix sort options in tournament games table (fixes #90) --- src/components/databases/TournamentCard.tsx | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/databases/TournamentCard.tsx b/src/components/databases/TournamentCard.tsx index 5e438536..bfafe6d9 100644 --- a/src/components/databases/TournamentCard.tsx +++ b/src/components/databases/TournamentCard.tsx @@ -1,6 +1,6 @@ import { ActionIcon, Paper, Stack, Text, useMantineTheme } from "@mantine/core"; import { IconEye } from "@tabler/icons-react"; -import { DataTable } from "mantine-datatable"; +import { DataTable, DataTableSortStatus } from "mantine-datatable"; import { useEffect, useState } from "react"; import { NormalizedGame, Tournament, getTournamentGames } from "@/utils/db"; import { createTab } from "@/utils/tabs"; @@ -35,6 +35,22 @@ function PlayerCard({ }; }, [tournament.id, file]); + const [sort, setSort] = useState({ + columnAccessor: "date", + direction: "asc", + }); + + const sortedGames = games.sort((a, b) => { + const key = sort.columnAccessor; + if (sort.direction === "asc") { + /// @ts-expect-error we know they're the same type + return a[key] > b[key] ? 1 : -1; + } else { + /// @ts-expect-error we know they're the same type + return a[key] < b[key] ? 1 : -1; + } + }); + return ( @@ -44,8 +60,10 @@ function PlayerCard({