'use client' import { Column, Table as ReactTable, useReactTable, ColumnFiltersState, getCoreRowModel, getFilteredRowModel, getFacetedRowModel, getFacetedUniqueValues, getFacetedMinMaxValues, getPaginationRowModel, sortingFns, getSortedRowModel, FilterFn, SortingFn, ColumnDef, flexRender, FilterFns, ColumnOrderState, createColumnHelper, } from '@tanstack/react-table' import Image from 'next/image'; import { useState } from "react"; import { IStream } from "@/lib/streams"; import { LocalizedDate } from './localized-date'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faAngleLeft, faAngleRight, faAnglesLeft, faAnglesRight, faChevronCircleRight, faChevronRight } from '@fortawesome/free-solid-svg-icons'; import Link from 'next/link'; function Filter({ column, table, }: { column: Column table: ReactTable }) { const firstValue = table .getPreFilteredRowModel() .flatRows[0]?.getValue(column.id) const columnFilterValue = column.getFilterValue() if (typeof firstValue === 'number') { return (
column.setFilterValue((old: [number, number]) => [ e.target.value, old?.[1], ]) } placeholder={`Min`} className="w-24 border shadow rounded" /> column.setFilterValue((old: [number, number]) => [ old?.[0], e.target.value, ]) } placeholder={`Max`} className="w-24 border shadow rounded" />
) } if (typeof firstValue === 'boolean') { return ( <>
) } return ( column.setFilterValue(e.target.value)} placeholder={`Search...`} className="input" /> ) } const archiveStatusClassName = (archiveStatus: string): string => { if (archiveStatus === 'missing') return 'is-danger'; if (archiveStatus === 'issue') return 'is-warning'; if (archiveStatus === 'good') return 'is-success'; return 'is-info'; }; export default function StreamsTable({ streams }: { streams: IStream[] }) { const columnHelper = createColumnHelper() const columns = [ columnHelper.accessor('attributes.cuid', { cell: info => {info.getValue()}, header: () => ID }), columnHelper.accessor('attributes.vtuber.data.attributes.image', { cell: info =>
, header: () => , enableColumnFilter: false }), columnHelper.accessor('attributes.vtuber.data.attributes.displayName', { cell: info => info.getValue(), header: () => VTuber }), columnHelper.accessor('attributes.date', { cell: info => , header: () => Date }), columnHelper.accessor('attributes.isChaturbateStream', { id: 'isChaturbateStream', cell: info => info.getValue() === true ? 'yes' : 'no', header: () => Chaturbate }), columnHelper.accessor('attributes.isFanslyStream', { id: 'isFanslyStream', cell: info => info.getValue() === true ? 'yes' : 'no', header: () => Fansly }), columnHelper.accessor('attributes.archiveStatus', { cell: info =>
{info.getValue()}
, header: () => Status }) ] const [columnVisibility, setColumnVisibility] = useState({}) const [columnOrder, setColumnOrder] = useState([]) const [columnFilters, setColumnFilters] = useState([]) const [data, setData] = useState(() => streams) const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getFilteredRowModel: getFilteredRowModel(), getPaginationRowModel: getPaginationRowModel(), state: { columnVisibility, columnOrder, columnFilters }, onColumnOrderChange: setColumnOrder, onColumnFiltersChange: setColumnFilters, }) return ( <>
{table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map(header => { return ( ) })} ))} {table.getRowModel().rows.map(row => { return ( {row.getVisibleCells().map(cell => { return ( ) })} ) })}
{header.isPlaceholder ? null : (
{flexRender( header.column.columnDef.header, header.getContext() )} {header.column.getCanFilter() ? (
) : null}
)}
{flexRender( cell.column.columnDef.cell, cell.getContext() )}
Page {table.getState().pagination.pageIndex + 1} of{' '} {table.getPageCount()}
{ const page = e.target.value ? Number(e.target.value) - 1 : 0 table.setPageIndex(page) }} className="input p-1" />
Page
) }