|
1 | 1 | import { useRequiredParam } from '@/features/common/hooks/use-required-param' |
2 | | -import { UrlParams, Urls } from '@/routes/urls' |
3 | | -import { networksConfigs } from '@/features/settings/data' |
4 | | -import { useNavigate } from 'react-router-dom' |
| 2 | +import { UrlParams } from '@/routes/urls' |
| 3 | +import { networksConfigs, useSelectedNetwork } from '@/features/settings/data' |
| 4 | +import { useLocation, useNavigate } from 'react-router-dom' |
5 | 5 | import { useEffect } from 'react' |
6 | 6 |
|
7 | 7 | type Props = { |
8 | 8 | children: React.ReactNode |
9 | 9 | } |
10 | 10 | export function NetworkPage({ children }: Props) { |
| 11 | + const [selectedNetwork] = useSelectedNetwork() |
11 | 12 | const { networkId } = useRequiredParam(UrlParams.NetworkId) |
12 | 13 | const navigate = useNavigate() |
| 14 | + const { pathname, search, hash } = useLocation() |
| 15 | + const isWildcardNetworkRoute = networkId === '_' |
| 16 | + const isValidNetwork = isWildcardNetworkRoute || (networksConfigs.find((c) => c.id === networkId) ? true : false) |
| 17 | + if (!isValidNetwork) { |
| 18 | + throw new Error(`"${networkId}" is not a valid network.`) |
| 19 | + } |
13 | 20 |
|
14 | 21 | useEffect(() => { |
15 | | - if (!networksConfigs.find((c) => c.id === networkId)) { |
16 | | - navigate(Urls.Index.build({})) |
| 22 | + if (isWildcardNetworkRoute) { |
| 23 | + navigate(pathname.replace('_', selectedNetwork) + search + hash) |
17 | 24 | } |
18 | | - }, [navigate, networkId]) |
| 25 | + }, [hash, pathname, search, navigate, selectedNetwork, isWildcardNetworkRoute]) |
19 | 26 |
|
20 | 27 | return children |
21 | 28 | } |
0 commit comments