2025-12-12 14:22:00 +07:00
|
|
|
'use client';
|
|
|
|
|
|
2025-12-26 16:10:42 +07:00
|
|
|
import { useState, useMemo, useEffect, ReactNode } from 'react';
|
2025-12-12 14:22:00 +07:00
|
|
|
import {
|
|
|
|
|
useReactTable,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
getSortedRowModel,
|
|
|
|
|
getPaginationRowModel,
|
|
|
|
|
getFilteredRowModel,
|
|
|
|
|
ColumnDef,
|
|
|
|
|
SortingState,
|
|
|
|
|
ColumnFiltersState,
|
|
|
|
|
} from '@tanstack/react-table';
|
2026-01-08 11:51:07 +07:00
|
|
|
import { IconImageFile, IconVideoFile, IconAudioFile, IconEye, IconDoubleArrowRight, IconArrowRight, IconDoubleArrowLeft, IconArrowLeft, IconArrowUp, IconArrowDown, IconFilter, IconFileDownload, IconShieldExclamation, IconDelete } from '@/app/ui/icons/icons';
|
2026-01-15 12:27:39 +07:00
|
|
|
import { useTranslations, useLocale } from 'next-intl';
|
2025-12-12 20:28:59 +07:00
|
|
|
import DropDownList from '@/app/components/dropDownList';
|
2025-12-29 14:00:02 +07:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
2025-12-26 14:03:57 +07:00
|
|
|
import { getUserFilesData, removeUserFile } from '@/app/actions/fileEntity';
|
2025-12-26 16:10:42 +07:00
|
|
|
import ModalWindow from '@/app/components/modalWindow';
|
2025-12-29 14:00:02 +07:00
|
|
|
import { toast } from 'sonner';
|
2026-01-14 15:01:30 +07:00
|
|
|
import { pluralize } from '@/app/lib/pluralize';
|
2025-12-12 14:22:00 +07:00
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
type FileType = 'image' | 'video' | 'audio';
|
2025-12-29 14:00:02 +07:00
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
type FileItem = {
|
2025-12-26 14:03:57 +07:00
|
|
|
id: string;
|
2025-12-12 20:28:59 +07:00
|
|
|
fileName: string;
|
2025-12-29 14:00:02 +07:00
|
|
|
fileType: string;
|
2025-12-26 14:03:57 +07:00
|
|
|
violations?: number | undefined;
|
|
|
|
|
checks?: number | undefined;
|
2025-12-29 14:00:02 +07:00
|
|
|
lastCheck?: number;
|
|
|
|
|
_original?: ApiFile;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ApiFile = {
|
|
|
|
|
id: string;
|
|
|
|
|
originalFileName: string;
|
|
|
|
|
mimeType: string;
|
|
|
|
|
violations: number;
|
|
|
|
|
checks: number;
|
|
|
|
|
updatedAt: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ApiResponse = {
|
|
|
|
|
files?: ApiFile[];
|
2025-12-12 14:22:00 +07:00
|
|
|
};
|
|
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
// Иконки для типов файлов
|
2025-12-29 14:00:02 +07:00
|
|
|
const FileTypeIcon = ({ type }: { type: string }) => {
|
2025-12-12 20:28:59 +07:00
|
|
|
switch (type) {
|
|
|
|
|
case 'image':
|
2025-12-30 13:35:20 +07:00
|
|
|
return <span className="color-image">
|
2025-12-12 20:28:59 +07:00
|
|
|
<IconImageFile />
|
|
|
|
|
</span>;
|
|
|
|
|
case 'video':
|
2025-12-30 13:35:20 +07:00
|
|
|
return <span className="color-video">
|
2025-12-12 20:28:59 +07:00
|
|
|
<IconVideoFile />
|
|
|
|
|
</span>;
|
|
|
|
|
case 'audio':
|
2025-12-30 13:35:20 +07:00
|
|
|
return <span className="color-audio">
|
2025-12-12 20:28:59 +07:00
|
|
|
<IconAudioFile />
|
|
|
|
|
</span>;
|
|
|
|
|
default:
|
|
|
|
|
return <span>📄</span>;
|
2025-12-12 14:22:00 +07:00
|
|
|
}
|
2025-12-12 20:28:59 +07:00
|
|
|
};
|
2025-12-12 14:22:00 +07:00
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
// Форматирование даты из timestamp
|
|
|
|
|
const formatDate = (timestamp: number) => {
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
const day = date.getDate().toString().padStart(2, '0');
|
|
|
|
|
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
|
const year = date.getFullYear();
|
2025-12-26 14:03:57 +07:00
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
return `${day}.${month}.${year}`;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-26 14:03:57 +07:00
|
|
|
const formatDateTime = (timestamp: number) => {
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
const hours = date.getHours().toString().padStart(2, '0');
|
|
|
|
|
const minutes = date.getMinutes().toString().padStart(2, '0');
|
|
|
|
|
|
|
|
|
|
return `${hours}:${minutes}`;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-04 17:43:58 +07:00
|
|
|
const cutFileName = (fileName: string) => {
|
2026-01-09 12:35:48 +07:00
|
|
|
const MAX_FILE_NAME_LENGTH = 26;
|
2026-01-04 17:43:58 +07:00
|
|
|
const lastDotIndex = fileName.lastIndexOf('.');
|
|
|
|
|
|
|
|
|
|
if (lastDotIndex <= 0) {
|
2026-01-04 17:44:42 +07:00
|
|
|
return fileName.length > MAX_FILE_NAME_LENGTH ? fileName.substring(0, MAX_FILE_NAME_LENGTH - 3) + '...' : fileName;
|
2026-01-04 17:43:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nameWithoutExtension = fileName.substring(0, lastDotIndex);
|
|
|
|
|
const extension = fileName.substring(lastDotIndex);
|
|
|
|
|
|
2026-01-04 17:44:42 +07:00
|
|
|
if (fileName.length <= MAX_FILE_NAME_LENGTH) {
|
2026-01-04 17:43:58 +07:00
|
|
|
return fileName;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 17:44:42 +07:00
|
|
|
const maxNameLength = MAX_FILE_NAME_LENGTH - extension.length - 3;
|
2026-01-04 17:43:58 +07:00
|
|
|
|
|
|
|
|
if (maxNameLength <= 0) {
|
|
|
|
|
return '...' + extension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nameWithoutExtension.substring(0, maxNameLength) + '...' + extension;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 16:08:38 +07:00
|
|
|
export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
2025-12-26 14:03:57 +07:00
|
|
|
const {
|
2025-12-29 14:00:02 +07:00
|
|
|
data: tableData,
|
2025-12-26 14:03:57 +07:00
|
|
|
isLoading,
|
|
|
|
|
isError,
|
2025-12-26 16:10:42 +07:00
|
|
|
error,
|
2026-01-09 12:35:48 +07:00
|
|
|
} = useQuery<ApiResponse, Error, FileItem[], ['userFilesData', number, number]>({
|
|
|
|
|
queryKey: ['userFilesData', 1, 10000],
|
|
|
|
|
queryFn: () => getUserFilesData(1, 10000),
|
2025-12-26 14:03:57 +07:00
|
|
|
|
2025-12-29 14:00:02 +07:00
|
|
|
select: (data: ApiResponse): FileItem[] => {
|
|
|
|
|
if (!data?.files) return [];
|
2025-12-26 14:03:57 +07:00
|
|
|
|
2025-12-29 14:00:02 +07:00
|
|
|
return data.files.map((item: ApiFile) => {
|
2025-12-26 16:10:42 +07:00
|
|
|
const [datePart, timePart] = item.updatedAt.split(' ');
|
|
|
|
|
const [day, month, year] = datePart.split('-').map(Number);
|
|
|
|
|
const [hours, minutes, seconds] = timePart.split(':').map(Number);
|
|
|
|
|
const newDate = new Date(year, month - 1, day, hours, minutes, seconds).getTime();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: item.id,
|
|
|
|
|
fileName: item.originalFileName,
|
|
|
|
|
fileType: item.mimeType.toLocaleLowerCase(),
|
|
|
|
|
violations: item.violations,
|
|
|
|
|
checks: item.checks,
|
2025-12-29 14:00:02 +07:00
|
|
|
lastCheck: newDate,
|
|
|
|
|
_original: item
|
2025-12-26 16:10:42 +07:00
|
|
|
};
|
|
|
|
|
});
|
2025-12-29 14:00:02 +07:00
|
|
|
},
|
|
|
|
|
});
|
2025-12-26 16:10:42 +07:00
|
|
|
|
2025-12-29 14:00:02 +07:00
|
|
|
const queryClient = useQueryClient();
|
2025-12-26 14:03:57 +07:00
|
|
|
|
2025-12-12 14:22:00 +07:00
|
|
|
// Состояния
|
|
|
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
|
|
|
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
|
2025-12-12 20:28:59 +07:00
|
|
|
const [dateFilter, setDateFilter] = useState<string>('all');
|
2025-12-29 16:08:38 +07:00
|
|
|
const [typeFilter, setTypeFilter] = useState<string>(fileType);
|
2025-12-26 16:10:42 +07:00
|
|
|
const [pagination, setPagination] = useState({
|
|
|
|
|
pageIndex: 0,
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
});
|
|
|
|
|
const [openWindow, setOpenWindow] = useState<boolean>(false);
|
|
|
|
|
const [openWindowChildren, setOpenWindowChildren] = useState<ReactNode>(null);
|
2025-12-12 20:28:59 +07:00
|
|
|
|
2026-01-02 12:52:01 +07:00
|
|
|
const [isFileLoading, setIsFileLoading] = useState(false);
|
|
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
const t = useTranslations("Global");
|
2026-01-15 12:27:39 +07:00
|
|
|
const locale = useLocale();
|
2025-12-12 14:22:00 +07:00
|
|
|
|
|
|
|
|
// Определение колонок
|
2025-12-12 20:28:59 +07:00
|
|
|
const columns = useMemo<ColumnDef<FileItem>[]>(
|
2025-12-12 14:22:00 +07:00
|
|
|
() => [
|
|
|
|
|
{
|
2025-12-12 20:28:59 +07:00
|
|
|
accessorKey: 'fileName',
|
|
|
|
|
header: ({ column }) => (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="column start">
|
2025-12-12 20:28:59 +07:00
|
|
|
<span>
|
|
|
|
|
{t('file')}
|
|
|
|
|
</span>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
2025-12-30 13:35:20 +07:00
|
|
|
className="sort-button"
|
2025-12-12 20:28:59 +07:00
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
column.getIsSorted() === 'asc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowUp />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: column.getIsSorted() === 'desc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowDown />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: <span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconFilter />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-12-12 14:22:00 +07:00
|
|
|
),
|
|
|
|
|
cell: ({ row }) => (
|
2026-01-04 17:43:58 +07:00
|
|
|
<div className="flex items-center space-x-2">
|
2025-12-12 20:28:59 +07:00
|
|
|
<FileTypeIcon type={row.original.fileType} />
|
|
|
|
|
<div>
|
2026-01-04 17:43:58 +07:00
|
|
|
<div className="font-medium w-full truncate" title={row.original.fileName}>
|
|
|
|
|
{/* {row.original.fileName} */}
|
|
|
|
|
{cutFileName(row.original.fileName)}
|
|
|
|
|
</div>
|
2025-12-12 20:28:59 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-12-12 14:22:00 +07:00
|
|
|
),
|
|
|
|
|
enableColumnFilter: false,
|
|
|
|
|
},
|
2025-12-26 14:03:57 +07:00
|
|
|
{
|
|
|
|
|
accessorKey: 'status',
|
|
|
|
|
header: ({ column }) => (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="column">
|
2025-12-26 14:03:57 +07:00
|
|
|
<span>
|
2026-01-08 11:16:59 +07:00
|
|
|
{t('status')}
|
2025-12-26 14:03:57 +07:00
|
|
|
</span>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
2025-12-30 13:35:20 +07:00
|
|
|
className="sort-button"
|
2025-12-26 14:03:57 +07:00
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
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`}>
|
2026-01-08 11:16:59 +07:00
|
|
|
{t('status')}
|
2025-12-26 14:03:57 +07:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-12-12 14:22:00 +07:00
|
|
|
{
|
2025-12-12 20:28:59 +07:00
|
|
|
accessorKey: 'violations',
|
|
|
|
|
header: ({ column }) => (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="column">
|
2025-12-12 20:28:59 +07:00
|
|
|
<span>
|
|
|
|
|
{t('violations')}
|
|
|
|
|
</span>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
2025-12-30 13:35:20 +07:00
|
|
|
className="sort-button"
|
2025-12-12 20:28:59 +07:00
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
column.getIsSorted() === 'asc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowUp />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: column.getIsSorted() === 'desc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowDown />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: <span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconFilter />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2025-12-26 14:03:57 +07:00
|
|
|
cell: ({ row }) => {
|
|
|
|
|
let classCollor = () => {
|
|
|
|
|
let result = ''
|
|
|
|
|
if (row.original.violations !== undefined) {
|
|
|
|
|
result = row.original.violations > 0 ? 'text-red-600' : 'text-green-600'
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<div className={`text-center font-semibold ${classCollor()}`}>
|
|
|
|
|
{row.original.violations !== undefined ? row.original.violations : '-'}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
2025-12-12 14:22:00 +07:00
|
|
|
},
|
|
|
|
|
{
|
2025-12-12 20:28:59 +07:00
|
|
|
accessorKey: 'checks',
|
|
|
|
|
header: ({ column }) => (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="column">
|
2025-12-12 20:28:59 +07:00
|
|
|
<span>
|
|
|
|
|
{t('checks')}
|
|
|
|
|
</span>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
2025-12-30 13:35:20 +07:00
|
|
|
className="sort-button"
|
2025-12-12 20:28:59 +07:00
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
column.getIsSorted() === 'asc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowUp />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: column.getIsSorted() === 'desc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowDown />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: <span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconFilter />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
cell: ({ row }) => (
|
|
|
|
|
<div className="text-center">
|
2025-12-26 14:03:57 +07:00
|
|
|
{row.original.checks !== undefined ? row.original.checks : '-'}
|
2025-12-12 20:28:59 +07:00
|
|
|
</div>
|
|
|
|
|
),
|
2025-12-12 14:22:00 +07:00
|
|
|
},
|
|
|
|
|
{
|
2025-12-12 20:28:59 +07:00
|
|
|
accessorKey: 'lastCheck',
|
|
|
|
|
header: ({ column }) => (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="column">
|
2025-12-12 20:28:59 +07:00
|
|
|
<span>
|
|
|
|
|
{t('last-check')}
|
|
|
|
|
</span>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
2025-12-30 13:35:20 +07:00
|
|
|
className="sort-button"
|
2025-12-12 20:28:59 +07:00
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
column.getIsSorted() === 'asc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowUp />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: column.getIsSorted() === 'desc' ?
|
|
|
|
|
<span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowDown />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
: <span>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconFilter />
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2025-12-26 14:03:57 +07:00
|
|
|
cell: ({ row }) => {
|
|
|
|
|
return (
|
|
|
|
|
<div className="text-center">
|
|
|
|
|
{row.original.lastCheck ? (
|
|
|
|
|
<>
|
|
|
|
|
{formatDate(row.original.lastCheck)}
|
|
|
|
|
<br />
|
|
|
|
|
{formatDateTime(row.original.lastCheck)}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<div>-</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
2025-12-12 20:28:59 +07:00
|
|
|
enableColumnFilter: false,
|
2025-12-12 14:22:00 +07:00
|
|
|
},
|
|
|
|
|
{
|
2025-12-12 20:28:59 +07:00
|
|
|
id: 'actions',
|
|
|
|
|
header: () => {
|
|
|
|
|
return (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="column">
|
2025-12-12 20:28:59 +07:00
|
|
|
{t('actions')}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
cell: ({ row }) => (
|
2026-01-04 13:27:59 +07:00
|
|
|
<div className="actions">
|
2026-01-04 17:08:28 +07:00
|
|
|
<div className="actions-group">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => handleView(row.original)}
|
|
|
|
|
className="bg-blue-500 hover:bg-blue-600"
|
|
|
|
|
>
|
|
|
|
|
<IconEye />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => handleDownload(row.original)}
|
|
|
|
|
disabled={isFileLoading}
|
|
|
|
|
className="bg-green-500 hover:bg-green-600"
|
|
|
|
|
>
|
2026-01-08 11:51:07 +07:00
|
|
|
<IconFileDownload />
|
2026-01-04 17:08:28 +07:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="actions-group">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => handleProtect(row.original)}
|
|
|
|
|
className="bg-violet-500 hover:bg-violet-600"
|
|
|
|
|
>
|
|
|
|
|
<IconShieldExclamation />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => handleOpenWindowForRemove(row.original)}
|
|
|
|
|
className="bg-red-700 hover:bg-red-800"
|
|
|
|
|
>
|
|
|
|
|
<IconDelete />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
enableSorting: false,
|
|
|
|
|
enableColumnFilter: false,
|
2025-12-12 14:22:00 +07:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
// Обработчики действий
|
|
|
|
|
const handleView = (file: FileItem) => {
|
|
|
|
|
console.log(`Просмотр файла: ${file.fileName}`);
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-02 12:52:01 +07:00
|
|
|
const handleDownload = async (file: FileItem) => {
|
|
|
|
|
setIsFileLoading(true);
|
2026-01-02 13:00:37 +07:00
|
|
|
|
2026-01-02 12:52:01 +07:00
|
|
|
try {
|
|
|
|
|
const response = await fetch(`/api/download/${file.id}`);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`error: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
a.href = url;
|
|
|
|
|
a.download = file.fileName || `file-${file.id}`;
|
|
|
|
|
document.body.appendChild(a);
|
|
|
|
|
a.click();
|
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
|
document.body.removeChild(a);
|
2026-01-02 13:00:37 +07:00
|
|
|
toast.success(`${t('file-is-downloading')} - ${file.fileName}`);
|
2026-01-02 12:52:01 +07:00
|
|
|
} catch (error) {
|
2026-01-02 13:00:37 +07:00
|
|
|
toast.error(t('failed-to-download-file'))
|
2026-01-02 12:52:01 +07:00
|
|
|
} finally {
|
|
|
|
|
setIsFileLoading(false);
|
|
|
|
|
}
|
2025-12-12 20:28:59 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleProtect = (file: FileItem) => {
|
|
|
|
|
console.log(`Щиток: ${file.fileName}`);
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-29 14:00:02 +07:00
|
|
|
const handleOpenWindowForRemove = async (file: FileItem) => {
|
2025-12-26 16:10:42 +07:00
|
|
|
|
|
|
|
|
setOpenWindowChildren(() => {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h3 className="text-2xl">
|
|
|
|
|
{t('you-sure-you-want-to-delete')}
|
|
|
|
|
</h3>
|
|
|
|
|
<div className="mt-2 mb-8 text-center">{file.fileName}</div>
|
|
|
|
|
<div className="flex justify-center gap-4">
|
|
|
|
|
<button className="btn-primary btn-modal"
|
2025-12-29 14:00:02 +07:00
|
|
|
onClick={() => {
|
|
|
|
|
deleteMutation.mutate(file.id)
|
2025-12-26 16:10:42 +07:00
|
|
|
}}
|
2025-12-29 14:00:02 +07:00
|
|
|
disabled={deleteMutation.isPending}
|
2025-12-26 16:10:42 +07:00
|
|
|
>
|
2025-12-29 14:00:02 +07:00
|
|
|
{deleteMutation.isPending ? '...' : t('yes')}
|
2025-12-26 16:10:42 +07:00
|
|
|
</button>
|
|
|
|
|
<button className="btn-primary btn-modal"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setOpenWindow(false);
|
|
|
|
|
setOpenWindowChildren(null);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('no')}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
setOpenWindow(true);
|
2025-12-26 14:03:57 +07:00
|
|
|
};
|
|
|
|
|
|
2025-12-29 14:00:02 +07:00
|
|
|
const deleteMutation = useMutation({
|
|
|
|
|
mutationFn: removeUserFile,
|
|
|
|
|
|
|
|
|
|
onMutate: async (fileId: string) => {
|
|
|
|
|
await queryClient.cancelQueries({ queryKey: ['userFilesData'] });
|
|
|
|
|
|
|
|
|
|
queryClient.setQueryData<ApiResponse>(['userFilesData'], (old) => {
|
|
|
|
|
if (!old?.files) return old;
|
|
|
|
|
return {
|
|
|
|
|
...old,
|
|
|
|
|
files: old.files.filter(file => file.id !== fileId)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onError: () => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ['userFilesData'] });
|
|
|
|
|
toast.error(t('error'));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onSuccess: (response, fileId) => {
|
|
|
|
|
if (response === fileId) {
|
2025-12-29 16:08:38 +07:00
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: ['userFilesData'],
|
|
|
|
|
refetchType: 'active'
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-09 12:35:48 +07:00
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: ['userFilesInfo']
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-29 14:00:02 +07:00
|
|
|
setOpenWindow(false);
|
|
|
|
|
setOpenWindowChildren(null);
|
|
|
|
|
toast.success(t('file-has-been-deleted'));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-12 20:28:59 +07:00
|
|
|
// Фильтрация по типу файла и дате
|
|
|
|
|
const filteredData = useMemo(() => {
|
2025-12-26 14:03:57 +07:00
|
|
|
let result = tableData;
|
2025-12-29 14:00:02 +07:00
|
|
|
if (!result) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2025-12-12 20:28:59 +07:00
|
|
|
|
|
|
|
|
// Фильтр по типу файла
|
|
|
|
|
if (typeFilter !== 'all') {
|
|
|
|
|
result = result.filter(item => item.fileType === typeFilter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Фильтр по дате
|
|
|
|
|
if (dateFilter !== 'all') {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const oneDay = 24 * 60 * 60 * 1000;
|
|
|
|
|
const sevenDays = 7 * oneDay;
|
|
|
|
|
const thirtyDays = 30 * oneDay;
|
|
|
|
|
|
|
|
|
|
switch (dateFilter) {
|
|
|
|
|
case 'today':
|
|
|
|
|
const todayStart = new Date().setHours(0, 0, 0, 0);
|
2025-12-26 14:03:57 +07:00
|
|
|
result = result.filter(item => {
|
|
|
|
|
if (item.lastCheck) {
|
|
|
|
|
return item.lastCheck >= todayStart
|
|
|
|
|
} else {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-12 20:28:59 +07:00
|
|
|
break;
|
|
|
|
|
case 'week':
|
|
|
|
|
const weekAgo = now - sevenDays;
|
2025-12-26 14:03:57 +07:00
|
|
|
result = result.filter(item => {
|
|
|
|
|
if (item.lastCheck) {
|
|
|
|
|
return item.lastCheck >= weekAgo;
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-12 20:28:59 +07:00
|
|
|
break;
|
|
|
|
|
case 'month':
|
|
|
|
|
const monthAgo = now - thirtyDays;
|
2025-12-26 14:03:57 +07:00
|
|
|
result = result.filter(item => {
|
|
|
|
|
if (item.lastCheck) {
|
|
|
|
|
return item.lastCheck >= monthAgo;
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-12 20:28:59 +07:00
|
|
|
break;
|
|
|
|
|
case 'older':
|
|
|
|
|
const monthAgo2 = now - thirtyDays;
|
2025-12-26 14:03:57 +07:00
|
|
|
result = result.filter(item => {
|
|
|
|
|
if (item.lastCheck) {
|
|
|
|
|
return item.lastCheck < monthAgo2;
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-12 20:28:59 +07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-12-26 14:03:57 +07:00
|
|
|
}, [tableData, typeFilter, dateFilter]);
|
2025-12-12 20:28:59 +07:00
|
|
|
|
2025-12-26 16:10:42 +07:00
|
|
|
useEffect(() => {
|
|
|
|
|
const currentPageRows = table.getRowModel().rows;
|
|
|
|
|
const pageCount = table.getPageCount();
|
|
|
|
|
|
|
|
|
|
if (currentPageRows.length === 0 && pagination.pageIndex > 0 && pageCount > 0) {
|
|
|
|
|
table.setPageIndex(pagination.pageIndex - 1);
|
|
|
|
|
}
|
|
|
|
|
}, [filteredData, pagination.pageIndex]);
|
|
|
|
|
|
2025-12-12 14:22:00 +07:00
|
|
|
// Создание таблицы
|
|
|
|
|
const table = useReactTable({
|
2025-12-12 20:28:59 +07:00
|
|
|
data: filteredData,
|
2025-12-12 14:22:00 +07:00
|
|
|
columns,
|
|
|
|
|
state: {
|
|
|
|
|
sorting,
|
|
|
|
|
columnFilters,
|
2025-12-26 16:10:42 +07:00
|
|
|
pagination
|
2025-12-12 14:22:00 +07:00
|
|
|
},
|
2025-12-26 16:10:42 +07:00
|
|
|
autoResetPageIndex: false,
|
|
|
|
|
onPaginationChange: setPagination,
|
2025-12-12 14:22:00 +07:00
|
|
|
onSortingChange: setSorting,
|
|
|
|
|
onColumnFiltersChange: setColumnFilters,
|
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
getSortedRowModel: getSortedRowModel(),
|
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
|
|
|
getFilteredRowModel: getFilteredRowModel(),
|
2025-12-12 20:28:59 +07:00
|
|
|
initialState: {
|
|
|
|
|
pagination: {
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-12-12 14:22:00 +07:00
|
|
|
});
|
|
|
|
|
|
2026-01-14 15:01:30 +07:00
|
|
|
const pluralizeFiles = (number: number) => {
|
|
|
|
|
const translate = [t('file'), t('files-few'), t('files')];
|
2026-01-15 12:27:39 +07:00
|
|
|
return pluralize(number, translate[0], translate[1], translate[2], locale);
|
2026-01-14 15:01:30 +07:00
|
|
|
};
|
|
|
|
|
|
2025-12-12 14:22:00 +07:00
|
|
|
return (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="tanstak-table-wrapper">
|
2025-12-26 16:10:42 +07:00
|
|
|
<ModalWindow children={openWindowChildren} state={openWindow} callBack={setOpenWindow}></ModalWindow>
|
2025-12-12 20:28:59 +07:00
|
|
|
{/* Фильтры */}
|
2025-12-30 13:35:20 +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>
|
2025-12-12 20:28:59 +07:00
|
|
|
<DropDownList
|
2025-12-19 13:05:50 +07:00
|
|
|
value={dateFilter}
|
|
|
|
|
translatedValue={(() => {
|
2025-12-15 11:24:07 +07:00
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
})()}
|
2025-12-12 20:28:59 +07:00
|
|
|
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>
|
2025-12-12 14:22:00 +07:00
|
|
|
|
2025-12-29 16:08:38 +07:00
|
|
|
{fileType === "all" && (
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="table-filtres-item">
|
|
|
|
|
<div className="table-filtres-label">{t('type-filter')}:</div>
|
2025-12-29 16:08:38 +07:00
|
|
|
<DropDownList
|
|
|
|
|
value={typeFilter}
|
|
|
|
|
translatedValue={
|
|
|
|
|
(() => {
|
|
|
|
|
switch (typeFilter) {
|
|
|
|
|
case 'image':
|
|
|
|
|
return t('images')
|
|
|
|
|
case 'video':
|
|
|
|
|
return t('videos')
|
|
|
|
|
case 'audio':
|
|
|
|
|
return t('audios')
|
|
|
|
|
default:
|
|
|
|
|
return t('all-types')
|
|
|
|
|
}
|
|
|
|
|
})()
|
|
|
|
|
}
|
|
|
|
|
callBack={setTypeFilter}
|
|
|
|
|
>
|
|
|
|
|
<li value="all">
|
|
|
|
|
{t('all-types')}
|
|
|
|
|
</li>
|
|
|
|
|
<li value="image">
|
2026-01-08 17:21:09 +07:00
|
|
|
{t('images-few')}
|
2025-12-29 16:08:38 +07:00
|
|
|
</li>
|
|
|
|
|
<li value="video">
|
|
|
|
|
{t('videos')}
|
|
|
|
|
</li>
|
|
|
|
|
<li value="audio">
|
|
|
|
|
{t('audios')}
|
|
|
|
|
</li>
|
|
|
|
|
</DropDownList>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-12-12 20:28:59 +07:00
|
|
|
</div>
|
2025-12-12 14:22:00 +07:00
|
|
|
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="table-filtres-item">
|
|
|
|
|
<div className="table-filtres-label">{t('items-per-page')}:</div>
|
2025-12-12 20:28:59 +07:00
|
|
|
<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>
|
2025-12-12 14:22:00 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Таблица */}
|
2025-12-30 13:35:20 +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>
|
2025-12-12 20:28:59 +07:00
|
|
|
))}
|
|
|
|
|
</tr>
|
2025-12-30 13:35:20 +07:00
|
|
|
))
|
|
|
|
|
) : (
|
|
|
|
|
<tr>
|
|
|
|
|
<td colSpan={columns.length} className="text-center">
|
|
|
|
|
{t('no-data-for-selected-filters')}
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
)}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2025-12-12 14:22:00 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Пагинация */}
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="tanstak-table-pagination">
|
|
|
|
|
<div className="pagination-info">
|
|
|
|
|
<span className="pagination-info-pages">
|
2025-12-12 20:28:59 +07:00
|
|
|
{t('page')}{' '}
|
|
|
|
|
<strong>
|
2025-12-19 13:05:50 +07:00
|
|
|
{table.getState().pagination.pageIndex + 1} {t('out-of')} {table.getPageCount() ? table.getPageCount() : 1}
|
2025-12-12 20:28:59 +07:00
|
|
|
</strong>
|
|
|
|
|
</span>
|
2025-12-30 13:35:20 +07:00
|
|
|
<span className="pagination-info-files">
|
2026-01-14 15:01:30 +07:00
|
|
|
| {t('shown')} {table.getRowModel().rows.length} {t('out-of')} {filteredData.length} {pluralizeFiles(filteredData.length || 0)}
|
2025-12-12 20:28:59 +07:00
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="pagination-controls">
|
2025-12-12 14:22:00 +07:00
|
|
|
<button
|
2025-12-30 13:35:20 +07:00
|
|
|
className="arrow"
|
2025-12-12 20:28:59 +07:00
|
|
|
onClick={() => table.firstPage()}
|
2025-12-12 14:22:00 +07:00
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconDoubleArrowLeft />
|
2025-12-12 14:22:00 +07:00
|
|
|
</button>
|
|
|
|
|
<button
|
2025-12-30 13:35:20 +07:00
|
|
|
className="arrow"
|
2025-12-12 14:22:00 +07:00
|
|
|
onClick={() => table.previousPage()}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowLeft />
|
2025-12-12 14:22:00 +07:00
|
|
|
</button>
|
2025-12-12 20:28:59 +07:00
|
|
|
|
2025-12-30 13:35:20 +07:00
|
|
|
<div className="pagination-controls-pages">
|
2025-12-12 20:28:59 +07:00
|
|
|
{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;
|
|
|
|
|
|
|
|
|
|
if (pageIndex < table.getPageCount()) {
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
key={pageIndex}
|
2025-12-30 13:35:20 +07:00
|
|
|
className={`${table.getState().pagination.pageIndex === pageIndex
|
|
|
|
|
? 'current'
|
|
|
|
|
: 'other'
|
2025-12-12 20:28:59 +07:00
|
|
|
}`}
|
|
|
|
|
onClick={() => table.setPageIndex(pageIndex)}
|
|
|
|
|
>
|
|
|
|
|
{pageIndex + 1}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-12-12 14:22:00 +07:00
|
|
|
<button
|
2025-12-30 13:35:20 +07:00
|
|
|
className="arrow"
|
2025-12-12 14:22:00 +07:00
|
|
|
onClick={() => table.nextPage()}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconArrowRight />
|
2025-12-12 14:22:00 +07:00
|
|
|
</button>
|
|
|
|
|
<button
|
2025-12-30 13:35:20 +07:00
|
|
|
className="arrow"
|
2025-12-12 20:28:59 +07:00
|
|
|
onClick={() => table.lastPage()}
|
2025-12-12 14:22:00 +07:00
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
2025-12-17 16:41:07 +07:00
|
|
|
<IconDoubleArrowRight />
|
2025-12-12 14:22:00 +07:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|