Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement trollboard UI * remove unneeded code * add link to user * user link in leaderboard
- Loading branch information
Showing
13 changed files
with
389 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
website/src/components/LeaderboardTable/TrollboardTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { Box, CircularProgress, Flex, Link } from "@chakra-ui/react"; | ||
import { createColumnHelper } from "@tanstack/react-table"; | ||
import { ThumbsDown, ThumbsUp } from "lucide-react"; | ||
import NextLink from "next/link"; | ||
import { FetchTrollBoardResponse, TrollboardEntity, TrollboardTimeFrame } from "src/types/Trollboard"; | ||
|
||
import { DataTable } from "../DataTable"; | ||
import { useBoardPagination } from "./useBoardPagination"; | ||
import { useBoardRowProps } from "./useBoardRowProps"; | ||
import { useFetchBoard } from "./useFetchBoard"; | ||
const columnHelper = createColumnHelper<TrollboardEntity>(); | ||
|
||
const toPercentage = (num: number) => `${Math.round(num * 100)}%`; | ||
|
||
const columns = [ | ||
columnHelper.accessor("rank", {}), | ||
columnHelper.accessor("display_name", { | ||
header: "Display name", | ||
cell: ({ getValue, row }) => ( | ||
<Link as={NextLink} href={`/admin/manage_user/${row.original.user_id}`}> | ||
{getValue()} | ||
</Link> | ||
), | ||
}), | ||
columnHelper.accessor("troll_score", { | ||
header: "Troll score", | ||
}), | ||
columnHelper.accessor("red_flags", { | ||
header: "Red flags", | ||
}), | ||
columnHelper.accessor((row) => [row.upvotes, row.downvotes] as const, { | ||
id: "vote", | ||
cell: ({ getValue }) => { | ||
const [up, down] = getValue(); | ||
return ( | ||
<Flex gap={2} justifyItems="center" alignItems="center"> | ||
<ThumbsUp></ThumbsUp> | ||
{up} | ||
<ThumbsDown></ThumbsDown> | ||
{down} | ||
</Flex> | ||
); | ||
}, | ||
}), | ||
columnHelper.accessor((row) => row.spam + row.spam_prompts, { | ||
header: "Spam", | ||
}), | ||
columnHelper.accessor("lang_mismach", { | ||
header: "Lang mismach", | ||
}), | ||
columnHelper.accessor("not_appropriate", { | ||
header: "Not appropriate", | ||
}), | ||
columnHelper.accessor("pii", {}), | ||
columnHelper.accessor("hate_speech", { | ||
header: "Hate speech", | ||
}), | ||
columnHelper.accessor("sexual_content", { | ||
header: "Sexual Content", | ||
}), | ||
columnHelper.accessor("political_content", { | ||
header: "Political Content", | ||
}), | ||
columnHelper.accessor("quality", { | ||
cell: ({ getValue }) => toPercentage(getValue()), | ||
}), | ||
columnHelper.accessor("helpfulness", { | ||
cell: ({ getValue }) => toPercentage(getValue()), | ||
}), | ||
columnHelper.accessor("humor", { | ||
cell: ({ getValue }) => toPercentage(getValue()), | ||
}), | ||
columnHelper.accessor("violence", { | ||
cell: ({ getValue }) => toPercentage(getValue()), | ||
}), | ||
columnHelper.accessor("toxicity", { | ||
cell: ({ getValue }) => toPercentage(getValue()), | ||
}), | ||
]; | ||
|
||
export const TrollboardTable = ({ | ||
limit, | ||
rowPerPage, | ||
timeFrame, | ||
}: { | ||
timeFrame: TrollboardTimeFrame; | ||
limit: number; | ||
rowPerPage: number; | ||
}) => { | ||
const { | ||
data: trollboardRes, | ||
isLoading, | ||
error, | ||
lastUpdated, | ||
} = useFetchBoard<FetchTrollBoardResponse>(`/api/admin/trollboard?time_frame=${timeFrame}&limit=${limit}`); | ||
|
||
const { data, ...paginationProps } = useBoardPagination({ rowPerPage, data: trollboardRes?.trollboard, limit }); | ||
const rowProps = useBoardRowProps<TrollboardEntity>(); | ||
if (isLoading) { | ||
return <CircularProgress isIndeterminate></CircularProgress>; | ||
} | ||
|
||
if (error) { | ||
return <span>Unable to load leaderboard</span>; | ||
} | ||
|
||
return ( | ||
<Box | ||
sx={{ | ||
"th,td": { | ||
px: 2, | ||
}, | ||
}} | ||
> | ||
<DataTable<TrollboardEntity> | ||
data={data} | ||
columns={columns} | ||
caption={lastUpdated} | ||
rowProps={rowProps} | ||
{...paginationProps} | ||
></DataTable> | ||
</Box> | ||
); | ||
}; |
34 changes: 34 additions & 0 deletions
34
website/src/components/LeaderboardTable/useBoardPagination.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useState } from "react"; | ||
|
||
export const useBoardPagination = <T>({ | ||
rowPerPage, | ||
limit, | ||
...res | ||
}: { | ||
rowPerPage: number; | ||
data?: T[]; | ||
limit: number; | ||
}) => { | ||
const data = res.data || []; | ||
const [page, setPage] = useState(1); | ||
const maxPage = data ? Math.ceil(data.length / rowPerPage) : 0; | ||
const disablePagination = limit <= rowPerPage; | ||
const disableNext = page >= maxPage; | ||
const disablePrevious = page === 1; | ||
const onNextClick = () => setPage((p) => p + 1); | ||
const onPreviousClick = () => setPage((p) => p - 1); | ||
const start = (page - 1) * rowPerPage; | ||
const end = start + rowPerPage; | ||
const entities = data.slice(start, end); | ||
|
||
return { | ||
page, | ||
data: entities, | ||
end, | ||
disablePrevious, | ||
disableNext, | ||
disablePagination, | ||
onNextClick, | ||
onPreviousClick, | ||
}; | ||
}; |
Oops, something went wrong.