From 855784529a64d320997f82ab895d65aab1c2e006 Mon Sep 17 00:00:00 2001 From: Michal Zielenkiewicz Date: Wed, 17 Jan 2024 15:31:32 +0100 Subject: [PATCH] Enable Consensus routing --- .changelog/1129.trivial.md | 1 + src/app/hooks/useScopeParam.ts | 15 ++++++++++++--- src/app/utils/route-utils.ts | 22 ++++++++++++++++++++-- src/routes.tsx | 20 ++++++++++++++++++-- 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 .changelog/1129.trivial.md diff --git a/.changelog/1129.trivial.md b/.changelog/1129.trivial.md new file mode 100644 index 000000000..53771bbf7 --- /dev/null +++ b/.changelog/1129.trivial.md @@ -0,0 +1 @@ +Enable Consensus routing diff --git a/src/app/hooks/useScopeParam.ts b/src/app/hooks/useScopeParam.ts index 6e3cc715c..c88dbd5d2 100644 --- a/src/app/hooks/useScopeParam.ts +++ b/src/app/hooks/useScopeParam.ts @@ -1,4 +1,4 @@ -import { useParams, useRouteError } from 'react-router-dom' +import { useRouteLoaderData, useParams, useRouteError } from 'react-router-dom' import { Network } from '../../types/network' import { RouteUtils } from '../utils/route-utils' import { AppError, AppErrors } from '../../types/errors' @@ -14,14 +14,23 @@ type ScopeInfo = SearchScope & { valid: boolean } +type Scope = { + layer: Layer | undefined + network: Network | undefined +} + /** * Use this in situations where we might or might not have a scope */ export const useScopeParam = (): ScopeInfo | undefined => { - const { network, layer } = useParams() + const runtimeScope = useRouteLoaderData('runtimeScope') as Scope + const consensusScope = useRouteLoaderData('consensusScope') as Scope + const loaderData = runtimeScope || consensusScope const error = useRouteError() - if (network === undefined && layer === undefined) return undefined + if (loaderData?.network === undefined && loaderData?.layer === undefined) return undefined + + const { network, layer } = loaderData const scope: ScopeInfo = { network: network as Network, diff --git a/src/app/utils/route-utils.ts b/src/app/utils/route-utils.ts index 5c7698838..57114ab18 100644 --- a/src/app/utils/route-utils.ts +++ b/src/app/utils/route-utils.ts @@ -176,7 +176,22 @@ export const transactionParamLoader = async ({ params }: LoaderFunctionArgs) => return validateTxHashParam(params.hash!) } -export const scopeLoader = async (args: LoaderFunctionArgs) => { +export const scopeConsensusLoader = async (args: LoaderFunctionArgs) => { + const { + params: { network }, + } = args + + if (!network || !RouteUtils.getEnabledNetworks().includes(network as Network)) { + throw new AppError(AppErrors.InvalidUrl) + } + + return { + network: network as Network, + layer: Layer.consensus, + } +} + +export const scopeRuntimeLoader = async (args: LoaderFunctionArgs) => { const { params: { network, layer }, } = args @@ -192,5 +207,8 @@ export const scopeLoader = async (args: LoaderFunctionArgs) => { throw new AppError(AppErrors.UnsupportedLayer) } - return true + return { + network: network as Network, + layer: layer as Layer, + } } diff --git a/src/routes.tsx b/src/routes.tsx index 1db47a9ec..780537caf 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -13,7 +13,8 @@ import { addressParamLoader, blockHeightParamLoader, transactionParamLoader, - scopeLoader, + scopeConsensusLoader, + scopeRuntimeLoader, } from './app/utils/route-utils' import { searchParamLoader } from './app/components/Search/search-utils' import { RoutingErrorPage } from './app/pages/RoutingErrorPage' @@ -29,6 +30,7 @@ import { TokenHoldersCard } from './app/pages/TokenDashboardPage/TokenHoldersCar import { TokenInventoryCard } from './app/pages/TokenDashboardPage/TokenInventoryCard' import { NFTInstanceDashboardPage, useNftDetailsProps } from './app/pages/NFTInstanceDashboardPage' import { NFTMetadataCard } from './app/pages/NFTInstanceDashboardPage/NFTMetadataCard' +import { ConsensusDashboardPage } from 'app/pages/ConsensusDashboardPage' const NetworkSpecificPart = () => ( @@ -55,11 +57,25 @@ export const routes: RouteObject[] = [ element: withDefaultTheme(), loader: searchParamLoader, }, + { + path: '/:network/consensus', + element: , + errorElement: withDefaultTheme(), + loader: scopeConsensusLoader, + id: 'consensusScope', + children: [ + { + path: '', + element: , + }, + ], + }, { path: '/:network/:layer', element: , errorElement: withDefaultTheme(), - loader: scopeLoader, + loader: scopeRuntimeLoader, + id: 'runtimeScope', children: [ { path: '',