|
| 1 | +import { DETAIL_TABLE_LENGTH, TABLE_REFETCH_INTERVAL } from '@/config'; |
| 2 | +import { execute } from '@/graphql/poco/execute'; |
| 3 | +import { useQuery } from '@tanstack/react-query'; |
| 4 | +import { useEffect } from 'react'; |
| 5 | +import { DataTable } from '@/components/DataTable'; |
| 6 | +import { PaginatedNavigation } from '@/components/PaginatedNavigation'; |
| 7 | +import { usePageParam } from '@/hooks/usePageParam'; |
| 8 | +import { ErrorAlert } from '@/modules/ErrorAlert'; |
| 9 | +import { columns } from '@/modules/deals/dealsTable/columns'; |
| 10 | +import useUserStore from '@/stores/useUser.store'; |
| 11 | +import { createPlaceholderDataFnForQueryKey } from '@/utils/createPlaceholderDataFnForQueryKey'; |
| 12 | +import { getAdditionalPages } from '@/utils/format'; |
| 13 | +import { datasetBulkDealsQuery } from './datasetBulkDealsQuery'; |
| 14 | + |
| 15 | +function useDatasetBulkDealsData({ |
| 16 | + datasetId, |
| 17 | + currentPage, |
| 18 | +}: { |
| 19 | + datasetId: string; |
| 20 | + currentPage: number; |
| 21 | +}) { |
| 22 | + const { chainId } = useUserStore(); |
| 23 | + const skip = currentPage * DETAIL_TABLE_LENGTH; |
| 24 | + const nextSkip = skip + DETAIL_TABLE_LENGTH; |
| 25 | + const nextNextSkip = skip + 2 * DETAIL_TABLE_LENGTH; |
| 26 | + |
| 27 | + const queryKey = [chainId, 'dataset', 'bulkDeals', datasetId, currentPage]; |
| 28 | + const { data, isLoading, isRefetching, isError, errorUpdateCount } = useQuery( |
| 29 | + { |
| 30 | + queryKey, |
| 31 | + queryFn: () => |
| 32 | + execute(datasetBulkDealsQuery, chainId, { |
| 33 | + length: DETAIL_TABLE_LENGTH, |
| 34 | + skip, |
| 35 | + nextSkip, |
| 36 | + nextNextSkip, |
| 37 | + datasetId, |
| 38 | + }), |
| 39 | + refetchInterval: TABLE_REFETCH_INTERVAL, |
| 40 | + placeholderData: createPlaceholderDataFnForQueryKey(queryKey), |
| 41 | + } |
| 42 | + ); |
| 43 | + |
| 44 | + const bulkDeals = data?.bulkSliceice?.map((slice) => slice.task?.deal) ?? []; |
| 45 | + // 0 = only current, 1 = next, 2 = next+1 |
| 46 | + const additionalPages = getAdditionalPages( |
| 47 | + Boolean(data?.bulkSliceiceHasNext?.length), |
| 48 | + Boolean(data?.bulkSliceiceHasNextNext?.length) |
| 49 | + ); |
| 50 | + |
| 51 | + const formattedBulkDeal = |
| 52 | + bulkDeals |
| 53 | + ?.filter((bulkDeal) => bulkDeal !== undefined) |
| 54 | + .map((bulkDeal) => ({ |
| 55 | + ...bulkDeal, |
| 56 | + destination: `/bulkDeal/${bulkDeal!.dealid}`, |
| 57 | + })) ?? []; |
| 58 | + |
| 59 | + return { |
| 60 | + data: formattedBulkDeal, |
| 61 | + isLoading, |
| 62 | + isRefetching, |
| 63 | + isError, |
| 64 | + additionalPages, |
| 65 | + hasPastError: isError || errorUpdateCount > 0, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export function DatasetBulkDealsTable({ |
| 70 | + datasetId, |
| 71 | + setLoading, |
| 72 | + setOutdated, |
| 73 | +}: { |
| 74 | + datasetId: string; |
| 75 | + setLoading: (loading: boolean) => void; |
| 76 | + setOutdated: (outdated: boolean) => void; |
| 77 | +}) { |
| 78 | + const [currentPage, setCurrentPage] = usePageParam('datasetBulkDealsPage'); |
| 79 | + const { |
| 80 | + data: bulkDeals, |
| 81 | + isError, |
| 82 | + isLoading, |
| 83 | + isRefetching, |
| 84 | + additionalPages, |
| 85 | + hasPastError, |
| 86 | + } = useDatasetBulkDealsData({ datasetId, currentPage: currentPage - 1 }); |
| 87 | + |
| 88 | + useEffect( |
| 89 | + () => setLoading(isLoading || isRefetching), |
| 90 | + [isLoading, isRefetching, setLoading] |
| 91 | + ); |
| 92 | + useEffect( |
| 93 | + () => setOutdated(bulkDeals.length > 0 && isError), |
| 94 | + [bulkDeals.length, isError, setOutdated] |
| 95 | + ); |
| 96 | + |
| 97 | + const filteredColumns = columns.filter((col) => col.accessorKey !== 'dealid'); |
| 98 | + |
| 99 | + return ( |
| 100 | + <div className="space-y-6"> |
| 101 | + {hasPastError && !bulkDeals.length ? ( |
| 102 | + <ErrorAlert message="An error occurred during dataset bulkDeals loading." /> |
| 103 | + ) : ( |
| 104 | + <DataTable |
| 105 | + columns={filteredColumns} |
| 106 | + data={bulkDeals} |
| 107 | + tableLength={DETAIL_TABLE_LENGTH} |
| 108 | + /> |
| 109 | + )} |
| 110 | + <PaginatedNavigation |
| 111 | + currentPage={currentPage} |
| 112 | + totalPages={currentPage + additionalPages} |
| 113 | + onPageChange={setCurrentPage} |
| 114 | + /> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +} |
0 commit comments