|
| 1 | +import { TABLE_LENGTH, TABLE_REFETCH_INTERVAL } from '@/config'; |
| 2 | +import { execute } from '@/graphql/execute'; |
| 3 | +import { useQuery } from '@tanstack/react-query'; |
1 | 4 | import { createFileRoute } from '@tanstack/react-router'; |
| 5 | +import { Box, LoaderCircle, Terminal } from 'lucide-react'; |
| 6 | +import { useState } from 'react'; |
| 7 | +import { DataTable } from '@/components/DataTable'; |
| 8 | +import { PaginatedNavigation } from '@/components/PaginatedNavigation'; |
| 9 | +import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; |
| 10 | +import { SearcherBar } from '@/modules/SearcherBar'; |
| 11 | +import { dealsQuery } from '@/modules/deals/dealsQuery'; |
| 12 | +import { columns } from '@/modules/deals/dealsTable/columns'; |
| 13 | +import { nextDealsQuery } from '@/modules/deals/nextDealsQuery'; |
| 14 | +import useUserStore from '@/stores/useUser.store'; |
2 | 15 |
|
3 | 16 | export const Route = createFileRoute('/$chainSlug/_layout/deals')({ |
4 | | - component: RouteComponent, |
| 17 | + component: DealsRoute, |
5 | 18 | }); |
6 | 19 |
|
7 | | -function RouteComponent() { |
8 | | - return <div>Hello "/deals"!</div>; |
| 20 | +function useDealsData(currentPage: number) { |
| 21 | + const { chainId } = useUserStore(); |
| 22 | + const skip = currentPage * TABLE_LENGTH; |
| 23 | + |
| 24 | + const { data, isLoading, isRefetching, isError, errorUpdateCount } = useQuery( |
| 25 | + { |
| 26 | + queryKey: [chainId, 'deals', currentPage], |
| 27 | + queryFn: () => |
| 28 | + execute(dealsQuery, chainId, { length: TABLE_LENGTH, skip }), |
| 29 | + refetchInterval: TABLE_REFETCH_INTERVAL, |
| 30 | + } |
| 31 | + ); |
| 32 | + |
| 33 | + const { data: nextData } = useQuery({ |
| 34 | + queryKey: [chainId, 'deals-next', currentPage], |
| 35 | + queryFn: () => |
| 36 | + execute(nextDealsQuery, chainId, { |
| 37 | + length: TABLE_LENGTH * 2, |
| 38 | + skip: (currentPage + 1) * TABLE_LENGTH, |
| 39 | + }), |
| 40 | + refetchInterval: TABLE_REFETCH_INTERVAL, |
| 41 | + }); |
| 42 | + |
| 43 | + const nextDeals = nextData?.deals ?? []; |
| 44 | + |
| 45 | + const additionalPages = Math.ceil(nextDeals.length / TABLE_LENGTH); |
| 46 | + |
| 47 | + const formattedData = |
| 48 | + data?.deals.map((deal) => ({ |
| 49 | + ...deal, |
| 50 | + destination: `/deal/${deal.dealid}`, |
| 51 | + })) ?? []; |
| 52 | + |
| 53 | + return { |
| 54 | + data: formattedData, |
| 55 | + isLoading, |
| 56 | + isRefetching, |
| 57 | + isError: isError, |
| 58 | + hasPastError: isError || errorUpdateCount > 0, |
| 59 | + additionalPages, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function DealsRoute() { |
| 64 | + const [currentPage, setCurrentPage] = useState(0); |
| 65 | + const { |
| 66 | + data, |
| 67 | + isLoading, |
| 68 | + isRefetching, |
| 69 | + isError, |
| 70 | + hasPastError, |
| 71 | + additionalPages, |
| 72 | + } = useDealsData(currentPage); |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className="mt-8 grid gap-6"> |
| 76 | + <SearcherBar className="py-10" /> |
| 77 | + |
| 78 | + <h1 className="flex items-center gap-2 font-sans text-2xl font-extrabold"> |
| 79 | + <Box size="20" /> |
| 80 | + Deals |
| 81 | + {data.length > 0 && isError && ( |
| 82 | + <span className="text-muted-foreground text-sm font-light"> |
| 83 | + (outdated) |
| 84 | + </span> |
| 85 | + )} |
| 86 | + {(isLoading || isRefetching) && ( |
| 87 | + <LoaderCircle className="animate-spin" /> |
| 88 | + )} |
| 89 | + </h1> |
| 90 | + {hasPastError && !data.length ? ( |
| 91 | + <Alert variant="destructive" className="mx-auto w-fit text-left"> |
| 92 | + <Terminal className="h-4 w-4" /> |
| 93 | + <AlertTitle>Error</AlertTitle> |
| 94 | + <AlertDescription> |
| 95 | + A error occurred during deals loading. |
| 96 | + </AlertDescription> |
| 97 | + </Alert> |
| 98 | + ) : ( |
| 99 | + <DataTable |
| 100 | + columns={columns} |
| 101 | + data={data} |
| 102 | + tableLength={TABLE_LENGTH} |
| 103 | + isLoading={isLoading || isRefetching} |
| 104 | + /> |
| 105 | + )} |
| 106 | + <PaginatedNavigation |
| 107 | + currentPage={currentPage + 1} |
| 108 | + totalPages={currentPage + 1 + additionalPages} |
| 109 | + onPageChange={(newPage) => setCurrentPage(newPage - 1)} |
| 110 | + /> |
| 111 | + </div> |
| 112 | + ); |
9 | 113 | } |
0 commit comments