import { useInfiniteQuery } from 'react-query';

import { getFileList } from 'src/services/files';
import { QueryParams } from 'src/utils/files';

import { filesKeys } from '../queries';

export const useInfiniteFiles = (
  params: QueryParams,
  currentFolderId?: number | undefined,
) => {
  return useInfiniteQuery(
    filesKeys.detail(currentFolderId, params),
    ({ pageParam = 1 }) =>
      getFileList(currentFolderId, {
        page: pageParam,
        ...params,
      }),
    {
      getNextPageParam: lastPage => {
        return lastPage.data.page < lastPage.data.totalPages
          ? lastPage.data.page + 1
          : undefined;
      },
      staleTime: 5 * 60 * 1000,
    },
  );
};
