Skip to content

Commit 177ffec

Browse files
authored
Show both Fastest and cheapest as top routes (#3567)
* Show both Fastest and cheapest as top routes Signed-off-by: Emre Bogazliyanlioglu <emre@wormholelabs.xyz> * Fix the issue with non-badge route selected when there are 2 routes Signed-off-by: Emre Bogazliyanlioglu <emre@wormholelabs.xyz> --------- Signed-off-by: Emre Bogazliyanlioglu <emre@wormholelabs.xyz>
1 parent 18cf34b commit 177ffec

File tree

1 file changed

+78
-20
lines changed
  • wormhole-connect/src/views/v2/Bridge/Routes

1 file changed

+78
-20
lines changed

wormhole-connect/src/views/v2/Bridge/Routes/index.tsx

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ const Routes = ({ ...props }: Props) => {
4040
return props.routes.filter((rs) => props.quotes[rs] !== undefined);
4141
}, [props.routes, props.quotes]);
4242

43-
const renderRoutes = useMemo(() => {
44-
if (showAll) {
45-
return routes;
46-
}
47-
48-
const selectedRoute = routes.find((route) => route === props.selectedRoute);
49-
50-
return selectedRoute ? [selectedRoute] : routes.slice(0, 1);
51-
}, [showAll, routes, props.selectedRoute]);
52-
5343
const fastestRoute = useMemo(() => {
5444
return routes.reduce(
5545
(fastest, route) => {
@@ -88,6 +78,83 @@ const Routes = ({ ...props }: Props) => {
8878
);
8979
}, [routes, props.quotes]);
9080

81+
const renderRoutes = useMemo(() => {
82+
if (showAll) {
83+
return routes;
84+
}
85+
86+
const selectedRoute = routes.find((route) => route === props.selectedRoute);
87+
88+
// Special case when we have a selected route
89+
if (selectedRoute) {
90+
const topRoutes: Array<string> = [];
91+
// if the selected route is the fastest, add it first and the cheapest route below
92+
if (selectedRoute === fastestRoute.name) {
93+
topRoutes.push(selectedRoute);
94+
if (cheapestRoute.name && cheapestRoute.name !== selectedRoute) {
95+
topRoutes.push(cheapestRoute.name);
96+
}
97+
} else if (selectedRoute === cheapestRoute.name) {
98+
// if the selected route is the cheapest add the fastest route first and selected below
99+
if (fastestRoute.name && fastestRoute.name !== selectedRoute) {
100+
topRoutes.push(fastestRoute.name);
101+
}
102+
topRoutes.push(selectedRoute);
103+
} else {
104+
// if the selected route is neither fastest nor cheapest, we add it at the top
105+
topRoutes.push(selectedRoute);
106+
if (routes.length > 2) {
107+
// if we have more than 2 routes in total, meaning there are at least two more routes to show,
108+
// then we add one of the fastest or cheapest routes below the selected route
109+
if (fastestRoute.name) {
110+
// Add the fastest route if it we have one
111+
topRoutes.push(fastestRoute.name);
112+
} else if (cheapestRoute.name) {
113+
// otherwise add the cheapest route
114+
topRoutes.push(cheapestRoute.name);
115+
}
116+
}
117+
}
118+
return topRoutes;
119+
}
120+
121+
// If we have fastest and cheapest routes, we show them both at the top
122+
if (!!fastestRoute.name && !!cheapestRoute.name) {
123+
return routes.slice(0, 2);
124+
}
125+
126+
// Otherwise we might have a cheapest route but none qualifying as fastest,
127+
// so we show the first route at the top
128+
return routes.slice(0, 1);
129+
}, [showAll, routes, fastestRoute, cheapestRoute, props.selectedRoute]);
130+
131+
const hideShowToggle = useMemo(() => {
132+
// If we have less than 2 routes; or there are 2 but those are the fastest and cheapest routes,
133+
// we do not show the toggle to view other routes
134+
if (
135+
routes.length < 2 ||
136+
(routes.length === 2 && !!fastestRoute.name && !!cheapestRoute.name)
137+
) {
138+
return null;
139+
}
140+
141+
return (
142+
<Link
143+
className={classes.otherRoutesToggle}
144+
data-testid="other-routes-toggle"
145+
onClick={() => setShowAll((prev) => !prev)}
146+
>
147+
{showAll ? 'Hide other routes' : 'View other routes'}
148+
</Link>
149+
);
150+
}, [
151+
cheapestRoute.name,
152+
classes.otherRoutesToggle,
153+
fastestRoute.name,
154+
routes.length,
155+
showAll,
156+
]);
157+
91158
return (
92159
<>
93160
{props.isLoading || renderRoutes.length > 0 ? (
@@ -137,16 +204,7 @@ const Routes = ({ ...props }: Props) => {
137204
);
138205
})
139206
)}
140-
141-
{routes.length > 1 && (
142-
<Link
143-
className={classes.otherRoutesToggle}
144-
data-testid="other-routes-toggle"
145-
onClick={() => setShowAll((prev) => !prev)}
146-
>
147-
{showAll ? 'Hide other routes' : 'View other routes'}
148-
</Link>
149-
)}
207+
{hideShowToggle}
150208
</>
151209
);
152210
};

0 commit comments

Comments
 (0)