Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware: route default home page to explore page #346

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 },
];