router.ts 1.42 KiB
Newer Older
Andrei Eres's avatar
Andrei Eres committed
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'
Andrei Eres's avatar
Andrei Eres committed

type Page = {
  path: string
  param: string
}

Andrei Eres's avatar
Andrei Eres committed
export const pageStore = atom<Page>()

Andrei Eres's avatar
Andrei Eres committed
function parse() {
  const path = getPath()
  const param = getParam(path)
Andrei Eres's avatar
Andrei Eres committed
  pageStore.set({ path, param })
Andrei Eres's avatar
Andrei Eres committed
}

Andrei Eres's avatar
Andrei Eres committed
onStart(pageStore, () => {
Andrei Eres's avatar
Andrei Eres committed
  parse()
  window.addEventListener('hashchange', parse)
  return () => {
    window.removeEventListener('hashchange', parse)
  }
})
Andrei Eres's avatar
Andrei Eres committed

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()
})