import { ChevronDoubleLeftIcon, ChevronLeftIcon, ChevronRightIcon, ChevronDoubleRightIcon } from "@heroicons/react/solid" import { formatDistanceToNowStrict } from "date-fns" import React from "react" import { useQuery } from "react-query" import { useTable, useSortBy, usePagination } from "react-table" import APIClient from "../api/APIClient" import { EmptyListState } from "../components/emptystates" import { classNames } from "../utils" export function Releases() { return (

Releases

) } // 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 status = value ? value.toLowerCase() : "unknown"; // return ( // // {status} // // ); // }; 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, column, row }: any) { const formatDate = formatDistanceToNowStrict( new Date(value), { addSuffix: true } ) return (
{formatDate}
) } export function ReleaseCell({ value, column, row }: any) { return (
{value}
) } const initialState = { queryPageIndex: 0, queryPageSize: 10, totalCount: null, }; const PAGE_CHANGED = 'PAGE_CHANGED'; const PAGE_SIZE_CHANGED = 'PAGE_SIZE_CHANGED'; const TOTAL_COUNT_CHANGED = 'TOTAL_COUNT_CHANGED'; const reducer = (state: any, { type, payload }: any) => { switch (type) { case PAGE_CHANGED: return { ...state, queryPageIndex: payload, }; case PAGE_SIZE_CHANGED: return { ...state, queryPageSize: payload, }; case TOTAL_COUNT_CHANGED: return { ...state, totalCount: payload, }; default: throw new Error(`Unhandled action type: ${type}`); } }; function Table() { const columns = React.useMemo(() => [ { Header: "Age", accessor: 'timestamp', Cell: AgeCell, }, { Header: "Release", accessor: 'torrent_name', Cell: ReleaseCell, }, // { // Header: "Filter Status", // accessor: 'filter_status', // Cell: StatusPill, // }, { Header: "Push Status", accessor: 'push_status', Cell: StatusPill, }, { Header: "Indexer", accessor: 'indexer', Filter: SelectColumnFilter, // new filter: 'includes', }, ], []) const [{ queryPageIndex, queryPageSize, totalCount }, dispatch] = React.useReducer(reducer, initialState); const { isLoading, error, data, isSuccess } = useQuery( ['releases', queryPageIndex, queryPageSize], () => APIClient.release.find(`?offset=${queryPageIndex * queryPageSize}&limit=${queryPageSize}`), { keepPreviousData: true, staleTime: Infinity, } ); // 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: { pageIndex, pageSize }, // preGlobalFilteredRows, // setGlobalFilter, } = useTable({ columns, data: isSuccess ? data.data : [], initialState: { pageIndex: queryPageIndex, pageSize: queryPageSize, }, manualPagination: true, manualSortBy: true, pageCount: isSuccess ? Math.ceil(totalCount / queryPageSize) : 0, }, // useFilters, // useFilters! // useGlobalFilter, useSortBy, usePagination, // new ) React.useEffect(() => { dispatch({ type: PAGE_CHANGED, payload: pageIndex }); }, [pageIndex]); React.useEffect(() => { dispatch({ type: PAGE_SIZE_CHANGED, payload: pageSize }); gotoPage(0); }, [pageSize, gotoPage]); React.useEffect(() => { if (data?.count) { dispatch({ type: TOTAL_COUNT_CHANGED, payload: data.count, }); } }, [data?.count]); if (error) { return

Error

; } if (isLoading) { return

Loading...

; } // Render the UI for your table return ( <>
{/* */} {/* {headerGroups.map((headerGroup: { headers: any[] }) => headerGroup.headers.map((column) => column.Filter ? (
{column.render("Filter")}
) : null ) )} */}
{isSuccess ?
{headerGroups.map((headerGroup: { getHeaderGroupProps: () => JSX.IntrinsicAttributes & React.ClassAttributes & React.HTMLAttributes; headers: any[] }) => ( {headerGroup.headers.map(column => ( // Add the sorting props to control sorting. For this example // we can add them into the header props ))} ))} {page.map((row: any, i: any) => { // new prepareRow(row) return ( {row.cells.map((cell: any) => { return ( ) })} ) })}
{column.render('Header')} {/* Add a sort direction indicator */} {column.isSorted ? column.isSortedDesc ? : : ( )}
{cell.column.Cell.name === "defaultRenderer" ?
{cell.render('Cell')}
: cell.render('Cell') }
{/* Pagination */}
Page {pageIndex + 1} of {pageOptions.length}
: } ) } function SortIcon({ className }: any) { return ( ) } function SortUpIcon({ className }: any) { return ( ) } function SortDownIcon({ className }: any) { return ( ) } function Button({ children, className, ...rest }: any) { return ( ) } function PageButton({ children, className, ...rest }: any) { return ( ) }