From 0c5e2fc45deb0f8491d34229b728428e4ef1f964 Mon Sep 17 00:00:00 2001 From: AbdBarho Date: Fri, 20 Jan 2023 13:00:59 +0100 Subject: [PATCH] Show last updated on leaderboard --- website/public/locales/en/common.json | 3 ++- .../LeaderboardGridCell.tsx | 27 ++++++++++++++----- website/src/pages/api/leaderboard.ts | 6 ++--- website/src/types/Leaderboard.ts | 1 + 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/website/public/locales/en/common.json b/website/public/locales/en/common.json index e18eb8ec..de764cc2 100644 --- a/website/public/locales/en/common.json +++ b/website/public/locales/en/common.json @@ -13,5 +13,6 @@ "sign_in": "Sign In", "sign_out": "Sign Out", "terms_of_service": "Terms of Service", - "title": "Open Assistant" + "title": "Open Assistant", + "last_updated_at": "Last updated at: {{val, datetime}}" } diff --git a/website/src/components/LeaderboardGridCell/LeaderboardGridCell.tsx b/website/src/components/LeaderboardGridCell/LeaderboardGridCell.tsx index df18735d..9750a851 100644 --- a/website/src/components/LeaderboardGridCell/LeaderboardGridCell.tsx +++ b/website/src/components/LeaderboardGridCell/LeaderboardGridCell.tsx @@ -1,8 +1,9 @@ -import { Table, TableContainer, Tbody, Td, Th, Thead, Tr, useColorModeValue } from "@chakra-ui/react"; -import React from "react"; +import { Table, TableContainer, Tbody, Td, Text, Th, Thead, Tr, useColorModeValue } from "@chakra-ui/react"; +import { useTranslation } from "next-i18next"; +import React, { useMemo } from "react"; import { useTable } from "react-table"; import { get } from "src/lib/api"; -import { LeaderboardEntity, LeaderboardTimeFrame } from "src/types/Leaderboard"; +import { LeaderboardReply, LeaderboardTimeFrame } from "src/types/Leaderboard"; import useSWRImmutable from "swr/immutable"; const columns = [ @@ -26,13 +27,26 @@ const columns = [ * Presents a grid of leaderboard entries with more detailed information. */ const LeaderboardGridCell = ({ timeFrame }: { timeFrame: LeaderboardTimeFrame }) => { - const { data } = useSWRImmutable(`/api/leaderboard?time_frame=${timeFrame}`, get, { - fallbackData: [], + const { t } = useTranslation(); + const { data: reply } = useSWRImmutable(`/api/leaderboard?time_frame=${timeFrame}`, get, { revalidateOnMount: true, }); + + const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ + columns, + data: reply?.leaderboard ?? [], + }); + const backgroundColor = useColorModeValue("white", "gray.800"); - const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data }); + const lastUpdated = useMemo(() => { + const val = new Date(reply?.last_updated); + return t("last_updated_at", { val, formatParams: { val: { dateStyle: "full", timeStyle: "short" } } }); + }, [t, reply?.last_updated]); + + if (!reply) { + return null; + } return ( @@ -66,6 +80,7 @@ const LeaderboardGridCell = ({ timeFrame }: { timeFrame: LeaderboardTimeFrame }) })} + {lastUpdated} ); }; diff --git a/website/src/pages/api/leaderboard.ts b/website/src/pages/api/leaderboard.ts index 592f3da5..1ddf947e 100644 --- a/website/src/pages/api/leaderboard.ts +++ b/website/src/pages/api/leaderboard.ts @@ -6,9 +6,9 @@ import { LeaderboardTimeFrame } from "src/types/Leaderboard"; * Returns the set of valid labels that can be applied to messages. */ const handler = withoutRole("banned", async (req, res) => { - const time_frame = (req.query.time_frame as LeaderboardTimeFrame) || LeaderboardTimeFrame.day; - const { leaderboard } = await oasstApiClient.fetch_leaderboard(time_frame); - res.status(200).json(leaderboard); + const time_frame = (req.query.time_frame as LeaderboardTimeFrame) ?? LeaderboardTimeFrame.day; + const info = await oasstApiClient.fetch_leaderboard(time_frame); + res.status(200).json(info); }); export default handler; diff --git a/website/src/types/Leaderboard.ts b/website/src/types/Leaderboard.ts index 21c91766..5c0acfc3 100644 --- a/website/src/types/Leaderboard.ts +++ b/website/src/types/Leaderboard.ts @@ -12,6 +12,7 @@ export const enum LeaderboardTimeFrame { } export interface LeaderboardReply { time_frame: LeaderboardTimeFrame; + last_updated: string; // date time iso string leaderboard: LeaderboardEntity[]; }