Skip to content
Merged
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
31 changes: 31 additions & 0 deletions src/hooks/search/use-network-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client"

import { useEffect } from "react"
import { usePathname, useRouter } from "next/navigation"

import { chainByName, type ChainName } from "@/config/chains"
Expand All @@ -15,12 +16,42 @@ const whitelistedRoutesRegex = new RegExp(
"/accounts",
].join("|")})`
)

const hosts: { [key in ChainName]?: URL } = {
mainnet: new URL("https://explorer.ssv.network/"), // production explorer
hoodi: new URL("https://explorer.hoodi.ssv.network/"), // production explorer
}

const origins = Object.values(hosts).map((host) => host.origin)

const maybeRedirect = (targetNetwork: ChainName) => {
// Only redirect when on a production explorer (mainnet or hoodi).
// This enables cross-network switching (mainnet ↔ hoodi).
// Non-production origins (e.g. explorer.stage.ssv.network, localhost) skip this redirect.
if (!origins.includes(window.location.origin)) return false

const targetHost = hosts[targetNetwork]
if (!targetHost) return false

const targetUrl = new URL(window.location.pathname, targetHost.origin)
if (window.location.origin === targetUrl.origin) return false

window.location.assign(targetUrl.toString())
return true
}

export const useNetworkQuery = () => {
const router = useRouter()
const network = useNetworkParam()
const pathname = usePathname()

useEffect(() => {
maybeRedirect(network)
}, [network])

const setNetwork = (network: ChainName) => {
if (maybeRedirect(network)) return

if (whitelistedRoutesRegex.test(pathname))
return router.push(pathname.replace(networkRegex, network))
router.push(`/${network}/overview`)
Expand Down