7
7
TableRow ,
8
8
} from "@/components/ui/table" ;
9
9
import type { Address } from "thirdweb" ;
10
- import { checksumAddress } from "thirdweb/utils" ;
11
10
import { getRoutes } from "../../../utils" ;
12
11
import { ChainlistPagination } from "../client/pagination" ;
13
12
import { RouteListCard } from "./routelist-card" ;
@@ -25,18 +24,15 @@ export type SearchParams = Partial<{
25
24
26
25
// 120 is divisible by 2, 3, and 4 so card layout looks nice
27
26
const DEFAULT_PAGE_SIZE = 120 ;
28
- const DEFAULT_PAGE = 1 ;
29
27
30
28
async function getRoutesToRender ( params : SearchParams ) {
31
29
const filters : Partial < {
32
- limit : number ;
33
- offset : number ;
30
+ originQuery ?: string ;
31
+ destinationQuery ?: string ;
34
32
originChainId ?: number ;
35
33
originTokenAddress ?: Address ;
36
34
destinationChainId ?: number ;
37
35
destinationTokenAddress ?: Address ;
38
- originTextQuery ?: string ;
39
- destinationTextQuery ?: string ;
40
36
} > = { } ;
41
37
42
38
if ( params . type === "origin" || typeof params . type === "undefined" ) {
@@ -45,77 +41,28 @@ async function getRoutesToRender(params: SearchParams) {
45
41
} else if ( Number . isInteger ( Number ( params . query ) ) ) {
46
42
filters . originChainId = Number ( params . query ) ;
47
43
} else if ( params . query ) {
48
- filters . originTextQuery = params . query ;
44
+ filters . originQuery = params . query ;
49
45
}
50
46
} else if ( params . type === "destination" ) {
51
47
if ( params . query ?. startsWith ( "0x" ) ) {
52
48
filters . destinationTokenAddress = params . query as Address ;
53
49
} else if ( Number . isInteger ( Number ( params . query ) ) ) {
54
50
filters . destinationChainId = Number ( params . query ) ;
55
51
} else if ( params . query ) {
56
- filters . destinationTextQuery = params . query ;
52
+ filters . destinationQuery = params . query ;
57
53
}
58
54
}
59
- // Temporary, will update this after the /routes endpoint
60
- let routes = await getRoutes ( { limit : 500_000 } ) ;
61
-
62
- const totalCount = routes . length ;
63
-
64
- if ( filters . originChainId ) {
65
- routes = routes . filter (
66
- ( route ) => route . originToken . chainId === filters . originChainId ,
67
- ) ;
68
- }
69
- if ( filters . originTokenAddress ) {
70
- const originTokenAddress = filters . originTokenAddress ;
71
- routes = routes . filter (
72
- ( route ) =>
73
- checksumAddress ( route . originToken . address ) ===
74
- checksumAddress ( originTokenAddress ) ,
75
- ) ;
76
- }
77
- if ( filters . destinationChainId ) {
78
- routes = routes . filter (
79
- ( route ) => route . destinationToken . chainId === filters . destinationChainId ,
80
- ) ;
81
- }
82
- if ( filters . destinationTokenAddress ) {
83
- const destinationTokenAddress = filters . destinationTokenAddress ;
84
- routes = routes . filter (
85
- ( route ) =>
86
- checksumAddress ( route . destinationToken . address ) ===
87
- checksumAddress ( destinationTokenAddress ) ,
88
- ) ;
89
- }
90
-
91
- if ( filters . originTextQuery ) {
92
- const originTextQuery = filters . originTextQuery . toLowerCase ( ) ;
93
- routes = routes . filter ( ( route ) => {
94
- return (
95
- route . originToken . name . toLowerCase ( ) . includes ( originTextQuery ) ||
96
- route . originToken . symbol . toLowerCase ( ) . includes ( originTextQuery )
97
- ) ;
98
- } ) ;
99
- }
100
-
101
- if ( filters . destinationTextQuery ) {
102
- const destinationTextQuery = filters . destinationTextQuery . toLowerCase ( ) ;
103
- routes = routes . filter ( ( route ) => {
104
- return (
105
- route . destinationToken . name
106
- . toLowerCase ( )
107
- . includes ( destinationTextQuery ) ||
108
- route . destinationToken . symbol
109
- . toLowerCase ( )
110
- . includes ( destinationTextQuery )
111
- ) ;
112
- } ) ;
113
- }
55
+ const routes = await getRoutes ( {
56
+ limit : DEFAULT_PAGE_SIZE ,
57
+ offset : DEFAULT_PAGE_SIZE * ( ( params . page || 1 ) - 1 ) ,
58
+ originQuery : filters . originQuery ,
59
+ destinationQuery : filters . destinationQuery ,
60
+ } ) ;
114
61
115
62
return {
116
- routesToRender : routes ,
117
- totalCount,
118
- filteredCount : routes . length ,
63
+ routesToRender : routes . data ,
64
+ totalCount : routes . meta . totalCount ,
65
+ filteredCount : routes . meta . filteredCount ,
119
66
} ;
120
67
}
121
68
@@ -128,10 +75,9 @@ export async function RoutesData(props: {
128
75
props . searchParams ,
129
76
) ;
130
77
131
- // pagination
132
- const totalPages = Math . ceil ( routesToRender . length / DEFAULT_PAGE_SIZE ) ;
78
+ const totalPages = Math . ceil ( filteredCount / DEFAULT_PAGE_SIZE ) ;
133
79
134
- const activePage = Number ( props . searchParams . page || DEFAULT_PAGE ) ;
80
+ const activePage = Number ( props . searchParams . page || 1 ) ;
135
81
const pageSize = DEFAULT_PAGE_SIZE ;
136
82
const startIndex = ( activePage - 1 ) * pageSize ;
137
83
const endIndex = startIndex + pageSize ;
0 commit comments