mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem On the Query Performance page, more rows only load when the grid fires a scroll event. The first page is 20 rows at 44px each, so on a tall viewport the rows don't overflow the grid. The user can't scroll, and the remaining rows never load. Zooming in works around it. The same dead end happens whenever a client-side filter leaves too few loaded rows to fill the grid. Fixes part of FE-5092. The `calls >= N` filter returning no results is a separate cause and will follow in its own PR. ## Solution `useInfiniteScroll` now takes an optional `containerRef` (the `DataGrid` ref) and `rowCount`. When the ref is passed, the hook also requests the next page whenever the grid's content doesn't overflow its container and there is another page, and it re-checks when the container resizes. Existing callers that don't pass it behave as before. The three `react-data-grid` consumers of the hook share the same dead end, so all three now pass the ref: Query Performance, Cron Jobs and Cron Job runs. For Query Performance, the hook call moved from `WithStatements` into `QueryPerformanceGrid`, which owns the grid ref. ## Review instructions 1. Open Observability → Query Performance on a project with more than 20 statements. 2. Make the browser window tall (or zoom out) so 20 rows don't fill the grid. 3. Confirm more rows load automatically until the grid scrolls, and that scrolling to the bottom still loads further pages. 4. Resize the window taller and confirm more rows load. 5. Repeat on Integrations → Cron (the jobs list and a job's previous runs) if the project has enough jobs or runs to need more than one page. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
import { RefObject, UIEvent, useCallback, useEffect, useRef } from 'react'
|
|
|
|
import { isAtBottom } from '@/lib/helpers'
|
|
|
|
interface UseInfiniteScrollOptions {
|
|
isLoading?: boolean
|
|
isFetchingNextPage?: boolean
|
|
/** True while a background refetch is in flight. */
|
|
isRefetching?: boolean
|
|
/**
|
|
* True when the last request failed. Pauses filling so a failing backend isn't retried in a
|
|
* loop, and re-checks once the error clears.
|
|
*/
|
|
isError?: boolean
|
|
hasNextPage?: boolean
|
|
fetchNextPage: (options?: { cancelRefetch?: boolean }) => void
|
|
/**
|
|
* The scroll container, e.g. the ref of a `react-data-grid` `DataGrid`. When provided, pages
|
|
* are also requested until the container overflows, since the scroll handler can never fire
|
|
* if the first page doesn't fill it (tall viewports, client-side filtered lists).
|
|
*/
|
|
containerRef?: RefObject<{ element: HTMLElement | null } | null>
|
|
/** Number of rendered rows. Re-checks whether the container overflows when this changes. */
|
|
rowCount?: number
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* Pass `containerRef` to also keep loading while the container isn't scrollable.
|
|
*/
|
|
export function useInfiniteScroll({
|
|
isLoading = false,
|
|
isFetchingNextPage = false,
|
|
isRefetching = false,
|
|
isError = false,
|
|
hasNextPage = false,
|
|
fetchNextPage,
|
|
containerRef,
|
|
rowCount,
|
|
}: UseInfiniteScrollOptions) {
|
|
const xScroll = useRef(0)
|
|
|
|
useEffect(() => {
|
|
const container = containerRef?.current?.element
|
|
// While refetching, fetchNextPage would only return the in-flight request, so wait for it
|
|
// to finish and re-check (this effect re-runs when the flag clears).
|
|
if (!container || !hasNextPage || isLoading || isFetchingNextPage || isRefetching || isError) {
|
|
return
|
|
}
|
|
|
|
const fetchIfNotScrollable = () => {
|
|
// A hidden container (display: none) reports 0 for both heights. Don't treat that as
|
|
// underfilled, or every page would be fetched while it's hidden.
|
|
if (container.clientHeight > 0 && container.scrollHeight <= container.clientHeight) {
|
|
fetchNextPage({ cancelRefetch: false })
|
|
}
|
|
}
|
|
|
|
fetchIfNotScrollable()
|
|
|
|
// The container can grow after the first check, e.g. the window is resized
|
|
const observer = new ResizeObserver(fetchIfNotScrollable)
|
|
observer.observe(container)
|
|
return () => observer.disconnect()
|
|
}, [
|
|
containerRef,
|
|
hasNextPage,
|
|
isLoading,
|
|
isFetchingNextPage,
|
|
isRefetching,
|
|
isError,
|
|
fetchNextPage,
|
|
rowCount,
|
|
])
|
|
|
|
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]
|
|
)
|
|
}
|