add debounce for tanstak table
This commit is contained in:
@@ -50,7 +50,9 @@ export async function getUserFilesData({
|
|||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
let parsed = await response.json();
|
let parsed = await response.json();
|
||||||
console.log(parsed);
|
|
||||||
|
/* console.log(parsed); */
|
||||||
|
|
||||||
if (parsed.message_code === 0) {
|
if (parsed.message_code === 0) {
|
||||||
return parsed.message_body;
|
return parsed.message_body;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useMemo, useEffect, ReactNode } from 'react';
|
import { useState, useMemo, useEffect, ReactNode, useCallback } from 'react';
|
||||||
import {
|
import {
|
||||||
useReactTable,
|
useReactTable,
|
||||||
getCoreRowModel,
|
getCoreRowModel,
|
||||||
@@ -22,7 +22,6 @@ import { toast } from 'sonner';
|
|||||||
import { pluralize } from '@/app/lib/pluralize';
|
import { pluralize } from '@/app/lib/pluralize';
|
||||||
import { convertBytes } from '@/app/lib/convertBytes';
|
import { convertBytes } from '@/app/lib/convertBytes';
|
||||||
import { FileInfoModalWindow } from '@/app/ui/modal-windows/file-info-modal-window';
|
import { FileInfoModalWindow } from '@/app/ui/modal-windows/file-info-modal-window';
|
||||||
import { FileMonitoringModalWindow } from '@/app/ui/modal-windows/file-monitoring-modal-window';
|
|
||||||
import { FileTypeIcon } from '@/app/components/FileTypeIcon';
|
import { FileTypeIcon } from '@/app/components/FileTypeIcon';
|
||||||
import { fetchReferralUserStats } from '@/app/actions/referralsActions';
|
import { fetchReferralUserStats } from '@/app/actions/referralsActions';
|
||||||
import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown';
|
import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown';
|
||||||
@@ -75,7 +74,6 @@ const cutFileExtension = (fileName: string) => {
|
|||||||
export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||||
|
|
||||||
const [sorting, setSorting] = useState<SortingState>([]);
|
const [sorting, setSorting] = useState<SortingState>([]);
|
||||||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
|
|
||||||
const [dateFilter, setDateFilter] = useState<string>('month');
|
const [dateFilter, setDateFilter] = useState<string>('month');
|
||||||
const [typeFilter, setTypeFilter] = useState<string>(fileType);
|
const [typeFilter, setTypeFilter] = useState<string>(fileType);
|
||||||
const [pagination, setPagination] = useState({
|
const [pagination, setPagination] = useState({
|
||||||
@@ -83,7 +81,29 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
pageSize: 10,
|
pageSize: 10,
|
||||||
});
|
});
|
||||||
const [query, setQuery] = useState<string>('');
|
const [query, setQuery] = useState<string>('');
|
||||||
|
const [searchInputValue, setSearchInputValue] = useState<string>('');
|
||||||
|
const [searchQuery, setSearchQuery] = useState<string>('');
|
||||||
|
|
||||||
|
const debouncedSetSearchQuery = useDebouncedCallback(
|
||||||
|
(value: string) => {
|
||||||
|
setSearchQuery(value);
|
||||||
|
setPagination(prev => ({ ...prev, pageIndex: 0 }));
|
||||||
|
},
|
||||||
|
500
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSearchInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const value = e.target.value;
|
||||||
|
setSearchInputValue(value);
|
||||||
|
debouncedSetSearchQuery(value);
|
||||||
|
}, [debouncedSetSearchQuery]);
|
||||||
|
|
||||||
|
const handleClearSearch = useCallback(() => {
|
||||||
|
setSearchInputValue('');
|
||||||
|
setSearchQuery('');
|
||||||
|
debouncedSetSearchQuery.cancel();
|
||||||
|
setPagination(prev => ({ ...prev, pageIndex: 0 }));
|
||||||
|
}, [debouncedSetSearchQuery]);
|
||||||
|
|
||||||
const getSortParams = useMemo(() => {
|
const getSortParams = useMemo(() => {
|
||||||
if (sorting.length === 0) {
|
if (sorting.length === 0) {
|
||||||
@@ -130,7 +150,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
getSortParams.sortOrder,
|
getSortParams.sortOrder,
|
||||||
getTypeParam,
|
getTypeParam,
|
||||||
getDateParam,
|
getDateParam,
|
||||||
query
|
searchQuery
|
||||||
],
|
],
|
||||||
queryFn: () => getUserFilesData({
|
queryFn: () => getUserFilesData({
|
||||||
page: pagination.pageIndex + 1,
|
page: pagination.pageIndex + 1,
|
||||||
@@ -139,7 +159,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
sortOrder: getSortParams?.sortOrder,
|
sortOrder: getSortParams?.sortOrder,
|
||||||
type: getTypeParam,
|
type: getTypeParam,
|
||||||
date: getDateParam,
|
date: getDateParam,
|
||||||
query: query
|
query: searchQuery
|
||||||
}),
|
}),
|
||||||
select: (data: any): {
|
select: (data: any): {
|
||||||
items: FileItem[];
|
items: FileItem[];
|
||||||
@@ -535,19 +555,6 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleMonitoring = async (file: FileItem) => {
|
|
||||||
console.log(`Мониторинг:`, file);
|
|
||||||
|
|
||||||
const fileInfo = await viewFileInfo(file.id);
|
|
||||||
|
|
||||||
setOpenWindowChildren(() => {
|
|
||||||
return (
|
|
||||||
<FileMonitoringModalWindow file={fileInfo} setWindowClose={setOpenWindow} setWindowChildren={setOpenWindowChildren} />
|
|
||||||
)
|
|
||||||
})
|
|
||||||
setOpenWindow(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Фильтрация по типу файла и дате
|
// Фильтрация по типу файла и дате
|
||||||
const filteredData = useMemo(() => {
|
const filteredData = useMemo(() => {
|
||||||
let result = tableData;
|
let result = tableData;
|
||||||
@@ -650,11 +657,6 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
setPagination(prev => ({ ...prev, pageIndex: 0 }));
|
setPagination(prev => ({ ...prev, pageIndex: 0 }));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleQueryChange = (newQuery: string) => {
|
|
||||||
setQuery(newQuery);
|
|
||||||
setPagination(prev => ({ ...prev, pageIndex: 0 }));
|
|
||||||
};
|
|
||||||
|
|
||||||
const pluralizeFiles = (number: number) => {
|
const pluralizeFiles = (number: number) => {
|
||||||
const translate = [t('file'), t('files-few'), t('files')];
|
const translate = [t('file'), t('files-few'), t('files')];
|
||||||
return pluralize(number, translate[0], translate[1], translate[2], locale);
|
return pluralize(number, translate[0], translate[1], translate[2], locale);
|
||||||
@@ -753,8 +755,8 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
className="table-filtres-text-filter"
|
className="table-filtres-text-filter"
|
||||||
value={query}
|
value={searchInputValue}
|
||||||
onChange={(e) => handleQueryChange(e.target.value)}
|
onChange={handleSearchInputChange}
|
||||||
placeholder={t('search')}
|
placeholder={t('search')}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user