Skip to content

Commit b483001

Browse files
committed
fix(explorer): preserve route when switching networks
1 parent af33f23 commit b483001

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

apps/explorer/src/comps/Header.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
buildExplorerNetworkHref,
1414
EXPLORER_NETWORK_OPTIONS,
1515
getActiveExplorerNetworkOption,
16+
isExplorerNetworkPathPreservable,
1617
} from '#lib/explorer-network'
1718
import { useIsNotFoundPage } from '#lib/not-found'
1819
import ChevronDownIcon from '~icons/lucide/chevron-down'
@@ -229,7 +230,9 @@ export namespace Header {
229230
<a
230231
key={option.env}
231232
href={buildExplorerNetworkHref(option.host, currentPath, {
232-
fallbackToHome: isNotFoundPage,
233+
fallbackToHome:
234+
isNotFoundPage &&
235+
!isExplorerNetworkPathPreservable(currentPath),
233236
})}
234237
role="menuitemradio"
235238
aria-checked={isActive}

apps/explorer/src/lib/explorer-network.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ export namespace buildExplorerNetworkHref {
4040
fallbackToHome?: boolean
4141
}
4242
}
43+
44+
export function isExplorerNetworkPathPreservable(path: string): boolean {
45+
const pathname = path.split(/[?#]/, 1)[0]
46+
47+
return [
48+
/^\/$/,
49+
/^\/blocks\/?$/,
50+
/^\/tokens\/?$/,
51+
/^\/fee-amm\/?$/,
52+
/^\/tx\/[^/]+\/?$/,
53+
/^\/receipt\/[^/]+\/?$/,
54+
/^\/block\/[^/]+\/?$/,
55+
/^\/block\/countdown\/[^/]+\/?$/,
56+
/^\/address\/[^/]+\/?$/,
57+
/^\/token\/[^/]+\/?$/,
58+
].some((pattern) => pattern.test(pathname))
59+
}

apps/explorer/test/explorer-network.test.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
22
import {
33
buildExplorerNetworkHref,
44
EXPLORER_NETWORK_OPTIONS,
5+
isExplorerNetworkPathPreservable,
56
} from '#lib/explorer-network.ts'
67

78
const MAINNET_HOST = 'https://explore.tempo.xyz'
@@ -41,20 +42,29 @@ describe('explorer network switcher hrefs', () => {
4142
)
4243
})
4344

44-
it('links to the target network homepage from not-found pages', () => {
45+
it('preserves resource paths even when the current route rendered not found', () => {
46+
expect(isExplorerNetworkPathPreservable(`/receipt/${SAMPLE_HASH}`)).toBe(
47+
true,
48+
)
49+
expect(
50+
isExplorerNetworkPathPreservable(`/tx/${SAMPLE_HASH}?tab=logs#top`),
51+
).toBe(true)
52+
expect(
53+
buildExplorerNetworkHref(TESTNET_HOST, `/receipt/${SAMPLE_HASH}`),
54+
).toBe(`${TESTNET_HOST}/receipt/${SAMPLE_HASH}`)
55+
expect(
56+
buildExplorerNetworkHref(MAINNET_HOST, `/tx/${SAMPLE_HASH}?tab=logs#top`),
57+
).toBe(`${MAINNET_HOST}/tx/${SAMPLE_HASH}?tab=logs#top`)
58+
})
59+
60+
it('still links unknown not-found routes to the target network homepage', () => {
61+
expect(isExplorerNetworkPathPreservable('/definitely-not-a-route')).toBe(
62+
false,
63+
)
4564
expect(
46-
buildExplorerNetworkHref(TESTNET_HOST, `/receipt/${SAMPLE_HASH}`, {
65+
buildExplorerNetworkHref(TESTNET_HOST, '/definitely-not-a-route', {
4766
fallbackToHome: true,
4867
}),
4968
).toBe(`${TESTNET_HOST}/`)
50-
expect(
51-
buildExplorerNetworkHref(
52-
MAINNET_HOST,
53-
`/tx/${SAMPLE_HASH}?tab=logs#top`,
54-
{
55-
fallbackToHome: true,
56-
},
57-
),
58-
).toBe(`${MAINNET_HOST}/`)
5969
})
6070
})

0 commit comments

Comments
 (0)