{"version":3,"file":"useInfiniteQuery.cjs","names":["useBaseQuery","InfiniteQueryObserver"],"sources":["../../src/useInfiniteQuery.ts"],"sourcesContent":["'use client'\nimport { InfiniteQueryObserver } from '@tanstack/query-core'\nimport { useBaseQuery } from './useBaseQuery'\nimport type {\n  DefaultError,\n  InfiniteData,\n  QueryClient,\n  QueryKey,\n  QueryObserver,\n} from '@tanstack/query-core'\nimport type {\n  DefinedUseInfiniteQueryResult,\n  UseInfiniteQueryOptions,\n  UseInfiniteQueryResult,\n} from './types'\nimport type {\n  DefinedInitialDataInfiniteOptions,\n  UndefinedInitialDataInfiniteOptions,\n} from './infiniteQueryOptions'\n\n/**\n * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of\n * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.\n *\n * This overload is selected when `initialData` is set.\n *\n * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default\n * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user\n * actions, or add conditions like `hasNextPage && !isFetching`.\n * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.\n * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` set.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,\n * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and\n * `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default\n * `InfiniteData<TQueryFnData>` shape.\n *\n * @example\n * ```tsx\n * import { useInfiniteQuery } from '@tanstack/react-query'\n *\n * function Projects() {\n *   // `data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the\n *   // list stays visible alongside the error.\n *   const { data, isError, error } = useInfiniteQuery({\n *     queryKey: ['projects'],\n *     queryFn: ({ pageParam }) => fetchProjects(pageParam),\n *     initialPageParam: 0,\n *     getNextPageParam: (lastPage) => lastPage.nextId,\n *     initialData: { pages: [], pageParams: [] },\n *   })\n *\n *   return (\n *     <div>\n *       {isError ? <span>Error: {error.message}</span> : null}\n *       <ul>\n *         {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}\n *       </ul>\n *     </div>\n *   )\n * }\n * ```\n */\nexport function useInfiniteQuery<\n  TQueryFnData,\n  TError = DefaultError,\n  TData = InfiniteData<TQueryFnData>,\n  TQueryKey extends QueryKey = QueryKey,\n  TPageParam = unknown,\n>(\n  options: DefinedInitialDataInfiniteOptions<\n    TQueryFnData,\n    TError,\n    TData,\n    TQueryKey,\n    TPageParam\n  >,\n  queryClient?: QueryClient,\n): DefinedUseInfiniteQueryResult<TData, TError>\n\n/**\n * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of\n * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.\n *\n * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default\n * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user\n * actions, or add conditions like `hasNextPage && !isFetching`.\n * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.\n * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,\n * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and\n * `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default\n * `InfiniteData<TQueryFnData>` shape.\n *\n * @example\n * Fetching the next page from a \"Load More\" button click:\n * ```tsx\n * import { useInfiniteQuery } from '@tanstack/react-query'\n *\n * function Projects() {\n *   const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =\n *     useInfiniteQuery({\n *       queryKey: ['projects'],\n *       queryFn: ({ pageParam }) => fetchProjects(pageParam),\n *       initialPageParam: 0,\n *       getNextPageParam: (lastPage) => lastPage.nextId,\n *     })\n *\n *   if (isPending) return 'Loading...'\n *   if (isError) return <span>Error: {error.message}</span>\n *\n *   return (\n *     <>\n *       <ul>\n *         {data.pages.map((page) =>\n *           page.projects.map((project) => <li key={project.id}>{project.name}</li>),\n *         )}\n *       </ul>\n *       <button\n *         onClick={() => fetchNextPage()}\n *         disabled={!hasNextPage || isFetching}\n *       >\n *         {isFetchingNextPage\n *           ? 'Loading more...'\n *           : hasNextPage\n *             ? 'Load More'\n *             : 'Nothing more to load'}\n *       </button>\n *     </>\n *   )\n * }\n * ```\n *\n * @example\n * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a\n * sentinel element after the list:\n * ```tsx\n * import { useInfiniteQuery } from '@tanstack/react-query'\n * import { useEffect, useRef } from 'react'\n *\n * function Projects() {\n *   const {\n *     data,\n *     isPending,\n *     isError,\n *     error,\n *     fetchNextPage,\n *     hasNextPage,\n *     isFetching,\n *     isFetchingNextPage,\n *   } = useInfiniteQuery({\n *     queryKey: ['projects'],\n *     queryFn: ({ pageParam }) => fetchProjects(pageParam),\n *     initialPageParam: 0,\n *     getNextPageParam: (lastPage) => lastPage.nextId,\n *   })\n *\n *   const sentinelRef = useRef<HTMLDivElement>(null)\n *\n *   useEffect(() => {\n *     const sentinel = sentinelRef.current\n *     if (sentinel == null || !hasNextPage || isFetching) return\n *\n *     const observer = new IntersectionObserver(([entry]) => {\n *       if (entry?.isIntersecting) fetchNextPage()\n *     })\n *     observer.observe(sentinel)\n *\n *     return () => observer.disconnect()\n *   }, [hasNextPage, isFetching, fetchNextPage])\n *\n *   if (isPending) return 'Loading...'\n *   if (isError) return <span>Error: {error.message}</span>\n *\n *   return (\n *     <>\n *       <ul>\n *         {data.pages.map((page) =>\n *           page.projects.map((project) => <li key={project.id}>{project.name}</li>),\n *         )}\n *       </ul>\n *       <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>\n *     </>\n *   )\n * }\n * ```\n */\nexport function useInfiniteQuery<\n  TQueryFnData,\n  TError = DefaultError,\n  TData = InfiniteData<TQueryFnData>,\n  TQueryKey extends QueryKey = QueryKey,\n  TPageParam = unknown,\n>(\n  options: UndefinedInitialDataInfiniteOptions<\n    TQueryFnData,\n    TError,\n    TData,\n    TQueryKey,\n    TPageParam\n  >,\n  queryClient?: QueryClient,\n): UseInfiniteQueryResult<TData, TError>\n\n/**\n * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of\n * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.\n *\n * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default\n * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user\n * actions, or add conditions like `hasNextPage && !isFetching`.\n * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.\n * @param options - The {@link UseInfiniteQueryOptions} to use — everything you can pass to `useInfiniteQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,\n * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and\n * `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default\n * `InfiniteData<TQueryFnData>` shape.\n *\n * @example\n * Fetching the next page from a \"Load More\" button click:\n * ```tsx\n * import { useInfiniteQuery } from '@tanstack/react-query'\n *\n * function Projects() {\n *   const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =\n *     useInfiniteQuery({\n *       queryKey: ['projects'],\n *       queryFn: ({ pageParam }) => fetchProjects(pageParam),\n *       initialPageParam: 0,\n *       getNextPageParam: (lastPage) => lastPage.nextId,\n *     })\n *\n *   if (isPending) return 'Loading...'\n *   if (isError) return <span>Error: {error.message}</span>\n *\n *   return (\n *     <>\n *       <ul>\n *         {data.pages.map((page) =>\n *           page.projects.map((project) => <li key={project.id}>{project.name}</li>),\n *         )}\n *       </ul>\n *       <button\n *         onClick={() => fetchNextPage()}\n *         disabled={!hasNextPage || isFetching}\n *       >\n *         {isFetchingNextPage\n *           ? 'Loading more...'\n *           : hasNextPage\n *             ? 'Load More'\n *             : 'Nothing more to load'}\n *       </button>\n *     </>\n *   )\n * }\n * ```\n *\n * @example\n * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a\n * sentinel element after the list:\n * ```tsx\n * import { useInfiniteQuery } from '@tanstack/react-query'\n * import { useEffect, useRef } from 'react'\n *\n * function Projects() {\n *   const {\n *     data,\n *     isPending,\n *     isError,\n *     error,\n *     fetchNextPage,\n *     hasNextPage,\n *     isFetching,\n *     isFetchingNextPage,\n *   } = useInfiniteQuery({\n *     queryKey: ['projects'],\n *     queryFn: ({ pageParam }) => fetchProjects(pageParam),\n *     initialPageParam: 0,\n *     getNextPageParam: (lastPage) => lastPage.nextId,\n *   })\n *\n *   const sentinelRef = useRef<HTMLDivElement>(null)\n *\n *   useEffect(() => {\n *     const sentinel = sentinelRef.current\n *     if (sentinel == null || !hasNextPage || isFetching) return\n *\n *     const observer = new IntersectionObserver(([entry]) => {\n *       if (entry?.isIntersecting) fetchNextPage()\n *     })\n *     observer.observe(sentinel)\n *\n *     return () => observer.disconnect()\n *   }, [hasNextPage, isFetching, fetchNextPage])\n *\n *   if (isPending) return 'Loading...'\n *   if (isError) return <span>Error: {error.message}</span>\n *\n *   return (\n *     <>\n *       <ul>\n *         {data.pages.map((page) =>\n *           page.projects.map((project) => <li key={project.id}>{project.name}</li>),\n *         )}\n *       </ul>\n *       <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>\n *     </>\n *   )\n * }\n * ```\n *\n * @example\n * A query that's disabled, type safe, until `postId` is set — pass `skipToken` as `queryFn`\n * instead of setting `enabled: false`:\n * ```tsx\n * import { skipToken, useInfiniteQuery } from '@tanstack/react-query'\n *\n * function Comments({ postId }: { postId: string | undefined }) {\n *   // Use `isLoading`, not `isPending`, so the loading state doesn't show while the query is disabled.\n *   const { data, isLoading, isError, error } = useInfiniteQuery({\n *     queryKey: ['post', postId, 'comments'],\n *     queryFn:\n *       postId != null\n *         ? ({ pageParam }) => fetchComments(postId, pageParam)\n *         : skipToken,\n *     initialPageParam: 0,\n *     getNextPageParam: (lastPage) => lastPage.nextId,\n *   })\n *\n *   if (postId == null) return 'Select a post'\n *   if (isLoading) return 'Loading...'\n *   if (isError) return <span>Error: {error.message}</span>\n *\n *   return (\n *     <ul>\n *       {data?.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}\n *     </ul>\n *   )\n * }\n * ```\n */\nexport function useInfiniteQuery<\n  TQueryFnData,\n  TError = DefaultError,\n  TData = InfiniteData<TQueryFnData>,\n  TQueryKey extends QueryKey = QueryKey,\n  TPageParam = unknown,\n>(\n  options: UseInfiniteQueryOptions<\n    TQueryFnData,\n    TError,\n    TData,\n    TQueryKey,\n    TPageParam\n  >,\n  queryClient?: QueryClient,\n): UseInfiniteQueryResult<TData, TError>\n\nexport function useInfiniteQuery(\n  options: UseInfiniteQueryOptions,\n  queryClient?: QueryClient,\n) {\n  return useBaseQuery(\n    options,\n    InfiniteQueryObserver as typeof QueryObserver,\n    queryClient,\n  )\n}\n"],"mappings":";;;;;AA2WA,SAAgB,iBACd,SACA,aACA;CACA,OAAOA,qBAAAA,aACL,SACAC,qBAAAA,uBACA,WACF;AACF"}