fix sorting for document view

This commit is contained in:
smanylov
2026-05-16 16:56:31 +07:00
parent d0793a281e
commit 210145f5ca
3 changed files with 51 additions and 17 deletions
@@ -2,7 +2,7 @@
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 { 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';
@@ -19,6 +19,25 @@ export function TrackingHistoryView() {
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<string, string> = {
'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,
@@ -26,7 +45,7 @@ export function TrackingHistoryView() {
isError,
error,
refetch
} = useHistoryView(pagination.pageIndex, pagination.pageSize, locale);
} = useHistoryView(pagination.pageIndex, pagination.pageSize, locale, getSortParams.sortOrder, getSortParams.sortBy);
const tableData = apiResponse?.history || [];
const totalItems = apiResponse?.history.length || 0;
@@ -65,14 +84,15 @@ export function TrackingHistoryView() {
return nameWithoutExtension.substring(0, maxNameLength) + '...';
}
const editLocationName = (location: string | undefined | null, place: 'city' | 'country') => {
let notDefinedPlace = (place === 'city') ? t('not-defined-city') : t('not-defined-country');
location === 'UNKNOWN' ? notDefinedPlace : location;
let response = notDefinedPlace;
if (location !== 'UNKNOWN' && location !== undefined && location !== null) {
response = location;
}
return response;
const editLocationName = (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 `${translatedCity}/${translatedCountry}`;
}
const columns = useMemo<ColumnDef<HistoryViewItem>[]>(
@@ -220,10 +240,10 @@ export function TrackingHistoryView() {
cell: ({ row }) => {
return (
<div className="text-center font-semibold table-item">
{row.original.city === 'UNKNOWN' && row.original.country === 'UNKNOWN' ? (
{row.original.country === 'unknow/unknow' ? (
t('not-defined')
) : (
`${editLocationName(row.original.city, 'city')} / ${editLocationName(row.original.country, 'country')}`
`${editLocationName(row.original.country)}`
)}
</div>
)