Skip to content

Commit d1dc7e9

Browse files
committed
Add code for customizing scope paths
Normally the "sapphire" paratime on the "testnet" network will be available via "testnet/sapphire". This commit adds a config option to override the paths for specific paratimes with any two-word constant. (So that we can use "magic/kingdom" instead of "testnet/sapphire", if we want to.)
1 parent 191a655 commit d1dc7e9

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/app/utils/route-utils.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AppError, AppErrors } from '../../types/errors'
55
import { EvmTokenType, Layer } from '../../oasis-nexus/api'
66
import { Network } from '../../types/network'
77
import { SearchScope } from '../../types/searchScope'
8-
import { isStableDeploy } from '../../config'
8+
import { isStableDeploy, specialScopePaths } from '../../config'
99
import { getSearchTermFromRequest } from '../components/Search/search-utils'
1010
import { isLayerHidden } from '../../types/layers'
1111

@@ -37,6 +37,33 @@ export type SpecifiedPerEnabledLayer<T = any, ExcludeLayers = never> = {
3737

3838
export type SpecifiedPerEnabledRuntime<T = any> = SpecifiedPerEnabledLayer<T, typeof Layer.consensus>
3939

40+
export const specialScopeRecognition: Partial<Record<string, Partial<Record<string, SearchScope>>>> = {}
41+
42+
function cacheSpecialScopePaths() {
43+
const networks = Object.keys(specialScopePaths) as Network[]
44+
45+
networks.forEach(network => {
46+
const networkPaths = specialScopePaths[network]!
47+
const layers = Object.keys(networkPaths) as Layer[]
48+
layers.forEach(layer => {
49+
const [word1, word2] = networkPaths[layer]!
50+
if (!specialScopeRecognition[word1]) {
51+
specialScopeRecognition[word1] = {}
52+
}
53+
if (specialScopeRecognition[word1]![word2]) {
54+
const other = specialScopeRecognition[word1]![word2]!
55+
console.warn(
56+
`Wrong config: conflicting special scope paths ${word1}/${word2} definitions used both for ${other.network}/${other.layer} and ${network}/${layer} `,
57+
)
58+
} else {
59+
specialScopeRecognition[word1]![word2] = { network, layer }
60+
}
61+
})
62+
})
63+
}
64+
65+
cacheSpecialScopePaths()
66+
4067
export abstract class RouteUtils {
4168
private static ENABLED_LAYERS_FOR_NETWORK = {
4269
[Network.mainnet]: {
@@ -60,7 +87,11 @@ export abstract class RouteUtils {
6087
} satisfies Record<Network, Record<Layer, boolean>>
6188

6289
static getScopeRoute = ({ network, layer }: SearchScope) => {
63-
return `/${encodeURIComponent(network)}/${encodeURIComponent(layer)}`
90+
const specialPath = specialScopePaths[network]?.[layer]
91+
const result = specialPath
92+
? `/${specialPath[0]}/${specialPath[1]}`
93+
: `/${encodeURIComponent(network)}/${encodeURIComponent(layer)}`
94+
return result
6495
}
6596

6697
static getDashboardRoute = (scope: SearchScope) => this.getScopeRoute(scope)
@@ -297,19 +328,25 @@ export const runtimeTransactionParamLoader = async ({ params }: LoaderFunctionAr
297328
return validateRuntimeTxHashParam(params.hash!)
298329
}
299330

300-
export const assertEnabledScope = ({
301-
network,
302-
layer,
303-
}: {
331+
export const assertEnabledScope = (params: {
304332
network: string | undefined
305333
layer: string | undefined
306334
}): SearchScope => {
307-
if (!network || !RouteUtils.getEnabledNetworks().includes(network as Network)) {
335+
const { network: networkLike, layer: layerLike } = params
336+
if (!networkLike || !layerLike) {
337+
throw new AppError(AppErrors.InvalidUrl)
338+
}
339+
340+
const { network, layer } = specialScopeRecognition[networkLike]?.[layerLike] ?? {
341+
network: networkLike as Network,
342+
layer: layerLike as Layer,
343+
}
344+
345+
if (!RouteUtils.getEnabledNetworks().includes(network as Network)) {
308346
throw new AppError(AppErrors.InvalidUrl)
309347
}
310348

311349
if (
312-
!layer || // missing param
313350
!RouteUtils.getAllLayersForNetwork(network as Network).enabled.includes(layer as Layer) // unsupported on network
314351
) {
315352
throw new AppError(AppErrors.UnsupportedLayer)

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,8 @@ export const specialScopeNames: Partial<Record<Network, Partial<Record<Layer, st
208208
[Network.mainnet]: {},
209209
[Network.testnet]: {},
210210
}
211+
212+
export const specialScopePaths: Partial<Record<Network, Partial<Record<Layer, [string, string]>>>> = {
213+
[Network.mainnet]: {},
214+
[Network.testnet]: {},
215+
}

0 commit comments

Comments
 (0)