Skip to content

Commit

Permalink
Simplify useScopeParam
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Jan 18, 2024
1 parent c8a5f4e commit 7b07d0f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/app/components/PageLayout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const Header: FC = () => {
showText={!scrollTrigger && !isMobile}
/>
</Grid>
{scope?.valid && (
{scope && (
<>
<Grid lg={6} xs={8}>
<NetworkSelector layer={scope.layer} network={scope.network} />
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/PageLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const PageLayout: FC<PropsWithChildren<PageLayoutProps>> = ({ children, m
<>
<BuildBanner />
<NetworkOfflineBanner />
{scope?.valid && scope.layer !== Layer.consensus && <RuntimeOfflineBanner />}
{scope && scope.layer !== Layer.consensus && <RuntimeOfflineBanner />}
<Box
sx={{
minHeight: '100vh',
Expand Down
51 changes: 6 additions & 45 deletions src/app/hooks/useScopeParam.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,29 @@
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'
import { SearchScope } from '../../types/searchScope'
import { Layer } from '../../oasis-nexus/api'

export const useNetworkParam = (): Network | undefined => {
const { network } = useParams()
return network as Network | undefined
}

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 runtimeScope = useRouteLoaderData('runtimeScope') as Scope
const consensusScope = useRouteLoaderData('consensusScope') as Scope
const loaderData = runtimeScope || consensusScope
export const useScopeParam = (): SearchScope | undefined => {
const runtimeScope = useRouteLoaderData('runtimeScope') as SearchScope | undefined
const consensusScope = useRouteLoaderData('consensusScope') as SearchScope | undefined
const error = useRouteError()

if (loaderData?.network === undefined && loaderData?.layer === undefined) return undefined

const { network, layer } = loaderData

const scope: ScopeInfo = {
network: network as Network,
layer: layer as Layer,
valid: true,
}

if (network === undefined || layer === undefined) {
scope.valid = false
if (!error)
throw new Error(
'You must either specify both network and layer or none of them. You can not have one but not the other.',
)
}

if (!RouteUtils.getEnabledNetworks().includes(scope.network)) {
scope.valid = false
if (!error) throw new AppError(AppErrors.UnsupportedNetwork)
}

if (!RouteUtils.getEnabledLayersForNetwork(scope.network).includes(scope.layer)) {
scope.valid = false
if (!error) throw new AppError(AppErrors.UnsupportedLayer)
}

return scope
if (error) throw error
return runtimeScope ?? consensusScope ?? undefined
}

/**
* Use this in situations where we require to have a scope
*/
export const useRequiredScopeParam = (): ScopeInfo => {
export const useRequiredScopeParam = (): SearchScope => {
const scope = useScopeParam()

if (!scope) throw new AppError(AppErrors.UnsupportedNetwork)
Expand Down
13 changes: 7 additions & 6 deletions src/app/utils/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ export const assertEnabledScope = ({
network: string | undefined
layer: string | undefined
}): SearchScope => {
if (!network || !RouteUtils.getEnabledNetworks().includes(network as Network)) {
if (!network || !layer) {
// missing param
throw new AppError(AppErrors.InvalidUrl)
}

if (
!layer || // missing param
!RouteUtils.getEnabledLayersForNetwork(network as Network).includes(layer as Layer) // unsupported on network
) {
if (!RouteUtils.getEnabledNetworks().includes(network as Network)) {
throw new AppError(AppErrors.UnsupportedNetwork)
}
if (!RouteUtils.getEnabledLayersForNetwork(network as Network).includes(layer as Layer)) {
// unsupported on network
throw new AppError(AppErrors.UnsupportedLayer)
}
return { network, layer } as SearchScope
Expand Down

0 comments on commit 7b07d0f

Please sign in to comment.