/* * Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors. * SPDX-License-Identifier: GPL-2.0-or-later */ import * as React from "react"; import { useQuery } from "@tanstack/react-query"; import { useTable, useFilters, useGlobalFilter, useSortBy, usePagination, FilterProps, Column } from "react-table"; import { APIClient } from "@api/APIClient"; import { EmptyListState } from "@components/emptystates"; import * as Icons from "@components/Icons"; import * as DataTable from "@components/data-table"; // This is a custom filter UI for selecting // a unique option from a list function SelectColumnFilter({ column: { filterValue, setFilter, preFilteredRows, id, render } }: FilterProps) { // Calculate the options for filtering // using the preFilteredRows const options = React.useMemo(() => { const options = new Set(); preFilteredRows.forEach((row: { values: { [x: string]: string } }) => { options.add(row.values[id]); }); return [...options.values()]; }, [id, preFilteredRows]); // Render a multi-select box return ( ); } interface TableProps { columns: Column[]; data: Release[]; } function Table({ columns, data }: TableProps) { // Use the state and functions returned from useTable to build your UI const { getTableProps, getTableBodyProps, headerGroups, prepareRow, page // Instead of using 'rows', we'll use page, } = useTable( { columns, data }, useFilters, useGlobalFilter, useSortBy, usePagination ); if (!page.length) { return ; } // Render the UI for your table return (
{headerGroups.map((headerGroup) => { const { key: rowKey, ...rowRest } = headerGroup.getHeaderGroupProps(); return ( {headerGroup.headers.map((column) => { const { key: columnKey, ...columnRest } = column.getHeaderProps(column.getSortByToggleProps()); return ( // Add the sorting props to control sorting. For this example // we can add them into the header props ); })} ); })} {page.map((row) => { prepareRow(row); const { key: bodyRowKey, ...bodyRowRest } = row.getRowProps(); return ( {row.cells.map((cell) => { const { key: cellRowKey, ...cellRowRest } = cell.getCellProps(); return ( ); })} ); })}
<>{column.render("Header")} {/* Add a sort direction indicator */} {column.isSorted ? ( column.isSortedDesc ? ( ) : ( ) ) : ( )}
<>{cell.render("Cell")}
); } export const ActivityTable = () => { const columns = React.useMemo(() => [ { Header: "Age", accessor: "timestamp", Cell: DataTable.AgeCell }, { Header: "Release", accessor: "torrent_name", Cell: DataTable.TitleCell }, { Header: "Actions", accessor: "action_status", Cell: DataTable.ReleaseStatusCell }, { Header: "Indexer", accessor: "indexer", Cell: DataTable.TitleCell, Filter: SelectColumnFilter, filter: "includes" } ], []); const { isLoading, data } = useQuery({ queryKey: ["dash_recent_releases"], queryFn: APIClient.release.findRecent, refetchOnWindowFocus: false }); if (isLoading) { return (

 

); } return (

Recent activity

); };