418 lines
11 KiB
TypeScript
418 lines
11 KiB
TypeScript
'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<SortingState>([]);
|
||
|
|
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<ColumnDef<HistoryViewItem>[]>(
|
||
|
|
() => [
|
||
|
|
{
|
||
|
|
accessorKey: 'document',
|
||
|
|
header: ({ column }) => (
|
||
|
|
<div className="column">
|
||
|
|
<span>
|
||
|
|
{t('document')}
|
||
|
|
</span>
|
||
|
|
<button
|
||
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
||
|
|
className="sort-button"
|
||
|
|
>
|
||
|
|
{
|
||
|
|
column.getIsSorted() === 'asc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowUp />
|
||
|
|
</span>
|
||
|
|
: column.getIsSorted() === 'desc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowDown />
|
||
|
|
</span>
|
||
|
|
: <span>
|
||
|
|
<IconFilter />
|
||
|
|
</span>
|
||
|
|
}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
),
|
||
|
|
cell: ({ row }) => (
|
||
|
|
<div className="text-center table-item">
|
||
|
|
{row.original.document}
|
||
|
|
</div>
|
||
|
|
),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
accessorKey: 'view_date',
|
||
|
|
header: ({ column }) => (
|
||
|
|
<div className="column">
|
||
|
|
<span>
|
||
|
|
Дата / время
|
||
|
|
</span>
|
||
|
|
<button
|
||
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
||
|
|
className="sort-button"
|
||
|
|
>
|
||
|
|
{
|
||
|
|
column.getIsSorted() === 'asc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowUp />
|
||
|
|
</span>
|
||
|
|
: column.getIsSorted() === 'desc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowDown />
|
||
|
|
</span>
|
||
|
|
: <span>
|
||
|
|
<IconFilter />
|
||
|
|
</span>
|
||
|
|
}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
),
|
||
|
|
cell: ({ row }) => {
|
||
|
|
return (
|
||
|
|
<div className="text-center table-item">
|
||
|
|
{row.original.view_date ? (
|
||
|
|
<>
|
||
|
|
{formatDate(row.original.view_date)}
|
||
|
|
<br />
|
||
|
|
{formatDateTime(row.original.view_date)}
|
||
|
|
<br />
|
||
|
|
</>
|
||
|
|
) : (
|
||
|
|
<div>-</div>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
)
|
||
|
|
},
|
||
|
|
enableColumnFilter: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
accessorKey: 'viewer',
|
||
|
|
header: ({ column }) => (
|
||
|
|
<div className="column">
|
||
|
|
<span>
|
||
|
|
viewer
|
||
|
|
</span>
|
||
|
|
<button
|
||
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
||
|
|
className="sort-button"
|
||
|
|
>
|
||
|
|
{
|
||
|
|
column.getIsSorted() === 'asc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowUp />
|
||
|
|
</span>
|
||
|
|
: column.getIsSorted() === 'desc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowDown />
|
||
|
|
</span>
|
||
|
|
: <span>
|
||
|
|
<IconFilter />
|
||
|
|
</span>
|
||
|
|
}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
),
|
||
|
|
cell: ({ row }) => {
|
||
|
|
return (
|
||
|
|
<div className="text-center font-semibold table-item">
|
||
|
|
{row.original.viewer}
|
||
|
|
</div>
|
||
|
|
)
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
accessorKey: 'location',
|
||
|
|
header: ({ column }) => (
|
||
|
|
<div className="column">
|
||
|
|
<span>
|
||
|
|
Местоположение
|
||
|
|
</span>
|
||
|
|
<button
|
||
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
||
|
|
className="sort-button"
|
||
|
|
>
|
||
|
|
{
|
||
|
|
column.getIsSorted() === 'asc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowUp />
|
||
|
|
</span>
|
||
|
|
: column.getIsSorted() === 'desc' ?
|
||
|
|
<span>
|
||
|
|
<IconArrowDown />
|
||
|
|
</span>
|
||
|
|
: <span>
|
||
|
|
<IconFilter />
|
||
|
|
</span>
|
||
|
|
}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
),
|
||
|
|
cell: ({ row }) => {
|
||
|
|
return (
|
||
|
|
<div className="text-center font-semibold table-item">
|
||
|
|
{row.original.city} {row.original.country}
|
||
|
|
</div>
|
||
|
|
)
|
||
|
|
},
|
||
|
|
}
|
||
|
|
],
|
||
|
|
[]
|
||
|
|
);
|
||
|
|
|
||
|
|
// Создание таблицы
|
||
|
|
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 (
|
||
|
|
<div
|
||
|
|
className="block-wrapper"
|
||
|
|
>
|
||
|
|
<div className="tanstak-table-wrapper">
|
||
|
|
<h3
|
||
|
|
className="tanstak-table-title"
|
||
|
|
>
|
||
|
|
История открытий
|
||
|
|
</h3>
|
||
|
|
{/* Фильтры */}
|
||
|
|
<div className="tanstak-table-filtres">
|
||
|
|
<div className="table-filtres-wrapper text-filter-search">
|
||
|
|
<div className="table-filtres-item">
|
||
|
|
<div className="table-filtres-label">{t('items-per-page')}:</div>
|
||
|
|
<DropDownList
|
||
|
|
value={table.getState().pagination.pageSize}
|
||
|
|
callBack={(newSize: string) => {
|
||
|
|
table.setPageSize(Number(newSize));
|
||
|
|
setPagination(prev => ({ ...prev, pageIndex: 0 }));
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
{['5', '10', '20', '50', '100'].map(pageSize => (
|
||
|
|
<li key={pageSize} value={pageSize}>
|
||
|
|
{t('show')} {pageSize}
|
||
|
|
</li>
|
||
|
|
))}
|
||
|
|
</DropDownList>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{/* Таблица */}
|
||
|
|
<div className="tanstak-table-block">
|
||
|
|
<table className="tanstak-table">
|
||
|
|
<thead className="tanstak-table-head">
|
||
|
|
{table.getHeaderGroups().map(headerGroup => (
|
||
|
|
<tr key={headerGroup.id}>
|
||
|
|
{headerGroup.headers.map(header => (
|
||
|
|
<th
|
||
|
|
key={header.id}
|
||
|
|
>
|
||
|
|
{header.isPlaceholder
|
||
|
|
? null
|
||
|
|
: typeof header.column.columnDef.header === 'function'
|
||
|
|
? header.column.columnDef.header(header.getContext())
|
||
|
|
: header.column.columnDef.header as string}
|
||
|
|
</th>
|
||
|
|
))}
|
||
|
|
</tr>
|
||
|
|
))}
|
||
|
|
</thead>
|
||
|
|
<tbody className={`tanstak-table-body ${isFetching ? 'loading' : ''}`}>
|
||
|
|
{isLoading && (
|
||
|
|
<tr>
|
||
|
|
<td colSpan={columns.length} className="text-center relative h-12.5">
|
||
|
|
<div
|
||
|
|
className="loading-animation"
|
||
|
|
>
|
||
|
|
<div
|
||
|
|
className="global-spinner"
|
||
|
|
>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</td>
|
||
|
|
</tr>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{table.getRowModel().rows.length > 0 ? (
|
||
|
|
table.getRowModel().rows.map(row => (
|
||
|
|
<tr key={row.id}>
|
||
|
|
{row.getVisibleCells().map(cell => (
|
||
|
|
<td key={cell.id}>
|
||
|
|
{typeof cell.column.columnDef.cell === 'function'
|
||
|
|
? cell.column.columnDef.cell(cell.getContext())
|
||
|
|
: cell.getValue() as string}
|
||
|
|
</td>
|
||
|
|
))}
|
||
|
|
</tr>
|
||
|
|
))
|
||
|
|
) : (
|
||
|
|
<tr>
|
||
|
|
{!isLoading && (
|
||
|
|
<td colSpan={columns.length} className="text-center">
|
||
|
|
{t('no-data-for-selected-filters')}
|
||
|
|
</td>
|
||
|
|
)}
|
||
|
|
</tr>
|
||
|
|
)}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{/* Пагинация */}
|
||
|
|
<div className="tanstak-table-pagination">
|
||
|
|
<div className="pagination-info">
|
||
|
|
<span className="pagination-info-pages">
|
||
|
|
{t('page')}{' '}
|
||
|
|
<strong>
|
||
|
|
{pagination.pageIndex + 1} {t('out-of')} {totalPages || 1}
|
||
|
|
</strong>
|
||
|
|
</span>
|
||
|
|
<span className="pagination-info-files">
|
||
|
|
| {t('shown')} {tableData.length} {t('out-of')} {totalItems}
|
||
|
|
</span>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="pagination-controls">
|
||
|
|
<button
|
||
|
|
className="arrow"
|
||
|
|
onClick={() => table.firstPage()}
|
||
|
|
disabled={!table.getCanPreviousPage() || isLoading}
|
||
|
|
>
|
||
|
|
<IconDoubleArrowLeft />
|
||
|
|
</button>
|
||
|
|
<button
|
||
|
|
className="arrow"
|
||
|
|
onClick={() => table.previousPage()}
|
||
|
|
disabled={!table.getCanPreviousPage() || isLoading}
|
||
|
|
>
|
||
|
|
<IconArrowLeft />
|
||
|
|
</button>
|
||
|
|
|
||
|
|
<div className="pagination-controls-pages">
|
||
|
|
{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 (
|
||
|
|
<button
|
||
|
|
key={pageIndex}
|
||
|
|
className={pagination.pageIndex === pageIndex ? 'current' : 'other'}
|
||
|
|
onClick={() => table.setPageIndex(pageIndex)}
|
||
|
|
disabled={isLoading}
|
||
|
|
>
|
||
|
|
{pageIndex + 1}
|
||
|
|
</button>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
})}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<button
|
||
|
|
className="arrow"
|
||
|
|
onClick={() => table.nextPage()}
|
||
|
|
disabled={!table.getCanNextPage() || isLoading}
|
||
|
|
>
|
||
|
|
<IconArrowRight />
|
||
|
|
</button>
|
||
|
|
<button
|
||
|
|
className="arrow"
|
||
|
|
onClick={() => table.lastPage()}
|
||
|
|
disabled={!table.getCanNextPage() || isLoading}
|
||
|
|
>
|
||
|
|
<IconDoubleArrowRight />
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div >
|
||
|
|
</div>
|
||
|
|
)
|
||
|
|
}
|