Skip to content

Commit

Permalink
chore: handle unknown network
Browse files Browse the repository at this point in the history
* handle unknown network

* chore: alternative solution (#102)

---------

Co-authored-by: Neil Campbell <[email protected]>
  • Loading branch information
PatrickDinh and neilcampbell authored Jun 17, 2024
1 parent e45fc7c commit 0c27a7c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/App.routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const routes = evalTemplates([
},
{
template: Urls.Explore,
errorElement: <ErrorPage />,
element: (
<NetworkPage>
<Outlet />
Expand Down
4 changes: 2 additions & 2 deletions src/features/deep-link/hooks/use-deep-link.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, useEffect } from 'react'
import { listen } from '@tauri-apps/api/event'
import { useNavigate } from 'react-router-dom'
import { useSelectedNetwork } from '@/features/settings/data'
import { useSetSelectedNetwork } from '@/features/settings/data'
import { parseDeepLink } from '@/features/deep-link/parse-deep-link'
import { Urls } from '@/routes/urls'

export function useDeepLink() {
const [_, setSelectedNetwork] = useSelectedNetwork()
const setSelectedNetwork = useSetSelectedNetwork()
const navigate = useNavigate()

const handleDeepLink = useCallback(
Expand Down
19 changes: 13 additions & 6 deletions src/features/network/pages/network-page.tsx
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
}

0 comments on commit 0c27a7c

Please sign in to comment.