'use client' import { useState, useMemo, useCallback } from 'react'; import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowModel, getFilteredRowModel, ColumnDef, SortingState, } from '@tanstack/react-table'; import { IconArrowUp, IconArrowDown, IconFilter, IconArrowRight, IconArrowLeft, IconDoubleArrowLeft, IconDoubleArrowRight, IconEye } from '@/app/ui/icons/icons'; import { useTranslations } from 'next-intl'; import { useLastCases, Case } from '@/app/hooks/react-query/useLastCases'; import { formatDate, formatDateTime } from '@/app/lib/formatDate'; import Link from 'next/link'; export function ViolationsMyClaimsTable() { const [sorting, setSorting] = useState([]); const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 5, }); const t = useTranslations("Global"); const getSortParams: { sort_by: string; sort_direction: 'asc' | 'desc' } = useMemo(() => { if (sorting.length === 0) { return { sort_by: 'createdAt', sort_direction: 'desc' as const }; } const sort = sorting[0]; const sortByMap: Record = { 'caseName': 'name', 'createdAt': 'created_at', 'updatedAt': 'updated_at', 'status': 'type' }; return { sort_by: sortByMap[sort.id] || sort.id, sort_direction: sort.desc ? 'desc' : 'asc' as const }; }, [sorting]); const { data: apiResponse, isLoading, isFetching, } = useLastCases({ page: pagination.pageIndex + 1, size: pagination.pageSize, sort_by: getSortParams.sort_by, sort_direction: getSortParams.sort_direction ? getSortParams.sort_direction : 'desc', }); const tableData = apiResponse?.content || []; const totalItems = apiResponse?.totalElements || 0; const totalPages = apiResponse?.totalPages || 0; const columns = useMemo[]>( () => [ { accessorKey: 'caseName', header: ({ column }) => (
{t('case-name')}
), cell: ({ row }) => (
{row.original.name.length > 50 ? `${row.original.name.substring(0, 50)}...` : row.original.name}
), }, { accessorKey: 'createdAt', header: ({ column }) => (
{t('date-of-creation')}
), cell: ({ row }) => (
{formatDate(new Date(row.original.created_at).getTime())}
{formatDateTime(new Date(row.original.created_at).getTime())}
), }, { accessorKey: 'updatedAt', header: ({ column }) => (
{t('date-of-update')}
), cell: ({ row }) => (
{formatDate(new Date(row.original.updated_at).getTime())}
{formatDateTime(new Date(row.original.updated_at).getTime())}
), }, { accessorKey: 'status', header: ({ column }) => (
{t('status')}
), cell: ({ row }) => { const status = row.original.type; const statusColor = status === 'ACTIVE' ? 'text-green-600' : 'text-gray-500'; return (
{t(status.toLowerCase())}
); }, }, { id: 'actions', header: () =>
{t('actions')}
, cell: ({ row }) => (
), enableSorting: false, }, ], [] ); const table = useReactTable({ data: tableData, columns, state: { sorting, pagination, }, pageCount: totalPages, manualPagination: true, manualSorting: 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('claims')}

{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')}
{/* Pagination */}
{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; })}
); }