-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* handle unknown network * chore: alternative solution (#102) --------- Co-authored-by: Neil Campbell <[email protected]>
- Loading branch information
1 parent
e45fc7c
commit 0c27a7c
Showing
3 changed files
with
16 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import { useRequiredParam } from '@/features/common/hooks/use-required-param' | ||
import { UrlParams, Urls } from '@/routes/urls' | ||
import { networksConfigs } from '@/features/settings/data' | ||
import { useNavigate } from 'react-router-dom' | ||
import { UrlParams } from '@/routes/urls' | ||
import { networksConfigs, useSelectedNetwork } from '@/features/settings/data' | ||
import { useLocation, useNavigate } from 'react-router-dom' | ||
import { useEffect } from 'react' | ||
|
||
type Props = { | ||
children: React.ReactNode | ||
} | ||
export function NetworkPage({ children }: Props) { | ||
const [selectedNetwork] = useSelectedNetwork() | ||
const { networkId } = useRequiredParam(UrlParams.NetworkId) | ||
const navigate = useNavigate() | ||
const { pathname, search, hash } = useLocation() | ||
const isWildcardNetworkRoute = networkId === '_' | ||
const isValidNetwork = isWildcardNetworkRoute || (networksConfigs.find((c) => c.id === networkId) ? true : false) | ||
if (!isValidNetwork) { | ||
throw new Error(`"${networkId}" is not a valid network.`) | ||
} | ||
|
||
useEffect(() => { | ||
if (!networksConfigs.find((c) => c.id === networkId)) { | ||
navigate(Urls.Index.build({})) | ||
if (isWildcardNetworkRoute) { | ||
navigate(pathname.replace('_', selectedNetwork) + search + hash) | ||
} | ||
}, [navigate, networkId]) | ||
}, [hash, pathname, search, navigate, selectedNetwork, isWildcardNetworkRoute]) | ||
|
||
return children | ||
} |