Files
no-copy-frontend/src/app/ui/violations/violations-table.tsx
T

509 lines
13 KiB
TypeScript
Raw Normal View History

2026-03-12 17:44:02 +07:00
'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';
2026-03-16 17:12:52 +07:00
import { IconArrowUp, IconArrowDown, IconFilter, IconDoubleArrowRight, IconArrowRight, IconDoubleArrowLeft, IconArrowLeft, IconEye } from '@/app/ui/icons/icons';
2026-03-12 17:44:02 +07:00
import { useQuery } from '@tanstack/react-query';
2026-03-16 17:12:52 +07:00
import { getViolationFilesArray } from '@/app/actions/violationActions';
import { useViewport } from '@/app/hooks/useViewport';
import { formatDate, formatDateTime } from '@/app/lib/formatDate';
import Link from 'next/link';
2026-03-12 17:44:02 +07:00
2026-03-16 17:12:52 +07:00
interface ViolationFile {
createdAt: string;
fileId: string;
fileName: string;
fileSize: number;
latestViolationDate: string;
mimeType: string;
status: string;
supportId: number;
violationCount: number;
}
2026-01-15 14:18:08 +07:00
export default function ViolationsTable() {
2026-03-12 17:44:02 +07:00
const {
data: violationData,
isLoading,
isError,
error,
2026-03-16 17:12:52 +07:00
} = useQuery<ViolationFile[]>({
queryKey: ['violationData'],
queryFn: () => getViolationFilesArray(),
2026-03-12 17:44:02 +07:00
select: (data) => {
2026-03-16 17:12:52 +07:00
return data;
2026-03-12 17:44:02 +07:00
},
2026-03-16 17:12:52 +07:00
/* refetchInterval: 30000 */
2026-03-12 17:44:02 +07:00
});
const t = useTranslations('Global');
// Состояния
const [sorting, setSorting] = useState<SortingState>([]);
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const [pagination, setPagination] = useState({
pageIndex: 0,
pageSize: 10,
});
2026-03-16 17:12:52 +07:00
const viewport = useViewport();
const getMaxLengthByWidth = (): number => {
if (viewport.device === 'MOBILE') return 26;
if (viewport.device === 'TABLET') return 32;
if (viewport.device === 'DESKTOP') return 84;
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 '...' /* + extension */;
}
return nameWithoutExtension.substring(0, maxNameLength) + '...' /* + extension */;
}
const columns = useMemo<ColumnDef<ViolationFile>[]>(
2026-03-12 17:44:02 +07:00
() => [
{
2026-03-16 17:12:52 +07:00
accessorKey: 'supportId',
2026-03-12 17:44:02 +07:00
header: ({ column }) => (
<div className="column">
<span>
2026-03-16 17:12:52 +07:00
ID
2026-03-12 17:44:02 +07:00
</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>
<div className="text-center table-item table-item-id">
2026-03-16 17:12:52 +07:00
{row.original.supportId}
2026-03-12 17:44:02 +07:00
</div>
</div>
)
}
},
{
2026-04-09 13:50:18 +07:00
accessorKey: 'fileName',
2026-03-12 17:44:02 +07:00
header: ({ column }) => (
2026-03-16 17:12:52 +07:00
<div className="column start">
2026-03-12 17:44:02 +07:00
<span>
2026-03-16 17:12:52 +07:00
{t('file')}
2026-03-12 17:44:02 +07:00
</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 (
2026-03-16 17:12:52 +07:00
<div>
<div
className="flex items-center space-x-2 table-item table-item-file-name"
title={row.original.fileName}
>
{cutFileName(row.original.fileName)}
</div>
2026-03-12 17:44:02 +07:00
</div>
)
2026-03-16 17:12:52 +07:00
}
},
{
2026-04-09 13:50:18 +07:00
accessorKey: 'violationCount',
2026-03-16 17:12:52 +07:00
header: ({ column }) => (
<div className="column">
<span>
2026-04-21 13:11:51 +07:00
{t('matches')}
2026-03-16 17:12:52 +07:00
</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.violationCount}
</div>
),
},
{
2026-04-09 13:50:18 +07:00
accessorKey: 'latestViolationDate',
2026-03-16 17:12:52 +07:00
header: ({ column }) => (
<div className="column">
<span>
{t('date')}
</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">
{formatDate(row.original.latestViolationDate)}
<br />
{formatDateTime(row.original.latestViolationDate)}
</div>
),
2026-03-12 17:44:02 +07:00
},
{
accessorKey: 'actions',
header: ({ column }) => (
<div className="column">
<span>
2026-03-16 17:12:52 +07:00
{t('actions')}
2026-03-12 17:44:02 +07:00
</span>
</div>
),
cell: ({ row }) => {
return (
2026-03-16 17:12:52 +07:00
<div className="actions">
<div
className="actions-group"
>
{/* <button
onClick={() => {
console.log(row.original.fileId);
}}
className="bg-violet-500 hover:bg-violet-600"
title={t('view')}
>
<IconEye />
</button> */}
<Link
2026-03-26 11:56:36 +07:00
href={`/pages/file/${row.original.fileId}`}
2026-03-16 17:12:52 +07:00
className="bg-violet-500 hover:bg-violet-600"
title={t('view')}
>
<IconEye />
</Link>
</div>
2026-03-12 17:44:02 +07:00
</div>
)
}
},
],
[]
)
const filteredData = useMemo(() => {
2026-03-16 17:12:52 +07:00
let result = violationData;
2026-03-12 17:44:02 +07:00
if (!result) {
return [];
}
return result
2026-03-16 17:12:52 +07:00
}, [violationData])
2026-03-12 17:44:02 +07:00
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,
},
},
});
2026-01-15 14:18:08 +07:00
return (
2026-03-12 17:44:02 +07:00
<div
className="block-wrapper"
>
<div className="tab-content">
<div
className="tanstak-table-wrapper"
>
<h3
className="tanstak-table-title"
>
2026-04-21 13:11:51 +07:00
{t('matches-detected')}
2026-03-12 17:44:02 +07:00
</h3>
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
{/* Фильтры */}
{/* <div className="tanstak-table-filtres">
<div className="table-filtres-wrapper">
<div className="table-filtres-item">
<div className="table-filtres-label">{t('date-filter')}:</div>
<DropDownList
value={dateFilter}
translatedValue={(() => {
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}
>
<li value="all">
{t('all-dates')}
</li>
<li value="today">
{t('today')}
</li>
<li value="week">
{t('for-a-week')}
</li>
<li value="month">
{t('for-a-month')}
</li>
<li value="older">
{t('older-than-a-month')}
</li>
</DropDownList>
</div>
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
</div>
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
<div
className="table-filtres-wrapper"
>
<div className="table-filtres-item">
<div className="table-filtres-label">{t('items-per-page')}:</div>
<DropDownList
value={table.getState().pagination.pageSize}
//@ts-ignore сделал так потому что для table.setPageSize нужна цифра
callBack={table.setPageSize}
>
{[5, 10, 20, 50, 100].map(pageSize => (
<li key={pageSize} value={pageSize}>
{t('show')} {pageSize}
</li>
))}
</DropDownList>
</div>
</div>
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
</div> */}
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
{/* Таблица */}
<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">
{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>
<td colSpan={columns.length} className="text-center">
{t('no-data-for-selected-filters')}
</td>
</tr>
)}
</tbody>
</table>
</div>
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
{/* Пагинация */}
<div className="tanstak-table-pagination">
<div className="pagination-info">
<span className="pagination-info-pages">
{t('page')}{' '}
<strong>
{table.getState().pagination.pageIndex + 1} {t('out-of')} {table.getPageCount() ? table.getPageCount() : 1}
</strong>
</span>
<span className="pagination-info-files">
| {t('shown')} {table.getRowModel().rows.length} {t('out-of')} {filteredData.length}
</span>
</div>
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
<div className="pagination-controls">
{table.getCanPreviousPage() && (
<button
className="arrow"
onClick={() => table.firstPage()}
disabled={!table.getCanPreviousPage()}
>
<IconDoubleArrowLeft />
2026-01-15 14:18:08 +07:00
</button>
2026-03-12 17:44:02 +07:00
)}
{table.getCanPreviousPage() && (
<button
className="arrow"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
<IconArrowLeft />
</button>
)}
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
<div className="pagination-controls-pages">
{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;
2026-01-15 14:18:08 +07:00
2026-03-12 17:44:02 +07:00
if (pageIndex < table.getPageCount()) {
return (
<button
key={pageIndex}
className={`${table.getState().pagination.pageIndex === pageIndex
? 'current'
: 'other'
}`}
onClick={() => table.setPageIndex(pageIndex)}
>
{pageIndex + 1}
</button>
);
}
return null;
})}
</div>
{table.getCanNextPage() && (
<button
className="arrow"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
<IconArrowRight />
</button>
)}
{table.getCanNextPage() && (
<button
className="arrow"
onClick={() => table.lastPage()}
disabled={!table.getCanNextPage()}
>
<IconDoubleArrowRight />
</button>
)}
</div>
</div>
</div>
2026-01-15 14:18:08 +07:00
</div>
</div>
)
}