|
1 | | -import type { FC } from 'react'; |
| 1 | +import { type FC, useEffect } from 'react'; |
2 | 2 | import { MainLayout } from '../../components/Layout/MainLayout'; |
3 | 3 | import { ExploreEmptyState } from './emptyState'; |
| 4 | +import { useInfiniteQuery } from '@tanstack/react-query'; |
| 5 | +import { useNetwork } from '../../hooks/useNetwork'; |
| 6 | +import { Skeleton } from '@oasisprotocol/ui-library/src/components/ui/skeleton'; |
| 7 | +import { ExploreAppCard } from './ExploreAppCard'; |
| 8 | +import { |
| 9 | + getGetRuntimeRoflAppsQueryKey, |
| 10 | + GetRuntimeRoflApps, |
| 11 | +} from '../../nexus/api'; |
| 12 | +import { useInView } from 'react-intersection-observer'; |
| 13 | + |
| 14 | +const pageLimit = 18; |
4 | 15 |
|
5 | 16 | export const Explore: FC = () => { |
| 17 | + const { ref, inView } = useInView(); |
| 18 | + const network = useNetwork('mainnet'); |
| 19 | + |
| 20 | + const { |
| 21 | + data, |
| 22 | + fetchNextPage, |
| 23 | + hasNextPage, |
| 24 | + isFetchingNextPage, |
| 25 | + isLoading, |
| 26 | + isFetched, |
| 27 | + } = useInfiniteQuery({ |
| 28 | + queryKey: [...getGetRuntimeRoflAppsQueryKey(network, 'sapphire')], |
| 29 | + queryFn: async ({ pageParam = 0 }) => { |
| 30 | + const result = await GetRuntimeRoflApps(network, 'sapphire', { |
| 31 | + limit: pageLimit, |
| 32 | + offset: pageParam, |
| 33 | + }); |
| 34 | + return result; |
| 35 | + }, |
| 36 | + initialPageParam: 0, |
| 37 | + getNextPageParam: (lastPage, allPages) => { |
| 38 | + const totalFetched = allPages.length * pageLimit; |
| 39 | + return totalFetched < lastPage.data.total_count |
| 40 | + ? totalFetched |
| 41 | + : undefined; |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (inView && hasNextPage && !isFetchingNextPage) { |
| 47 | + fetchNextPage(); |
| 48 | + } |
| 49 | + }, [fetchNextPage, hasNextPage, isFetchingNextPage, inView]); |
| 50 | + |
| 51 | + const allRoflApps = data?.pages.flatMap((page) => page.data.rofl_apps) || []; |
| 52 | + const isEmpty = isFetched && allRoflApps.length === 0; |
| 53 | + |
6 | 54 | return ( |
7 | 55 | <MainLayout> |
8 | | - <ExploreEmptyState /> |
| 56 | + {isEmpty && <ExploreEmptyState />} |
| 57 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> |
| 58 | + {isLoading && |
| 59 | + Array.from({ length: pageLimit }).map((_, index) => ( |
| 60 | + <Skeleton key={index} className="w-full h-[200px]" /> |
| 61 | + ))} |
| 62 | + |
| 63 | + {allRoflApps.map((app) => ( |
| 64 | + <ExploreAppCard key={app.id} app={app} network={network} /> |
| 65 | + ))} |
| 66 | + |
| 67 | + {isFetchingNextPage && |
| 68 | + Array.from({ length: 3 }).map((_, index) => ( |
| 69 | + <Skeleton key={`next-page-${index}`} className="w-full h-[200px]" /> |
| 70 | + ))} |
| 71 | + </div> |
| 72 | + |
| 73 | + <div ref={ref} className="h-10 w-full" /> |
9 | 74 | </MainLayout> |
10 | 75 | ); |
11 | 76 | }; |
0 commit comments