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