Unverified Commit 2e62baf2 authored by Andrei Eres's avatar Andrei Eres Committed by GitHub
Browse files

Refactor routing (#113)

parent 25fc38a7
Pipeline #189615 passed with stages
in 1 minute and 25 seconds
import { atom, onStart } from 'nanostores'
import { getParam, getPath } from '../utils/routing'
import { PHISHING_PAGE_REDIRECT } from '@polkadot/extension-base/defaults'
import { atom, computed, onSet, onStart } from 'nanostores'
import { exact, exactWithParam, getParam, getPath } from '../utils/routing'
import { authRequestsStore } from './authRequests'
import { metaRequestsStore } from './metaRequests'
import { signRequestsStore } from './signRequests'
type Page = {
path: string
param: string
}
export const pageStore = atom<Page>()
function parse() {
const path = getPath()
const param = getParam(path)
router.set({ path, param })
pageStore.set({ path, param })
}
export const router = atom<Page>()
onStart(router, () => {
onStart(pageStore, () => {
parse()
window.addEventListener('hashchange', parse)
return () => {
window.removeEventListener('hashchange', parse)
}
})
type Route = 'auth' | 'meta' | 'sign' | 'import' | 'phishing' | 'accounts'
export const routeStore = computed(
[pageStore, authRequestsStore, metaRequestsStore, signRequestsStore],
({ path }, authRequests, metaRequests, signRequests) => {
if (exact(path, '') && authRequests?.length > 0) return 'auth'
if (exact(path, '') && metaRequests?.length > 0) return 'meta'
if (exact(path, '') && signRequests?.length > 0) return 'sign'
if (exact(path, '/account/import')) return 'import'
if (exactWithParam(path, `${PHISHING_PAGE_REDIRECT}/:website`))
return 'phishing'
return 'accounts' as Route
}
)
onSet(routeStore, ({ newValue, abort }) => {
if (routeStore.get() === newValue) abort()
})
import { PHISHING_PAGE_REDIRECT } from '@polkadot/extension-base/defaults'
import { useStore } from '@nanostores/react'
import React from 'react'
import { routeStore } from '../../stores/router'
import Accounts from '../accounts/Accounts'
import ImportQr from '../accounts/ImportQr'
import Authorize from '../requests/Authorize'
import Metadata from '../requests/Metadata'
import Signing from '../requests/Signing'
import PhishingDetected from '../security/PhishingDetected'
import { authRequestsStore } from '../../stores/authRequests'
import { metaRequestsStore } from '../../stores/metaRequests'
import { router } from '../../stores/router'
import { signRequestsStore } from '../../stores/signRequests'
import { exact, exactWithParam } from '../../utils/routing'
const Router: React.FC = () => {
const { path } = useStore(router)
const authRequests = useStore(authRequestsStore)
const metaRequests = useStore(metaRequestsStore)
const signRequests = useStore(signRequestsStore)
const route = useStore(routeStore)
if (exact(path, '') && authRequests?.length) return <Authorize />
if (exact(path, '') && metaRequests?.length) return <Metadata />
if (exact(path, '') && signRequests?.length) return <Signing />
if (exact(path, '/account/import')) return <ImportQr />
if (exactWithParam(path, `${PHISHING_PAGE_REDIRECT}/:website`))
return <PhishingDetected />
return <Accounts />
return (
<>
{route === 'auth' && <Authorize />}
{route === 'meta' && <Metadata />}
{route === 'sign' && <Signing />}
{route === 'import' && <ImportQr />}
{route === 'phishing' && <PhishingDetected />}
{route === 'accounts' && <Accounts />}
</>
)
}
export default Router
import { useStore } from '@nanostores/react'
import React from 'react'
import { router } from '../../stores/router'
import { pageStore } from '../../stores/router'
const PhishingDetected: React.FC = () => {
const { param: website } = useStore(router)
const { param: website } = useStore(pageStore)
const decodedWebsite = decodeURIComponent(website)
return (
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment