Files
Drew McLellan 9aed3bcb6e fix(studio): load more query performance rows when the grid isn't scrollable (#53457)
## 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)
2026-11-17 15:15:33 +00:00

257 lines
6.9 KiB
TypeScript

import { useParams } from 'common'
import dayjs from 'dayjs'
import { CircleCheckIcon, CircleXIcon, LoaderIcon } from 'lucide-react'
import { useMemo, useRef } from 'react'
import DataGrid, { Column, DataGridHandle, Row } from 'react-data-grid'
import { cn, LoadingLine, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { CodeBlock } from 'ui-patterns/CodeBlock'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { TimestampInfo } from 'ui-patterns/TimestampInfo'
import { calculateDuration, formatDate } from './CronJobs.utils'
import CronJobsEmptyState from './CronJobsEmptyState'
import {
CronJobRun,
useCronJobRunsInfiniteQuery,
} from '@/data/database-cron-jobs/database-cron-jobs-runs-infinite-query'
import { useInfiniteScroll } from '@/hooks/misc/useInfiniteScroll'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
const cronJobColumns = [
{
id: 'runid',
name: 'RunID',
minWidth: 30,
width: 30,
value: (row: CronJobRun) => (
<div className="flex items-center gap-1.5">
<h3 className="text-xs">{row.runid}</h3>
</div>
),
},
{
id: 'message',
name: 'Message',
minWidth: 200,
value: (row: CronJobRun) => (
<div className="flex items-center gap-1.5">
{row.return_message ? (
<Tooltip>
<TooltipTrigger asChild>
<span className="text-xs cursor-pointer truncate max-w-[300px]">
{row.return_message}
</span>
</TooltipTrigger>
<TooltipContent
side="bottom"
align="start"
className="min-w-[200px] max-w-[300px] text-wrap p-0"
>
<p className="text-xs font-mono px-2 py-1 border-b bg-muted">Message</p>
<CodeBlock
hideLineNumbers
language="sql"
value={row.return_message.trim()}
className={cn(
'py-0 px-3.5 max-w-full prose dark:prose-dark border-0 rounded-t-none',
'[&>code]:m-0 [&>code>span]:flex [&>code>span]:flex-wrap min-h-11',
'[&>code]:text-xs'
)}
/>
</TooltipContent>
</Tooltip>
) : (
<span>-</span>
)}
</div>
),
},
{
id: 'status',
name: 'Status',
minWidth: 75,
value: (row: CronJobRun) => <StatusBadge status={row.status} />,
},
{
id: 'start_time',
name: 'Start Time',
minWidth: 120,
value: (row: CronJobRun) => <div className="text-xs">{formatDate(row.start_time)}</div>,
},
{
id: 'end_time',
name: 'End Time',
minWidth: 120,
value: (row: CronJobRun) => (
<div className="flex items-center text-xs">
{row.end_time ? formatDate(row.end_time) : '-'}
</div>
),
},
{
id: 'duration',
name: 'Duration',
minWidth: 100,
value: (row: CronJobRun) => (
<div className="flex items-center">
<span className="text-xs">
{row.start_time && row.end_time ? calculateDuration(row.start_time, row.end_time) : ''}
</span>
</div>
),
},
]
const columns = cronJobColumns.map((col) => {
const result: Column<CronJobRun> = {
key: col.id,
name: col.name,
resizable: true,
minWidth: col.minWidth ?? 120,
headerCellClass: undefined,
renderHeaderCell: () => {
return (
<div
className={cn(
'flex items-center justify-between font-normal text-xs w-full',
col.id === 'runid' && 'ml-8'
)}
>
<p className="text-foreground!">{col.name}</p>
</div>
)
},
renderCell: (props) => {
const value = col.value(props.row)
if (['start_time', 'end_time'].includes(col.id)) {
const rawValue = (props.row as any)[(col as any).id]
if (rawValue) {
const formattedValue = dayjs(rawValue).valueOf()
return (
<div className="flex items-center">
<TimestampInfo
utcTimestamp={formattedValue}
labelFormat="DD MMM YYYY HH:mm:ss (ZZ)"
className="text-xs"
/>
</div>
)
}
}
return value
},
}
return result
})
export const PreviousRunsTab = () => {
const { childId } = useParams()
const { data: project } = useSelectedProjectQuery()
const jobId = Number(childId)
const {
data,
isPending: isLoadingCronJobRuns,
isFetching,
isFetchingNextPage,
isRefetching,
isError,
hasNextPage,
fetchNextPage,
} = useCronJobRunsInfiniteQuery(
{
projectRef: project?.ref,
connectionString: project?.connectionString,
jobId: jobId,
},
{ enabled: !!jobId, staleTime: 30000 }
)
const cronJobRuns = useMemo(() => data?.pages.flatMap((p) => p) || [], [data?.pages])
const gridRef = useRef<DataGridHandle>(null)
const handleScroll = useInfiniteScroll({
isLoading: isLoadingCronJobRuns,
isFetchingNextPage,
isRefetching,
isError,
hasNextPage,
fetchNextPage,
containerRef: gridRef,
rowCount: cronJobRuns.length,
})
return (
<div className="h-full flex flex-col">
<LoadingLine loading={isFetching} />
<DataGrid
ref={gridRef}
className="grow border-t-0! border-b-0!"
rowHeight={44}
headerRowHeight={36}
onScroll={handleScroll}
columns={columns}
rows={cronJobRuns ?? []}
rowClass={() => {
return cn(
'cursor-pointer',
'[&>.rdg-cell]:border-box [&>.rdg-cell]:outline-hidden [&>.rdg-cell]:shadow-none',
'[&>.rdg-cell:first-child>div]:ml-8'
)
}}
renderers={{
renderRow(_idx, props) {
return <Row key={props.row.job_pid} {...props} />
},
noRowsFallback: isLoadingCronJobRuns ? (
<div className="absolute top-14 px-6 w-full">
<GenericSkeletonLoader />
</div>
) : (
<div className="flex items-center justify-center w-full col-span-6">
<CronJobsEmptyState page="runs" />
</div>
),
}}
/>
</div>
)
}
interface StatusBadgeProps {
status: string
}
function StatusBadge({ status }: StatusBadgeProps) {
if (status === 'succeeded') {
return (
<span className="text-brand-600 flex items-center gap-1 text-xs">
<CircleCheckIcon size={14} /> Succeeded
</span>
)
}
if (status === 'failed') {
return (
<span className="text-destructive flex items-center gap-1 text-xs">
<CircleXIcon size={14} /> Failed
</span>
)
}
if (['running', 'starting', 'sending', 'connecting'].includes(status)) {
return (
<span className="text-_secondary flex items-center gap-1 text-xs">
<LoaderIcon size={14} className="animate-spin" /> Running
</span>
)
}
return null
}