feat(VirtualList): VirtualList now supports pagination, it loads batches when user scrolls near the end of current batch
This commit is contained in:
@@ -15,7 +15,10 @@ import { cn } from '$shared/shadcn/utils/shadcn-utils';
|
||||
* Load more fonts by moving to the next page
|
||||
*/
|
||||
function loadMore() {
|
||||
if (!unifiedFontStore.pagination.hasMore || unifiedFontStore.isFetching) {
|
||||
if (
|
||||
!unifiedFontStore.pagination.hasMore
|
||||
|| unifiedFontStore.isFetching
|
||||
) {
|
||||
return;
|
||||
}
|
||||
unifiedFontStore.nextPage();
|
||||
@@ -24,15 +27,14 @@ function loadMore() {
|
||||
/**
|
||||
* Handle scroll near bottom - auto-load next page
|
||||
*
|
||||
* Triggered when the user scrolls within 5 items of the end of the list.
|
||||
* Only fetches if there are more pages available and not already fetching.
|
||||
* Triggered by VirtualList when the user scrolls within 5 items of the end
|
||||
* of the loaded items. Only fetches if there are more pages available.
|
||||
*/
|
||||
function handleNearBottom(lastVisibleIndex: number) {
|
||||
const { hasMore, total } = unifiedFontStore.pagination;
|
||||
const itemsRemaining = total - lastVisibleIndex;
|
||||
function handleNearBottom(_lastVisibleIndex: number) {
|
||||
const { hasMore } = unifiedFontStore.pagination;
|
||||
|
||||
// Only trigger if within 5 items of the end, more data exists, and not already fetching
|
||||
if (itemsRemaining <= 5 && hasMore && !unifiedFontStore.isFetching) {
|
||||
// VirtualList already checks if we're near the bottom of loaded items
|
||||
if (hasMore && !unifiedFontStore.isFetching) {
|
||||
loadMore();
|
||||
}
|
||||
}
|
||||
@@ -47,13 +49,8 @@ const displayRange = $derived.by(() => {
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if unifiedFontStore.pagination.total > 0 && !unifiedFontStore.isLoading}
|
||||
<div class="text-sm text-muted-foreground px-2 py-2">
|
||||
{displayRange}
|
||||
{#if unifiedFontStore.isFetching}
|
||||
<span class="ml-2 text-xs text-muted-foreground/70">(Loading...)</span>
|
||||
{/if}
|
||||
</div>
|
||||
{#if unifiedFontStore.isFetching || unifiedFontStore.isLoading}
|
||||
<span class="ml-2 text-xs text-muted-foreground/70">(Loading...)</span>
|
||||
{/if}
|
||||
|
||||
<FontVirtualList
|
||||
|
||||
Reference in New Issue
Block a user