1- import { useCallback , useEffect } from 'react' ;
1+ import { useCallback , useEffect , useState } from 'react' ;
22import { useErrors } from 'apps/web/contexts/Errors' ;
33import { useQuery , useQueryClient } from '@tanstack/react-query' ;
44import { useAccount , useChainId } from 'wagmi' ;
@@ -11,6 +11,11 @@ export function useNameList() {
1111 const chainId = useChainId ( ) ;
1212 const { logError } = useErrors ( ) ;
1313
14+ // Pagination state - need to maintain a stack of page tokens for proper back navigation
15+ const [ currentPage , setCurrentPage ] = useState < string | null > ( null ) ;
16+ const [ pageStack , setPageStack ] = useState < ( string | null ) [ ] > ( [ null ] ) ; // Stack of page tokens for history
17+ const [ pageNumber , setPageNumber ] = useState < number > ( 1 ) ;
18+
1419 const network = chainId === 8453 ? 'base-mainnet' : 'base-sepolia' ;
1520
1621 const {
@@ -19,12 +24,15 @@ export function useNameList() {
1924 error,
2025 refetch,
2126 } = useQuery < ManagedAddressesResponse > ( {
22- queryKey : [ 'usernames' , address , network ] ,
27+ queryKey : [ 'usernames' , address , network , currentPage ] ,
2328 queryFn : async ( ) : Promise < ManagedAddressesResponse > => {
2429 try {
25- const response = await fetch (
26- `/api/basenames/getUsernames?address=${ address } &network=${ network } ` ,
27- ) ;
30+ let url = `/api/basenames/getUsernames?address=${ address } &network=${ network } ` ;
31+ if ( currentPage ) {
32+ url += `&page=${ currentPage } ` ;
33+ }
34+
35+ const response = await fetch ( url ) ;
2836 if ( ! response . ok ) {
2937 throw new Error ( `Failed to fetch usernames: ${ response . statusText } ` ) ;
3038 }
@@ -37,7 +45,61 @@ export function useNameList() {
3745 enabled : ! ! address ,
3846 } ) ;
3947
40- return { namesData, isLoading, error, refetch } ;
48+ // Navigation functions
49+ const goToNextPage = useCallback ( ( ) => {
50+ if ( namesData ?. has_more && namesData ?. next_page ) {
51+ // Push current page to history stack and move to next page
52+ setPageStack ( ( prev ) => [ ...prev , currentPage ] ) ;
53+ setCurrentPage ( namesData . next_page ) ;
54+ setPageNumber ( ( prev ) => prev + 1 ) ;
55+ }
56+ } , [ namesData ?. has_more , namesData ?. next_page , currentPage ] ) ;
57+
58+ const goToPreviousPage = useCallback ( ( ) => {
59+ if ( pageStack . length > 1 ) {
60+ // Pop from stack to get the page token to go back to
61+ const newStack = [ ...pageStack ] ;
62+ const targetPageToken = newStack . pop ( ) ; // This is the page we want to go back to
63+
64+ if ( targetPageToken !== undefined ) {
65+ setPageStack ( newStack ) ;
66+ setCurrentPage ( targetPageToken ) ;
67+ setPageNumber ( ( prev ) => prev - 1 ) ;
68+ }
69+ }
70+ } , [ pageStack ] ) ;
71+
72+ const resetPagination = useCallback ( ( ) => {
73+ setCurrentPage ( null ) ;
74+ setPageStack ( [ null ] ) ;
75+ setPageNumber ( 1 ) ;
76+ } , [ ] ) ;
77+
78+ // Pagination info
79+ const totalCount = namesData ?. total_count ?? 0 ;
80+ const hasPrevious = pageStack . length > 1 ;
81+ const hasNext = namesData ?. has_more ?? false ;
82+ const currentPageNumber = pageNumber ;
83+
84+ // Reset pagination when component mounts or address/network changes
85+ useEffect ( ( ) => {
86+ resetPagination ( ) ;
87+ } , [ address , network , resetPagination ] ) ;
88+
89+ return {
90+ namesData,
91+ isLoading,
92+ error,
93+ refetch,
94+ // Pagination
95+ goToNextPage,
96+ goToPreviousPage,
97+ resetPagination,
98+ hasPrevious,
99+ hasNext,
100+ totalCount,
101+ currentPageNumber,
102+ } ;
41103}
42104
43105export function useRemoveNameFromUI ( domain : Basename ) {
0 commit comments