Skip to content

Commit

Permalink
Merge pull request #1129 from oasisprotocol/mz/consensusRoutes
Browse files Browse the repository at this point in the history
Enable Consensus routing
  • Loading branch information
buberdds authored Jan 18, 2024
2 parents 1727889 + 8557845 commit 93974e4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions .changelog/1129.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable Consensus routing
15 changes: 12 additions & 3 deletions src/app/hooks/useScopeParam.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions src/app/utils/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}
20 changes: 18 additions & 2 deletions src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 = () => (
<ThemeByNetwork network={useRequiredScopeParam().network}>
Expand All @@ -55,11 +57,25 @@ export const routes: RouteObject[] = [
element: withDefaultTheme(<SearchResultsPage />),
loader: searchParamLoader,
},
{
path: '/:network/consensus',
element: <NetworkSpecificPart />,
errorElement: withDefaultTheme(<RoutingErrorPage />),
loader: scopeConsensusLoader,
id: 'consensusScope',
children: [
{
path: '',
element: <ConsensusDashboardPage />,
},
],
},
{
path: '/:network/:layer',
element: <NetworkSpecificPart />,
errorElement: withDefaultTheme(<RoutingErrorPage />),
loader: scopeLoader,
loader: scopeRuntimeLoader,
id: 'runtimeScope',
children: [
{
path: '',
Expand Down

0 comments on commit 93974e4

Please sign in to comment.