Skip to content

Commit

Permalink
Merge pull request #553 from oasisprotocol/csillag/fix-layer-error-di…
Browse files Browse the repository at this point in the history
…splay

Fix displaying error messages on unsupported layers
  • Loading branch information
csillag authored Jun 19, 2023
2 parents 5525118 + 1d441c1 commit 61319b5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions .changelog/557.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error display when using invalid layers
2 changes: 1 addition & 1 deletion src/app/components/PageLayout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const Header: FC = () => {
showText={!scrollTrigger && !isMobile}
/>
</Grid>
{scope && (
{scope?.valid && (
<>
<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 @@ -46,7 +46,7 @@ export const PageLayout: FC<PropsWithChildren<PageLayoutProps>> = ({ children, m
<Box ref={bannersRef}>
<BuildBanner />
<NetworkOfflineBanner />
{scope && <RuntimeOfflineBanner />}
{scope?.valid && <RuntimeOfflineBanner />}
</Box>
<Box
sx={{
Expand Down
40 changes: 26 additions & 14 deletions src/app/hooks/useScopeParam.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useParams } from 'react-router-dom'
import { 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 @@ -10,38 +10,50 @@ export const useNetworkParam = (): Network | undefined => {
return network as Network | undefined
}

type ScopeInfo = SearchScope & {
valid: boolean
}

/**
* Use this in situations where we might or might not have a scope
*/
export const useScopeParam = (): SearchScope | undefined => {
export const useScopeParam = (): ScopeInfo | undefined => {
const { network, layer } = useParams()
const error = useRouteError()

if (network === undefined && layer === undefined) return undefined

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

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

if (!scope || !RouteUtils.getEnabledNetworks().includes(scope.network))
throw new AppError(AppErrors.UnsupportedNetwork)
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.getEnabledLayersForNetwork(scope.network).includes(scope.layer))
throw new AppError(AppErrors.UnsupportedLayer)
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
}

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

if (!scope) throw new AppError(AppErrors.UnsupportedNetwork)
Expand Down
2 changes: 1 addition & 1 deletion src/app/utils/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const scopeLoader = async (args: LoaderFunctionArgs) => {
!layer || // missing param
!RouteUtils.getEnabledLayersForNetwork(network as Network).includes(layer as Layer) // unsupported on network
) {
throw new AppError(AppErrors.InvalidUrl)
throw new AppError(AppErrors.UnsupportedLayer)
}

return true
Expand Down

0 comments on commit 61319b5

Please sign in to comment.