chore: store pagination for list
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
import { getDefaultStore } from 'jotai';
|
||||||
|
import { atomWithStorage } from 'jotai/utils';
|
||||||
|
|
||||||
|
export interface PaginationState {
|
||||||
|
perPage: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const paginationAtom = atomWithStorage<Record<string, any>>(
|
||||||
|
'paginationStatus',
|
||||||
|
{},
|
||||||
|
undefined,
|
||||||
|
{ getOnInit: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
export const getPaginationStatus = (key: string) => {
|
||||||
|
if (!key) return {};
|
||||||
|
const store = getDefaultStore();
|
||||||
|
const cache = store.get(paginationAtom);
|
||||||
|
return cache[key] || {};
|
||||||
|
};
|
||||||
@@ -30,3 +30,17 @@ export const tableSorter = (order: number | boolean) => {
|
|||||||
// }
|
// }
|
||||||
// return order;
|
// return order;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const PaginationKey = {
|
||||||
|
Deployments: 'Deployments',
|
||||||
|
Workers: 'Workers',
|
||||||
|
Clusters: 'Clusters',
|
||||||
|
Routes: 'Routes',
|
||||||
|
Providers: 'Providers',
|
||||||
|
Benchmarks: 'Benchmarks',
|
||||||
|
GPUs: 'GPUs',
|
||||||
|
ModelFiles: 'ModelFiles',
|
||||||
|
Users: 'Users',
|
||||||
|
APIKeys: 'APIKeys',
|
||||||
|
Credentials: 'Credentials'
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import {
|
||||||
|
getPaginationStatus,
|
||||||
|
paginationAtom,
|
||||||
|
PaginationState
|
||||||
|
} from '@/atoms/pagination';
|
||||||
|
import { useAtom } from 'jotai';
|
||||||
|
|
||||||
|
export const usePaginationStatus = (key: string) => {
|
||||||
|
const [cache, setCache] = useAtom(paginationAtom);
|
||||||
|
|
||||||
|
const value = cache[key] || {};
|
||||||
|
|
||||||
|
const setPagination = (val: PaginationState) => {
|
||||||
|
if (!key) return;
|
||||||
|
|
||||||
|
setCache((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[key]: {
|
||||||
|
...prev[key],
|
||||||
|
...val
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const getPagination = () => {
|
||||||
|
return cache[key] || {};
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearPagination = () => {
|
||||||
|
if (!key) return;
|
||||||
|
|
||||||
|
const newCache = { ...cache };
|
||||||
|
delete newCache[key];
|
||||||
|
setCache(newCache);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
pagination: value,
|
||||||
|
getPaginationStatus,
|
||||||
|
setPagination,
|
||||||
|
getPagination,
|
||||||
|
clearPagination
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -8,6 +8,8 @@ import { handleBatchRequest } from '@/utils';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import qs from 'query-string';
|
import qs from 'query-string';
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { PaginationKey } from '../config/settings';
|
||||||
|
import { usePaginationStatus } from './use-pagination-status';
|
||||||
import { useTableMultiSort } from './use-table-sort';
|
import { useTableMultiSort } from './use-table-sort';
|
||||||
|
|
||||||
type EventsType = 'CREATE' | 'UPDATE' | 'DELETE' | 'INSERT';
|
type EventsType = 'CREATE' | 'UPDATE' | 'DELETE' | 'INSERT';
|
||||||
@@ -33,6 +35,7 @@ type WatchConfig =
|
|||||||
*/
|
*/
|
||||||
export default function useTableFetch<T>(
|
export default function useTableFetch<T>(
|
||||||
options: {
|
options: {
|
||||||
|
key?: (typeof PaginationKey)[keyof typeof PaginationKey];
|
||||||
fetchAPI: (params: any, options?: any) => Promise<Global.PageResponse<T>>;
|
fetchAPI: (params: any, options?: any) => Promise<Global.PageResponse<T>>;
|
||||||
deleteAPI?: (id: number, params?: any) => Promise<any>;
|
deleteAPI?: (id: number, params?: any) => Promise<any>;
|
||||||
contentForDelete?: string;
|
contentForDelete?: string;
|
||||||
@@ -63,6 +66,9 @@ export default function useTableFetch<T>(
|
|||||||
const { sortOrder, handleMultiSortChange } = useTableMultiSort();
|
const { sortOrder, handleMultiSortChange } = useTableMultiSort();
|
||||||
const axiosTokenRef = useRef<any>(null);
|
const axiosTokenRef = useRef<any>(null);
|
||||||
const timerIDRef = useRef<any>(null);
|
const timerIDRef = useRef<any>(null);
|
||||||
|
const hasInitRef = useRef(false);
|
||||||
|
const mountedRef = useRef(true);
|
||||||
|
const { pagination, setPagination } = usePaginationStatus(options.key || '');
|
||||||
|
|
||||||
// for skeleton loading
|
// for skeleton loading
|
||||||
const [extraStatus, setExtraStatus] = useState<Record<string, any>>({
|
const [extraStatus, setExtraStatus] = useState<Record<string, any>>({
|
||||||
@@ -87,6 +93,7 @@ export default function useTableFetch<T>(
|
|||||||
perPage: 10,
|
perPage: 10,
|
||||||
search: '',
|
search: '',
|
||||||
sort_by: '',
|
sort_by: '',
|
||||||
|
...pagination,
|
||||||
...defaultQueryParams
|
...defaultQueryParams
|
||||||
});
|
});
|
||||||
const queryParamsRef = useRef(queryParams);
|
const queryParamsRef = useRef(queryParams);
|
||||||
@@ -297,6 +304,9 @@ export default function useTableFetch<T>(
|
|||||||
|
|
||||||
const handlePageChange = (page: number, pageSize: number) => {
|
const handlePageChange = (page: number, pageSize: number) => {
|
||||||
handleQueryChange({ page, perPage: pageSize || 10 }, { paginate: true });
|
handleQueryChange({ page, perPage: pageSize || 10 }, { paginate: true });
|
||||||
|
setPagination({
|
||||||
|
perPage: pageSize || 10
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleTableChange = (
|
const handleTableChange = (
|
||||||
@@ -402,22 +412,26 @@ export default function useTableFetch<T>(
|
|||||||
}, [dataSource.loadend, queryParams]);
|
}, [dataSource.loadend, queryParams]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let mounted = true;
|
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
await fetchData();
|
if (hasInitRef.current) return;
|
||||||
|
hasInitRef.current = true;
|
||||||
|
await fetchData({
|
||||||
|
query: { ...queryParams, ...pagination }
|
||||||
|
});
|
||||||
|
|
||||||
timerIDRef.current = setTimeout(() => {
|
timerIDRef.current = setTimeout(() => {
|
||||||
if (mounted) {
|
if (mountedRef.current) {
|
||||||
createTableListChunkRequest();
|
createTableListChunkRequest();
|
||||||
}
|
}
|
||||||
}, 200);
|
}, 200);
|
||||||
};
|
};
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
}, [pagination?.perPage]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
mounted = false;
|
mountedRef.current = false;
|
||||||
clearTimeout(timerIDRef.current);
|
clearTimeout(timerIDRef.current);
|
||||||
chunkRequestRef.current?.current?.cancel?.();
|
chunkRequestRef.current?.current?.cancel?.();
|
||||||
axiosTokenRef.current?.cancel?.();
|
axiosTokenRef.current?.cancel?.();
|
||||||
|
|||||||
@@ -172,6 +172,7 @@ const Models: React.FC<{ clusterId?: number }> = ({ clusterId }) => {
|
|||||||
|
|
||||||
const handlePageChange = useMemoizedFn(
|
const handlePageChange = useMemoizedFn(
|
||||||
(page: number, pageSize: number | undefined) => {
|
(page: number, pageSize: number | undefined) => {
|
||||||
|
console.log('page change', page, pageSize);
|
||||||
handleQueryChange({
|
handleQueryChange({
|
||||||
page: page,
|
page: page,
|
||||||
perPage: pageSize || 10
|
perPage: pageSize || 10
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import DeleteModal from '@/components/delete-modal';
|
import DeleteModal from '@/components/delete-modal';
|
||||||
import { FilterBar } from '@/components/page-tools';
|
import { FilterBar } from '@/components/page-tools';
|
||||||
import { TABLE_SORT_DIRECTIONS } from '@/config/settings';
|
import { PaginationKey, TABLE_SORT_DIRECTIONS } from '@/config/settings';
|
||||||
import useTableFetch from '@/hooks/use-table-fetch';
|
import useTableFetch from '@/hooks/use-table-fetch';
|
||||||
import PageBox from '@/pages/_components/page-box';
|
import PageBox from '@/pages/_components/page-box';
|
||||||
import { DockerStepsFromWorker } from '@/pages/cluster-management/components/add-worker/config';
|
import { DockerStepsFromWorker } from '@/pages/cluster-management/components/add-worker/config';
|
||||||
@@ -56,6 +56,7 @@ const Workers: React.FC<{
|
|||||||
handleQueryChange,
|
handleQueryChange,
|
||||||
handleNameChange
|
handleNameChange
|
||||||
} = useTableFetch<ListItem>({
|
} = useTableFetch<ListItem>({
|
||||||
|
key: PaginationKey.Workers,
|
||||||
events: ['UPDATE', 'DELETE', 'CREATE'],
|
events: ['UPDATE', 'DELETE', 'CREATE'],
|
||||||
fetchAPI: queryWorkersList,
|
fetchAPI: queryWorkersList,
|
||||||
deleteAPI: deleteWorker,
|
deleteAPI: deleteWorker,
|
||||||
|
|||||||
Reference in New Issue
Block a user