77 TableRow ,
88} from "@/components/ui/table" ;
99import type { Address } from "thirdweb" ;
10- import { checksumAddress } from "thirdweb/utils" ;
1110import { getRoutes } from "../../../utils" ;
1211import { ChainlistPagination } from "../client/pagination" ;
1312import { RouteListCard } from "./routelist-card" ;
@@ -25,18 +24,15 @@ export type SearchParams = Partial<{
2524
2625// 120 is divisible by 2, 3, and 4 so card layout looks nice
2726const DEFAULT_PAGE_SIZE = 120 ;
28- const DEFAULT_PAGE = 1 ;
2927
3028async function getRoutesToRender ( params : SearchParams ) {
3129 const filters : Partial < {
32- limit : number ;
33- offset : number ;
30+ originQuery ?: string ;
31+ destinationQuery ?: string ;
3432 originChainId ?: number ;
3533 originTokenAddress ?: Address ;
3634 destinationChainId ?: number ;
3735 destinationTokenAddress ?: Address ;
38- originTextQuery ?: string ;
39- destinationTextQuery ?: string ;
4036 } > = { } ;
4137
4238 if ( params . type === "origin" || typeof params . type === "undefined" ) {
@@ -45,77 +41,28 @@ async function getRoutesToRender(params: SearchParams) {
4541 } else if ( Number . isInteger ( Number ( params . query ) ) ) {
4642 filters . originChainId = Number ( params . query ) ;
4743 } else if ( params . query ) {
48- filters . originTextQuery = params . query ;
44+ filters . originQuery = params . query ;
4945 }
5046 } else if ( params . type === "destination" ) {
5147 if ( params . query ?. startsWith ( "0x" ) ) {
5248 filters . destinationTokenAddress = params . query as Address ;
5349 } else if ( Number . isInteger ( Number ( params . query ) ) ) {
5450 filters . destinationChainId = Number ( params . query ) ;
5551 } else if ( params . query ) {
56- filters . destinationTextQuery = params . query ;
52+ filters . destinationQuery = params . query ;
5753 }
5854 }
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+ } ) ;
11461
11562 return {
116- routesToRender : routes ,
117- totalCount,
118- filteredCount : routes . length ,
63+ routesToRender : routes . data ,
64+ totalCount : routes . meta . totalCount ,
65+ filteredCount : routes . meta . filteredCount ,
11966 } ;
12067}
12168
@@ -128,10 +75,9 @@ export async function RoutesData(props: {
12875 props . searchParams ,
12976 ) ;
13077
131- // pagination
132- const totalPages = Math . ceil ( routesToRender . length / DEFAULT_PAGE_SIZE ) ;
78+ const totalPages = Math . ceil ( filteredCount / DEFAULT_PAGE_SIZE ) ;
13379
134- const activePage = Number ( props . searchParams . page || DEFAULT_PAGE ) ;
80+ const activePage = Number ( props . searchParams . page || 1 ) ;
13581 const pageSize = DEFAULT_PAGE_SIZE ;
13682 const startIndex = ( activePage - 1 ) * pageSize ;
13783 const endIndex = startIndex + pageSize ;
0 commit comments