Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/features/dashboard/templates/list/table-body.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { flexRender, type Table } from '@tanstack/react-table'
import Link from 'next/link'
import posthog from 'posthog-js'
import type { RefObject } from 'react'
import { type RefObject, useEffect } from 'react'
import { PROTECTED_URLS } from '@/configs/urls'
import type { Template } from '@/core/modules/templates/models'
import { useRouteParams } from '@/lib/hooks/use-route-params'
Expand All @@ -18,6 +18,7 @@ import { useTemplateTableStore } from './stores/table-store'
const ROW_HEIGHT_PX = 32
const VIRTUAL_OVERSCAN = 8
const INITIAL_FALLBACK_ROW_COUNT = 100
const PREFETCH_THRESHOLD = 8

interface TemplatesTableBodyProps {
templates: Template[] | undefined
Expand Down Expand Up @@ -46,6 +47,7 @@ export function TemplatesTableBody({
const centerRows = table.getCenterRows()
const {
virtualRows,
virtualizer,
totalHeight: virtualizedTotalHeight,
paddingTop: virtualPaddingTop,
} = useVirtualRows<Template>({
Expand All @@ -55,6 +57,26 @@ export function TemplatesTableBody({
overscan: VIRTUAL_OVERSCAN,
})

const lastVisibleIndex = virtualizer.getVirtualItems().at(-1)?.index ?? -1

useEffect(() => {
if (
hasNextPage &&
!isFetchingNextPage &&
!isRefetching &&
lastVisibleIndex >= centerRows.length - PREFETCH_THRESHOLD
Comment thread
Kraci marked this conversation as resolved.
) {
fetchNextPage()
}
}, [
hasNextPage,
isFetchingNextPage,
isRefetching,
lastVisibleIndex,
centerRows.length,
fetchNextPage,
])

const rows =
virtualRows.length > 0
? virtualRows
Expand Down
Loading