import * as React from "react"; import { useQuery } from "react-query"; import formatDistanceToNowStrict from "date-fns/formatDistanceToNowStrict"; import { useTable, useFilters, useGlobalFilter, useSortBy, usePagination } from "react-table"; import { APIClient } from "../api/APIClient"; import { EmptyListState } from "../components/emptystates"; import { ReleaseStatusCell } from "./Releases"; export function Dashboard() { return (
) } const StatsItem = ({ name, stat }: any) => (

{name}

{stat}

) function Stats() { const { isLoading, data } = useQuery( 'dash_release_stats', () => APIClient.release.stats(), { refetchOnWindowFocus: false } ); if (isLoading) return null; return (

Stats

{/* */}
) } // This is a custom filter UI for selecting // a unique option from a list export function SelectColumnFilter({ column: { filterValue, setFilter, preFilteredRows, id, render }, }: any) { // Calculate the options for filtering // using the preFilteredRows const options = React.useMemo(() => { const options: any = new Set() preFilteredRows.forEach((row: { values: { [x: string]: unknown } }) => { options.add(row.values[id]) }) return [...options.values()] }, [id, preFilteredRows]) // Render a multi-select box return ( ) } export function StatusPill({ value }: any) { const statusMap: any = { "FILTER_APPROVED": Approved, "FILTER_REJECTED": Rejected, "PUSH_REJECTED": Rejected, "PUSH_APPROVED": Approved, "PENDING": PENDING, "MIXED": MIXED, } return statusMap[value]; } export function AgeCell({ value }: any) { const formatDate = formatDistanceToNowStrict( new Date(value), { addSuffix: true } ) return (
{formatDate}
) } export function ReleaseCell({ value }: any) { return (
{value}
) } export function IndexerCell({ value }: any) { return (
{value}
) } function Table({ columns, data }: any) { // 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, // which has only the rows for the active page // The rest of these things are super handy, too ;) // canPreviousPage, // canNextPage, // pageOptions, // pageCount, // gotoPage, // nextPage, // previousPage, // setPageSize, // state, // preGlobalFilteredRows, // setGlobalFilter, } = useTable({ columns, data, }, useFilters, // useFilters! useGlobalFilter, useSortBy, usePagination, // new ); 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: any) => { prepareRow(row); const { key: bodyRowKey, ...bodyRowRest } = row.getRowProps(); return ( {row.cells.map((cell: any) => { const { key: cellRowKey, ...cellRowRest } = cell.getCellProps(); return ( ) })} ) })}
{column.render('Header')} {/* Add a sort direction indicator */} {column.isSorted ? ( column.isSortedDesc ? ( ) : ( ) ) : ( )}
{cell.column.Cell.name === "defaultRenderer" ?
{cell.render('Cell')}
: cell.render('Cell') }
); } function SortIcon({ className }: any) { return ( ) } function SortUpIcon({ className }: any) { return ( ) } function SortDownIcon({ className }: any) { return ( ) } function DataTable() { const columns = React.useMemo(() => [ { Header: "Age", accessor: 'timestamp', Cell: AgeCell, }, { Header: "Release", accessor: 'torrent_name', Cell: ReleaseCell, }, { Header: "Actions", accessor: 'action_status', Cell: ReleaseStatusCell, }, { Header: "Indexer", accessor: 'indexer', Cell: IndexerCell, Filter: SelectColumnFilter, // new filter: 'includes', }, ], []) const { isLoading, data } = useQuery( 'dash_release', () => APIClient.release.find("?limit=10"), { refetchOnWindowFocus: false } ); if (isLoading) return null; return (

Recent activity

); }