'use client' import { useHistoryView, HistoryViewItem } from '@/app/hooks/react-query/useHistoryView'; import { useEffect, useState, useMemo } from 'react'; import { IconDoubleArrowRight, IconArrowRight, IconDoubleArrowLeft, IconArrowLeft, IconArrowUp, IconArrowDown, IconFilter, IconFileDownload, IconInfo, IconEye, IconCheck, IconLink } from '@/app/ui/icons/icons'; import DropDownList from '@/app/components/DropDownList'; import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowModel, SortingState, ColumnDef } from '@tanstack/react-table'; import { formatDate, formatDateTime } from '@/app/lib/formatDate'; import { useTranslations } from 'next-intl'; import { useViewport } from '@/app/hooks/useViewport'; export function TrackingHistoryView() { const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10, }); const [sorting, setSorting] = useState([]); const t = useTranslations('Global'); const { data: apiResponse, isLoading, isFetching, isError, error, refetch } = useHistoryView(pagination.pageIndex, pagination.pageSize); useEffect(() => { console.log(apiResponse); }, [apiResponse]); const tableData = apiResponse?.history || []; const totalItems = apiResponse?.history.length || 0; const totalPages = Math.ceil(totalItems / pagination.pageSize); const viewport = useViewport(); const getMaxLengthByWidth = (): number => { if (viewport.device === 'MOBILE') return 22; if (viewport.device === 'TABLET') return 30; if (viewport.device === 'DESKTOP') return 50; return 100; }; const cutFileName = (fileName: string) => { const MAX_FILE_NAME_LENGTH = getMaxLengthByWidth(); const lastDotIndex = fileName.lastIndexOf('.'); if (lastDotIndex <= 0) { return fileName.length > MAX_FILE_NAME_LENGTH ? fileName.substring(0, MAX_FILE_NAME_LENGTH - 3) + '...' : fileName; } const nameWithoutExtension = fileName.substring(0, lastDotIndex); const extension = fileName.substring(lastDotIndex); if (fileName.length <= MAX_FILE_NAME_LENGTH) { return fileName; } const maxNameLength = MAX_FILE_NAME_LENGTH - extension.length - 3; if (maxNameLength <= 0) { return '...'; } return nameWithoutExtension.substring(0, maxNameLength) + '...'; } const columns = useMemo[]>( () => [ { accessorKey: 'document', header: ({ column }) => (
{t('document')}
), cell: ({ row }) => (
{row.original.document}
), }, { accessorKey: 'view_date', header: ({ column }) => (
Дата / время
), cell: ({ row }) => { return (
{row.original.view_date ? ( <> {formatDate(row.original.view_date)}
{formatDateTime(row.original.view_date)}
) : (
-
)}
) }, enableColumnFilter: false, }, { accessorKey: 'viewer', header: ({ column }) => (
viewer
), cell: ({ row }) => { return (
{row.original.viewer}
) }, }, { accessorKey: 'location', header: ({ column }) => (
Местоположение
), cell: ({ row }) => { return (
{row.original.city} {row.original.country}
) }, } ], [] ); // Создание таблицы const table = useReactTable({ data: tableData, columns, state: { sorting, pagination }, pageCount: totalPages, manualPagination: true, manualSorting: true, manualFiltering: true, onPaginationChange: setPagination, onSortingChange: (updater) => { const newSorting = typeof updater === 'function' ? updater(sorting) : updater; setSorting(newSorting); setPagination(prev => ({ ...prev, pageIndex: 0 })); }, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getPaginationRowModel: getPaginationRowModel(), }); return (

История открытий

{/* Фильтры */}
{t('items-per-page')}:
{ table.setPageSize(Number(newSize)); setPagination(prev => ({ ...prev, pageIndex: 0 })); }} > {['5', '10', '20', '50', '100'].map(pageSize => (
  • {t('show')} {pageSize}
  • ))}
    {/* Таблица */}
    {table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map(header => ( ))} ))} {isLoading && ( )} {table.getRowModel().rows.length > 0 ? ( table.getRowModel().rows.map(row => ( {row.getVisibleCells().map(cell => ( ))} )) ) : ( {!isLoading && ( )} )}
    {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')}{' '} {pagination.pageIndex + 1} {t('out-of')} {totalPages || 1} | {t('shown')} {tableData.length} {t('out-of')} {totalItems}
    {Array.from({ length: Math.min(5, totalPages) }, (_, i) => { let pageIndex; if (totalPages <= 5) { pageIndex = i; } else if (pagination.pageIndex <= 2) { pageIndex = i; } else if (pagination.pageIndex >= totalPages - 3) { pageIndex = totalPages - 5 + i; } else { pageIndex = pagination.pageIndex - 2 + i; } if (pageIndex >= 0 && pageIndex < totalPages) { return ( ); } return null; })}
    ) }