'use client' import { useMemo, useState } from 'react'; import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowModel, getFilteredRowModel, ColumnDef, SortingState, ColumnFiltersState } from '@tanstack/react-table'; import { useTranslations } from 'next-intl'; import { IconArrowUp, IconArrowDown, IconFilter, IconDoubleArrowRight, IconArrowRight, IconDoubleArrowLeft, IconArrowLeft } from '@/app/ui/icons/icons'; import DropDownList from '@/app/components/DropDownList'; import { useQuery } from '@tanstack/react-query'; import { getUserFilesData } from '@/app/actions/fileEntity'; export default function ViolationsTable() { const { data: violationData, isLoading, isError, error, } = useQuery({ queryKey: ['violationData', 1, 10000], queryFn: () => getUserFilesData(1, 10000), select: (data) => { if (!data?.files) return []; return data.files.map((item: any) => { /* const newDate = new Date(item.updatedAt).getTime(); */ return { id: item.id, }; }); }, refetchInterval: 30000 }); const t = useTranslations('Global'); // Состояния const [sorting, setSorting] = useState([]); const [columnFilters, setColumnFilters] = useState([]); const [dateFilter, setDateFilter] = useState('all'); const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10, }); const columns = useMemo[]>( () => [ { accessorKey: 'file', header: ({ column }) => (
Файл
), cell: ({ row }) => { return (
{/* {row.original} */} Файл
) } }, { accessorKey: 'violationUrl', header: ({ column }) => (
violationUrl
), cell: ({ row }) => (
{/* {row.original.amount} */} violationUrl
), }, { accessorKey: 'statistic', header: ({ column }) => (
Статистика
), cell: ({ row }) => (
Статистика
), }, { accessorKey: 'DMCA', header: ({ column }) => (
DMCA жалоба
), cell: ({ row }) => { return (

DMCA жалоба

) }, }, { accessorKey: 'actions', header: ({ column }) => (
actions
), cell: ({ row }) => { return (
actions
) } }, ], [] ) const filteredData = useMemo(() => { let result = violationData?.payments; if (!result) { return []; } // Фильтр по дате if (dateFilter !== 'all') { const now = Date.now(); const oneDay = 24 * 60 * 60 * 1000; const sevenDays = 7 * oneDay; const thirtyDays = 30 * oneDay; switch (dateFilter) { case 'today': const todayStart = new Date().setHours(0, 0, 0, 0); result = result.filter((item: any) => { if (item.createdAt) { return new Date(item.createdAt).getTime() >= todayStart } else { return 0 } }); break; case 'week': const weekAgo = now - sevenDays; result = result.filter((item: any) => { if (item.createdAt) { return new Date(item.createdAt).getTime() >= weekAgo; } }); break; case 'month': const monthAgo = now - thirtyDays; result = result.filter((item: any) => { if (item.createdAt) { return new Date(item.createdAt).getTime() >= monthAgo; } }); break; case 'older': const monthAgo2 = now - thirtyDays; result = result.filter((item: any) => { if (item.createdAt) { return new Date(item.createdAt).getTime() < monthAgo2; } }); break; } } return result }, [violationData?.payments, dateFilter]) const table = useReactTable({ data: filteredData, columns, state: { sorting, columnFilters, pagination }, autoResetPageIndex: false, onPaginationChange: setPagination, onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getPaginationRowModel: getPaginationRowModel(), getFilteredRowModel: getFilteredRowModel(), initialState: { pagination: { pageSize: 10, }, }, }); return (

Обнаруженные нарушения

{/* Фильтры */} {/*
{t('date-filter')}:
{ switch (dateFilter) { case 'all': return t('all-dates') case 'week': return t('for-a-week') case 'month': return t('for-a-month') case 'older': return t('older-than-a-month') default: return t('today') } })()} callBack={setDateFilter} >
  • {t('all-dates')}
  • {t('today')}
  • {t('for-a-week')}
  • {t('for-a-month')}
  • {t('older-than-a-month')}
  • {t('items-per-page')}:
    {[5, 10, 20, 50, 100].map(pageSize => (
  • {t('show')} {pageSize}
  • ))}
    */} {/* Таблица */}
    {table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map(header => ( ))} ))} {table.getRowModel().rows.length > 0 ? ( table.getRowModel().rows.map(row => ( {row.getVisibleCells().map(cell => ( ))} )) ) : ( )}
    {header.isPlaceholder ? null : typeof header.column.columnDef.header === 'function' ? header.column.columnDef.header(header.getContext()) : header.column.columnDef.header as string}
    {typeof cell.column.columnDef.cell === 'function' ? cell.column.columnDef.cell(cell.getContext()) : cell.getValue() as string}
    {t('no-data-for-selected-filters')}
    {/* Пагинация */}
    {t('page')}{' '} {table.getState().pagination.pageIndex + 1} {t('out-of')} {table.getPageCount() ? table.getPageCount() : 1} | {t('shown')} {table.getRowModel().rows.length} {t('out-of')} {filteredData.length}
    {table.getCanPreviousPage() && ( )} {table.getCanPreviousPage() && ( )}
    {Array.from({ length: Math.min(5, table.getPageCount()) }, (_, i) => { const pageIndex = Math.max( 0, Math.min( table.getPageCount() - 5, table.getState().pagination.pageIndex - 2 ) ) + i; if (pageIndex < table.getPageCount()) { return ( ); } return null; })}
    {table.getCanNextPage() && ( )} {table.getCanNextPage() && ( )}
    ) }