|
| 1 | +import { useInfiniteQuery, useQueryClient } from "@tanstack/solid-query" |
| 2 | +import { For } from "solid-js" |
| 3 | +import { |
| 4 | + ScrollPaginationObserver, |
| 5 | + useScrollPagination, |
| 6 | +} from "@/components/scroll-pagination-observer" |
| 7 | +import { Badge } from "@/components/ui/badge" |
| 8 | +import { Button } from "@/components/ui/button" |
| 9 | + |
| 10 | +// Mock async fetcher that returns a page of items |
| 11 | +const all = Array.from({ length: 20 }, (_, i) => ({ |
| 12 | + id: i + 1, |
| 13 | + title: `Item ${i + 1}`, |
| 14 | +})) |
| 15 | +const fetchPage = async (page: number) => { |
| 16 | + await new Promise((r) => setTimeout(r, 1000)) // simulate network delay |
| 17 | + const limit = 3 // default from paginationDTO |
| 18 | + const start = (page - 1) * limit // pages start at 1 |
| 19 | + const items = all.slice(start, start + limit) |
| 20 | + return { |
| 21 | + items, |
| 22 | + hasMore: start + limit < all.length, |
| 23 | + total: all.length, |
| 24 | + page, |
| 25 | + limit, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export function ScrollPaginationExample() { |
| 30 | + const queryClient = useQueryClient() |
| 31 | + |
| 32 | + const infiniteQuery = useInfiniteQuery(() => ({ |
| 33 | + queryKey: ["scroll-pagination-items"], |
| 34 | + queryFn: async ({ pageParam }) => fetchPage(pageParam), |
| 35 | + initialPageParam: 1, |
| 36 | + getNextPageParam: (lastPage) => { |
| 37 | + return lastPage.hasMore ? lastPage.page + 1 : undefined |
| 38 | + }, |
| 39 | + })) |
| 40 | + const { scrollContainerRef, loadTriggerRef, loadTriggerState, _entry } = useScrollPagination( |
| 41 | + () => ({ |
| 42 | + onLoadMore: () => infiniteQuery.fetchNextPage(), |
| 43 | + isLoading: infiniteQuery.isFetching, |
| 44 | + hasMore: infiniteQuery.hasNextPage, |
| 45 | + }) |
| 46 | + ) |
| 47 | + |
| 48 | + // flatten pages into one list |
| 49 | + const items = () => infiniteQuery.data?.pages.flatMap((p) => p.items) ?? [] |
| 50 | + |
| 51 | + const handleReset = () => { |
| 52 | + queryClient.resetQueries({ queryKey: ["scroll-pagination-items"] }) |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <div class="w-72 p-6"> |
| 57 | + <Badge variant={_entry()?.isIntersecting ? "success" : "info"}> |
| 58 | + {_entry()?.isIntersecting ? "Intersecting" : "Idle"} |
| 59 | + </Badge> |
| 60 | + <div class="mb-4 flex items-center justify-between gap-5 text-xs"> |
| 61 | + <h2 class="">Items ({items().length}/ 20)</h2> |
| 62 | + <Button onClick={handleReset} variant="ghost" size="sm"> |
| 63 | + Reset |
| 64 | + </Button> |
| 65 | + </div> |
| 66 | + |
| 67 | + <div class="h-full max-h-52 overflow-y-auto" ref={scrollContainerRef}> |
| 68 | + <ul class="space-y-2"> |
| 69 | + <For each={items()}> |
| 70 | + {(item) => <li class="rounded border p-3 shadow-sm">{item.title}</li>} |
| 71 | + </For> |
| 72 | + </ul> |
| 73 | + |
| 74 | + <ScrollPaginationObserver ref={loadTriggerRef} state={loadTriggerState} /> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + ) |
| 78 | +} |
0 commit comments