From 7b07d0fb0d3a2ecb811e66363ee385c8df962bf1 Mon Sep 17 00:00:00 2001 From: lukaw3d Date: Thu, 18 Jan 2024 04:31:55 +0100 Subject: [PATCH] Simplify useScopeParam --- src/app/components/PageLayout/Header.tsx | 2 +- src/app/components/PageLayout/index.tsx | 2 +- src/app/hooks/useScopeParam.ts | 51 +++--------------------- src/app/utils/route-utils.ts | 13 +++--- 4 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/app/components/PageLayout/Header.tsx b/src/app/components/PageLayout/Header.tsx index b041d35ed3..ba64f3176f 100644 --- a/src/app/components/PageLayout/Header.tsx +++ b/src/app/components/PageLayout/Header.tsx @@ -49,7 +49,7 @@ export const Header: FC = () => { showText={!scrollTrigger && !isMobile} /> - {scope?.valid && ( + {scope && ( <> diff --git a/src/app/components/PageLayout/index.tsx b/src/app/components/PageLayout/index.tsx index e3ef6c4606..d59ef232b3 100644 --- a/src/app/components/PageLayout/index.tsx +++ b/src/app/components/PageLayout/index.tsx @@ -30,7 +30,7 @@ export const PageLayout: FC> = ({ children, m <> - {scope?.valid && scope.layer !== Layer.consensus && } + {scope && scope.layer !== Layer.consensus && } { 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) diff --git a/src/app/utils/route-utils.ts b/src/app/utils/route-utils.ts index 5322896c07..a2bdc345d9 100644 --- a/src/app/utils/route-utils.ts +++ b/src/app/utils/route-utils.ts @@ -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