-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3574952
commit 24eaddd
Showing
9 changed files
with
229 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import { fetchTokenHoldersOfAddress } from '@/app/user/Balances/actions' | ||
import { useInfiniteQuery } from '@tanstack/react-query' | ||
import { Address } from 'viem' | ||
import { NextPageParams, ServerResponseV2, TokenHoldersResponse } from '@/app/user/Balances/types' | ||
import { NextPageParams } from '@/app/user/Balances/types' | ||
import { usePagination } from '@/shared/hooks/usePagination' | ||
import { usePaginationUi } from '@/shared/hooks/usePaginationUi' | ||
|
||
export const useFetchTokenHolders = (address: Address) => { | ||
return useInfiniteQuery<ServerResponseV2<TokenHoldersResponse>, Error>({ | ||
queryKey: [`tokenHolders${address}`, address], | ||
const query = usePagination({ | ||
queryKey: ['tokenHolders'], | ||
queryFn: ({ pageParam }) => fetchTokenHoldersOfAddress(address, pageParam as NextPageParams), | ||
getNextPageParam: lastPage => lastPage.next_page_params, | ||
initialPageParam: null, | ||
queryFn: async ({ pageParam }) => fetchTokenHoldersOfAddress(address, pageParam as NextPageParams), | ||
getNextPageParam: lastPage => { | ||
return lastPage.next_page_params | ||
}, | ||
resultsPerTablePage: 10, | ||
hasMorePagesProperty: 'next_page_params', | ||
}) | ||
|
||
const ui = usePaginationUi(query) | ||
|
||
return { ...query, ...ui } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ export type ButtonVariants = | |
| 'outlined' | ||
| 'white' | ||
| 'sidebar-active' | ||
| 'pagination' | ||
| 'pagination-active' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { InfiniteData, useInfiniteQuery, UseInfiniteQueryResult } from '@tanstack/react-query' | ||
import { useState, useCallback, useMemo, useEffect } from 'react' | ||
|
||
interface PaginatedResponse<T> { | ||
items: T[] | ||
[key: string]: any | ||
} | ||
|
||
interface UsePaginatedQueryOptions<T> { | ||
queryKey: string[] | ||
queryFn: (pageParam: any) => Promise<PaginatedResponse<T>> | ||
getNextPageParam: (lastPage: PaginatedResponse<T>) => any | ||
initialPageParam: any | ||
resultsPerTablePage: number | ||
hasMorePagesProperty: keyof PaginatedResponse<T> | ||
} | ||
|
||
export interface UsePaginatedQueryResult<T> | ||
extends Omit<UseInfiniteQueryResult<InfiniteData<PaginatedResponse<T>>>, 'data'> { | ||
currentResults: T[] | ||
tablePage: number | ||
totalPages: number | ||
nextTablePage: () => void | ||
previousTablePage: () => void | ||
isFirstFetch: boolean | ||
hasMorePages: boolean | ||
} | ||
|
||
export function usePagination<T>({ | ||
queryKey, | ||
queryFn, | ||
getNextPageParam, | ||
initialPageParam, | ||
resultsPerTablePage, | ||
hasMorePagesProperty, | ||
}: UsePaginatedQueryOptions<T>): UsePaginatedQueryResult<T> { | ||
const [tablePage, setTablePage] = useState(0) | ||
const [isFirstFetch, setIsFirstFetch] = useState(true) | ||
|
||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, ...restQueryResult } = useInfiniteQuery< | ||
PaginatedResponse<T>, | ||
Error | ||
>({ | ||
queryKey, | ||
queryFn, | ||
getNextPageParam, | ||
initialPageParam, | ||
refetchOnWindowFocus: false, | ||
}) | ||
|
||
const allItems = useMemo(() => { | ||
return data?.pages.flatMap(page => page.items) || [] | ||
}, [data]) | ||
|
||
const totalItems = allItems.length | ||
const totalPages = Math.ceil(totalItems / resultsPerTablePage) | ||
|
||
const currentResults = useMemo(() => { | ||
const start = tablePage * resultsPerTablePage | ||
const end = start + resultsPerTablePage | ||
return allItems.slice(start, end) | ||
}, [allItems, tablePage, resultsPerTablePage]) | ||
|
||
const nextTablePage = useCallback(() => { | ||
if (tablePage < totalPages - 1) { | ||
setTablePage(prev => prev + 1) | ||
} | ||
}, [tablePage, totalPages]) | ||
|
||
const previousTablePage = useCallback(() => { | ||
if (tablePage > 0) { | ||
setTablePage(prev => prev - 1) | ||
} | ||
}, [tablePage]) | ||
|
||
const hasMorePages = useMemo(() => { | ||
if (!data || data.pages.length === 0) return true | ||
const lastPage = data.pages[data.pages.length - 1] | ||
return lastPage[hasMorePagesProperty] !== null | ||
}, [data, hasMorePagesProperty]) | ||
|
||
useEffect(() => { | ||
// Check if we need to fetch the next page | ||
const currentItemIndex = (tablePage + 1) * resultsPerTablePage | ||
if (currentItemIndex >= totalItems && hasMorePages && !isFetchingNextPage) { | ||
fetchNextPage() | ||
} | ||
}, [tablePage, resultsPerTablePage, totalItems, hasMorePages, isFetchingNextPage, fetchNextPage]) | ||
|
||
useEffect(() => { | ||
// Update isFirstFetch | ||
if (isFirstFetch && data && data.pages.length > 0) { | ||
setIsFirstFetch(false) | ||
} | ||
}, [data, isFirstFetch]) | ||
|
||
return { | ||
currentResults, | ||
totalPages, | ||
tablePage, | ||
nextTablePage, | ||
previousTablePage, | ||
isFirstFetch, | ||
hasMorePages, | ||
fetchNextPage, | ||
hasNextPage, | ||
isFetchingNextPage, | ||
...restQueryResult, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ReactNode, useMemo } from 'react' | ||
import { UsePaginatedQueryResult } from '@/shared/hooks/usePagination' | ||
import { Button } from '@/components/Button' | ||
import { ChevronLeft, ChevronRight } from 'lucide-react' | ||
|
||
interface UseSimplePaginationResult<T> { | ||
paginationElement: ReactNode | ||
currentResults: T[] | ||
} | ||
|
||
export function usePaginationUi<T>( | ||
paginationResult: UsePaginatedQueryResult<T>, | ||
): UseSimplePaginationResult<T> { | ||
const { currentResults, totalPages, tablePage, nextTablePage, previousTablePage, isLoading } = | ||
paginationResult | ||
|
||
const paginationElement = useMemo(() => { | ||
const getPageNumbers = () => { | ||
const maxVisiblePages = 5 | ||
const pages = [] | ||
const start = Math.max(0, Math.min(tablePage - 2, totalPages - maxVisiblePages)) | ||
const end = Math.min(start + maxVisiblePages, totalPages) | ||
|
||
for (let i = start; i < end; i++) { | ||
pages.push(i) | ||
} | ||
|
||
return pages | ||
} | ||
|
||
return ( | ||
<div className="flex gap-x-1 items-center justify-center"> | ||
<PaginationButton | ||
text={<ChevronLeft />} | ||
onClick={previousTablePage} | ||
disabled={tablePage === 0 || isLoading} | ||
/> | ||
{getPageNumbers().map(pageNumber => ( | ||
<PaginationButton | ||
key={pageNumber} | ||
onClick={() => | ||
tablePage !== pageNumber && (pageNumber > tablePage ? nextTablePage() : previousTablePage()) | ||
} | ||
disabled={isLoading} | ||
text={pageNumber + 1} | ||
isActive={pageNumber === tablePage} | ||
/> | ||
))} | ||
<PaginationButton | ||
text={<ChevronRight />} | ||
onClick={nextTablePage} | ||
disabled={tablePage === totalPages - 1 || isLoading} | ||
/> | ||
</div> | ||
) | ||
}, [tablePage, totalPages, isLoading, nextTablePage, previousTablePage]) | ||
|
||
return { | ||
paginationElement, | ||
currentResults, | ||
} | ||
} | ||
|
||
const PaginationButton = ({ | ||
text, | ||
onClick, | ||
disabled, | ||
isActive, | ||
}: { | ||
text: ReactNode | ||
onClick: () => void | ||
disabled?: boolean | ||
isActive?: boolean | ||
}) => ( | ||
<Button onClick={onClick} disabled={disabled} variant={isActive ? 'pagination-active' : 'pagination'}> | ||
{text} | ||
</Button> | ||
) |