Files
supabase/apps/studio/hooks/misc/useInfiniteScroll.ts
Jordi EnricandClaude Sonnet 4.6 2e1795dfa0 feat(studio): add pagination to query performance page FE-2774 (#43697)
## Problem

The Query Performance page loaded all results in a single query with a
fixed limit of 20 rows, giving users no way to browse beyond the first
page. There was also no way to control how many rows were shown at once.

## Fix

adds pagination 

## How to test

- Navigate to `/observability/query-performance` in Studio
- scroll to bottom
- should automatically load more results

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-23 15:27:43 +01:00

45 lines
1.2 KiB
TypeScript

import { isAtBottom } from 'lib/helpers'
import { UIEvent, useCallback, useRef } from 'react'
interface UseInfiniteScrollOptions {
isLoading?: boolean
isFetchingNextPage?: boolean
hasNextPage?: boolean
fetchNextPage: (options?: { cancelRefetch?: boolean }) => void
}
/**
* Returns a scroll handler that triggers fetchNextPage when the user scrolls
* to the bottom of a scrollable container. Includes horizontal scroll detection
* to avoid triggering loads when the user scrolls horizontally.
*/
export function useInfiniteScroll({
isLoading = false,
isFetchingNextPage = false,
hasNextPage = false,
fetchNextPage,
}: UseInfiniteScrollOptions) {
const xScroll = useRef(0)
return useCallback(
(event: UIEvent<HTMLDivElement>) => {
const isScrollingHorizontally = xScroll.current !== event.currentTarget.scrollLeft
xScroll.current = event.currentTarget.scrollLeft
const shouldFetchNextPage =
!isLoading &&
!isFetchingNextPage &&
!isScrollingHorizontally &&
isAtBottom(event) &&
hasNextPage
if (!shouldFetchNextPage) {
return
}
fetchNextPage({ cancelRefetch: false })
},
[isLoading, isFetchingNextPage, hasNextPage, fetchNextPage]
)
}