Skip to content

Commit 5fa416e

Browse files
karooolisfrolic
andauthored
feat(explorer): resolve paths using chain ID or name (#3735)
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
1 parent 8bd459b commit 5fa416e

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

.changeset/nine-pugs-speak.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@latticexyz/explorer": patch
3+
---
4+
5+
Explorer will now automatically resolve and redirect deep links using a chain ID to their corresponding chain name, e.g.
6+
7+
https://explorer.mud.dev/690/worlds/0x2d70F1eFFbFD865764CAF19BE2A01a72F3CE774f

packages/explorer/src/app/(explorer)/[chainName]/layout.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

3-
import { notFound } from "next/navigation";
4-
import { isValidChainName } from "../../../common";
3+
import { notFound, redirect, usePathname, useSearchParams } from "next/navigation";
4+
import { chainIdToName, isValidChainId, isValidChainName } from "../../../common";
55
import { Providers } from "./Providers";
66

77
type Props = {
@@ -11,8 +11,22 @@ type Props = {
1111
children: React.ReactNode;
1212
};
1313

14-
export default function ChainLayout({ params: { chainName }, children }: Props) {
15-
if (!isValidChainName(chainName)) {
14+
export default function ChainLayout({ params: { chainName: chainIdOrName }, children }: Props) {
15+
const pathname = usePathname();
16+
const searchParams = useSearchParams();
17+
18+
if (!isValidChainName(chainIdOrName)) {
19+
const chainId = Number(chainIdOrName);
20+
if (isValidChainId(chainId)) {
21+
const chainName = chainIdToName[chainId];
22+
const newPathname = pathname.replace(chainIdOrName, chainName);
23+
24+
if (pathname !== newPathname) {
25+
const search = searchParams.toString();
26+
const redirectUrl = search ? `${newPathname}?${search}` : `${newPathname}`;
27+
return redirect(redirectUrl);
28+
}
29+
}
1630
return notFound();
1731
}
1832

0 commit comments

Comments
 (0)