diff --git a/packages/manager-react-components/src/hooks/datagrid/useIcebergV6.tsx b/packages/manager-react-components/src/hooks/datagrid/useIcebergV6.tsx index 8b75b4c6c474..ed6425304a82 100644 --- a/packages/manager-react-components/src/hooks/datagrid/useIcebergV6.tsx +++ b/packages/manager-react-components/src/hooks/datagrid/useIcebergV6.tsx @@ -1,13 +1,17 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { IcebergFetchParamsV6, fetchIcebergV6 } from '@ovh-ux/manager-core-api'; import { useInfiniteQuery } from '@tanstack/react-query'; import { ColumnSort } from '../../components'; +import { defaultPageSize } from './index'; interface IcebergV6Hook { queryKey: string[]; defaultSorting?: ColumnSort; + shouldFetchAll?: boolean; } +export const API_V6_MAX_PAGE_SIZE = 4999; + /** * @deprecated use fetchIcebergV6 from @ovh-ux/manager-core-api */ @@ -15,21 +19,27 @@ export const getResourcesIcebergV6 = fetchIcebergV6; export function useResourcesIcebergV6({ route, - pageSize = 10, + pageSize = defaultPageSize, queryKey, defaultSorting = undefined, + shouldFetchAll = false, }: IcebergFetchParamsV6 & IcebergV6Hook) { const [sorting, setSorting] = useState(defaultSorting); - const { data: dataSelected, ...rest } = useInfiniteQuery({ + const { + data: dataSelected, + hasNextPage, + fetchNextPage, + ...rest + } = useInfiniteQuery({ initialPageParam: 1, - queryKey: [...queryKey, pageSize, sorting], + queryKey: [...queryKey, shouldFetchAll ? 'all' : pageSize, sorting], staleTime: Infinity, retry: false, queryFn: ({ pageParam: pageIndex }) => fetchIcebergV6({ route, - pageSize, + pageSize: shouldFetchAll ? API_V6_MAX_PAGE_SIZE : pageSize, page: pageIndex, sortBy: sorting?.id || null, sortReverse: sorting?.desc, @@ -53,8 +63,16 @@ export function useResourcesIcebergV6({ }, }); + useEffect(() => { + if (shouldFetchAll && hasNextPage) { + fetchNextPage(); + } + }, [dataSelected]); + return { ...(dataSelected ?? { ...dataSelected, totalCount: 0 }), + hasNextPage, + fetchNextPage, ...rest, sorting, setSorting,