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" ;
55import { Providers } from "./Providers" ;
66
77type 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