'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 } 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'; import { useLocale } from 'next-intl'; export function TrackingHistoryView() { const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10, }); const [sorting, setSorting] = useState([]); const t = useTranslations('Global'); const locale = useLocale(); const getSortParams = useMemo(() => { if (sorting.length === 0) { return { sortBy: '', sortOrder: undefined }; } const sort = sorting[0]; const sortByMap: Record = { 'document': 'docName', 'view_date': 'createdAt', 'viewer': 'viewerName', 'location': locale === 'ru' ? 'geoNameRu' : 'geoNameEn' }; return { sortBy: sortByMap[sort.id] || sort.id, sortOrder: sort.desc ? 'desc' : 'asc' as 'desc' | 'asc' }; }, [sorting]); const { data: apiResponse, isLoading, isFetching, isError, error, refetch } = useHistoryView(pagination.pageIndex, pagination.pageSize, locale, getSortParams.sortOrder, getSortParams.sortBy); const totalElements = apiResponse?.totalElements || 0 const tableData = apiResponse?.history || []; const totalPages = apiResponse?.totalPages || 0; 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) + '...'; } function EditLocationName({ location }: { location: string | undefined | null }) { if (!location) return (
); const [city, country] = location.split('/'); const translatedCity = city === 'unknow' ? t('not-defined-city') : city; const translatedCountry = country === 'unknow' ? t('not-defined-country') : country; return (
{t('city')}: {translatedCity}
{t('country')}: {translatedCountry}
); } const columns = useMemo[]>( () => [ { accessorKey: 'document', header: ({ column }) => (
{t('document')}
), cell: ({ row }) => (
{row.original.document}
), }, { accessorKey: 'view_date', header: ({ column }) => (
{t('date')} / {t('time')}
), cell: ({ row }) => { return (
{row.original.view_date ? ( <> {formatDate(row.original.view_date)}
{formatDateTime(row.original.view_date)}
) : (
-
)}
) }, enableColumnFilter: false, }, { accessorKey: 'viewer', header: ({ column }) => (
{t('who-opened')}
), cell: ({ row }) => { return (
{row.original.viewer}
) }, }, { accessorKey: 'location', header: ({ column }) => (
{t('location')}
), cell: ({ row }) => { return (
{row.original.country === 'unknow/unknow' ? ( t('not-defined') ) : ( )}
) }, } ], [] ); // Создание таблицы 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(), }); useEffect(() => { if (tableData.length === 0 && pagination.pageIndex > 0 && totalPages > 0) { setPagination(prev => ({ ...prev, pageIndex: Math.max(0, totalPages - 1) })); } }, [tableData, pagination.pageIndex, totalPages]); return (
{/* Фильтры */}

{t('history-of-opened')}

{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('total-document-openings')}: {totalElements}
    {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; })}
    ) }