@@ -8,9 +8,10 @@ import {
8
8
TabPanel ,
9
9
HStack ,
10
10
VStack ,
11
+ Spinner ,
11
12
} from "@chakra-ui/react" ;
12
13
import { useRouter } from "next/router" ;
13
- import { useState } from "react" ;
14
+ import { useMemo } from "react" ;
14
15
import { defineMessages , useIntl } from "react-intl" ;
15
16
16
17
import { AccountAssets } from "@/components/AccountAssets/AccountAssets" ;
@@ -25,7 +26,6 @@ import lionfishLock from "@/images/lionfish-lock.svg";
25
26
import MainLayout from "@/layouts/MainLayout" ;
26
27
import { WithExplanatorySidebar } from "@/layouts/WithExplanatorySidebar" ;
27
28
import { trpcReact } from "@/providers/TRPCProvider" ;
28
- import { PillButton } from "@/ui/PillButton/PillButton" ;
29
29
import { asQueryString } from "@/utils/parseRouteQuery" ;
30
30
31
31
const messages = defineMessages ( {
@@ -74,8 +74,6 @@ function AccountOverviewContent({ accountName }: { accountName: string }) {
74
74
const initialTabIndex = useInitialTabIndex ( ) ;
75
75
const { formatMessage } = useIntl ( ) ;
76
76
77
- const [ cursor , setCursor ] = useState ( 0 ) ;
78
-
79
77
const { data : accountData } = trpcReact . getAccount . useQuery ( {
80
78
name : accountName ,
81
79
} ) ;
@@ -84,13 +82,23 @@ function AccountOverviewContent({ accountName }: { accountName: string }) {
84
82
data : transactionsData ,
85
83
isLoading,
86
84
isError,
87
- } = trpcReact . getTransactions . useQuery ( {
88
- accountName,
89
- cursor,
90
- limit : 10 ,
91
- } ) ;
85
+ fetchNextPage,
86
+ hasNextPage,
87
+ isFetchingNextPage,
88
+ } = trpcReact . getTransactions . useInfiniteQuery (
89
+ {
90
+ accountName,
91
+ limit : 10 ,
92
+ } ,
93
+ {
94
+ getNextPageParam : ( lastPage ) => lastPage . nextCursor ,
95
+ initialCursor : 0 ,
96
+ } ,
97
+ ) ;
92
98
93
- const shouldShowPagination = cursor !== 0 || transactionsData ?. hasNextPage ;
99
+ const notes = useMemo ( ( ) => {
100
+ return transactionsData ?. pages . flatMap ( ( page ) => page . transactions ) ?? [ ] ;
101
+ } , [ transactionsData ?. pages ] ) ;
94
102
95
103
if ( ! accountData ) {
96
104
// @todo : Error handling
@@ -132,29 +140,16 @@ function AccountOverviewContent({ accountName }: { accountName: string }) {
132
140
asTransactions
133
141
isLoading = { isLoading }
134
142
isError = { isError }
135
- notes = { transactionsData ?. transactions ?? [ ] }
143
+ notes = { notes }
136
144
heading = { formatMessage ( messages . accountOverview ) }
145
+ onEndReached = { ( ) => {
146
+ if ( ! hasNextPage || isFetchingNextPage ) return ;
147
+ fetchNextPage ( ) ;
148
+ } }
137
149
/>
138
- { shouldShowPagination && (
139
- < HStack flex = { 1 } justifyContent = "center" >
140
- < PillButton
141
- isDisabled = { ! transactionsData || cursor <= 0 }
142
- onClick = { ( ) => {
143
- setCursor ( ( c ) => Math . max ( c - 10 , 0 ) ) ;
144
- } }
145
- >
146
- { formatMessage ( messages . previousButton ) }
147
- </ PillButton >
148
- < PillButton
149
- isDisabled = { ! transactionsData ?. hasNextPage }
150
- onClick = { ( ) => {
151
- setCursor ( ( c ) => c + 10 ) ;
152
- } }
153
- >
154
- { formatMessage ( messages . nextButton ) }
155
- </ PillButton >
156
- </ HStack >
157
- ) }
150
+ < HStack alignItems = "center" justifyContent = "center" h = "50px" >
151
+ { isFetchingNextPage && < Spinner opacity = "0.5" /> }
152
+ </ HStack >
158
153
</ TabPanel >
159
154
< TabPanel p = { 0 } >
160
155
< WithExplanatorySidebar
0 commit comments