Skip to content

Commit

Permalink
middleware: route default home page to explore page (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei authored Feb 4, 2025
1 parent 6c32d0e commit 4c71ae9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Redirects "/" and "/trade" paths to "/trade/:primary/:numeraire"
// Redirects "/" and "/trade" paths to paths defined in the routing middleware.
export const config = {
matcher: ['/', '/trade'],
};

export { tradeMiddleware as middleware } from '@/pages/trade/index.server';
export { routingMiddleware as middleware } from '@/shared/index.server';
23 changes: 0 additions & 23 deletions src/pages/trade/api/middleware.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/pages/trade/index.server.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/shared/api/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NextResponse, NextRequest } from 'next/server';
import { ChainRegistryClient } from '@penumbra-labs/registry';
import { getClientSideEnv } from '@/shared/api/env/getClientSideEnv';
import { assetPatterns } from '@penumbra-zone/types/assets';

export const routingMiddleware = async (request: NextRequest) => {
const { pathname, origin } = request.nextUrl;

if (pathname === '/') {
// Redirect the default homepage to the 'explore' page.
// TODO: Replace this with a path to a fully designed landing page.
return NextResponse.redirect(`${origin}/explore`);
}

// Otherwise, route to the default trading pair on the trading page.
if (pathname === '/trade') {
const { PENUMBRA_CHAIN_ID } = getClientSideEnv();
const chainRegistryClient = new ChainRegistryClient();
const registry = await chainRegistryClient.remote.get(PENUMBRA_CHAIN_ID);
const allAssets = registry
.getAllAssets()
.filter(m => !assetPatterns.delegationToken.matches(m.display))
.toSorted((a, b) => Number(b.priorityScore - a.priorityScore));

const baseAsset = allAssets[0]?.symbol;
const quoteAsset = allAssets[1]?.symbol;
if (!baseAsset || !quoteAsset) {
return NextResponse.redirect(new URL('not-found', request.url));
}

return NextResponse.redirect(new URL(`/trade/${baseAsset}/${quoteAsset}`, request.url));
}

return NextResponse.next();
};
1 change: 1 addition & 0 deletions src/shared/index.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { routingMiddleware } from './api/middleware';
4 changes: 2 additions & 2 deletions src/widgets/header/ui/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { MoonStar, ArrowUpFromDot, Coins } from 'lucide-react';
import { PagePath } from '@/shared/const/pages';

export const HEADER_LINKS = [
{ label: 'Trade', value: PagePath.Trade, icon: ArrowUpFromDot },
{ label: 'Explore', value: PagePath.Explore, icon: Coins },
{ label: 'Inspect', value: PagePath.Inspect, icon: MoonStar },
{ label: 'Portfolio', value: PagePath.Portfolio, icon: Coins },
{ label: 'Trade', value: PagePath.Trade, icon: ArrowUpFromDot },
{ label: 'Inspect', value: PagePath.Inspect, icon: MoonStar },
];

0 comments on commit 4c71ae9

Please sign in to comment.