Unverified Commit 11b01a8c authored by Andrei Eres's avatar Andrei Eres Committed by GitHub
Browse files

Refactor subscribable stores (#114)

parent 2e62baf2
Pipeline #189645 passed with stages
in 2 minutes and 15 seconds
import { AccountJson } from '@polkadot/extension-base/background/types'
import { action, atom, computed, onStart } from 'nanostores'
import { atom, computed, onStart } from 'nanostores'
import { subscribeAccounts } from '../messaging/uiActions'
import { buildHierarchy } from '../utils/buildHierarchy'
import { orderAccounts } from '../utils/orderAccounts'
export const accountsStore = atom<AccountJson[]>([])
let isSubscribed = false
onStart(accountsStore, () => {
subscribeAccounts(setAccounts).catch(console.error)
if (isSubscribed) return
isSubscribed = true
subscribeAccounts(accountsStore.set).catch(console.error)
})
export const accountNamesByAddressStore = computed(accountsStore, (list) =>
......@@ -15,11 +18,4 @@ export const accountNamesByAddressStore = computed(accountsStore, (list) =>
return res
}, {} as Record<string, string | undefined>)
)
const setAccounts = action(
accountsStore,
'set_accounts',
(store, list: AccountJson[]) => {
store.set(buildHierarchy(list))
}
)
export const orderedAccountsStore = computed(accountsStore, orderAccounts)
......@@ -11,6 +11,9 @@ import { subscribeAuthorizeRequests } from '../messaging/uiActions'
// ]
export const authRequestsStore = atom<AuthorizeRequest[]>([])
let isSubscribed = false
onStart(authRequestsStore, () => {
if (isSubscribed) return
isSubscribed = true
subscribeAuthorizeRequests(authRequestsStore.set).catch(console.error)
})
......@@ -4,6 +4,9 @@ import { subscribeMetadataRequests } from '../messaging/uiActions'
export const metaRequestsStore = atom<MetadataRequest[]>([])
let isSubscribed = false
onStart(metaRequestsStore, () => {
if (isSubscribed) return
isSubscribed = true
subscribeMetadataRequests(metaRequestsStore.set).catch(console.error)
})
......@@ -45,6 +45,9 @@ import { subscribeSigningRequests } from '../messaging/uiActions'
// ]
export const signRequestsStore = atom<SigningRequest[]>([])
let isSubscribed = false
onStart(signRequestsStore, () => {
if (isSubscribed) return
isSubscribed = true
subscribeSigningRequests(signRequestsStore.set).catch(console.error)
})
import { useStore } from '@nanostores/react'
import React, { useEffect } from 'react'
import styled from 'styled-components'
import Key from '../components/Key'
import { accountsStore } from '../../stores/accounts'
import { orderedAccountsStore } from '../../stores/accounts'
import {
addHeaderAction,
importHeaderAction,
resetHeaderActions,
} from '../../stores/headerActions'
import Key from '../components/Key'
import { BaseProps } from '../types'
import NoAccounts from './NoAccounts'
const Accounts: React.FC<BaseProps> = ({ className }) => {
const accounts = useStore(accountsStore)
const accounts = useStore(orderedAccountsStore)
useEffect(() => {
if (accounts.length > 0) addHeaderAction(importHeaderAction)
......
import { AccountJson } from '@polkadot/extension-base/background/types'
import { CHAINS_MAP } from './constants'
export function buildHierarchy(accounts: AccountJson[]): AccountJson[] {
return accounts.sort(compare)
}
function compare(a: AccountJson, b: AccountJson): number {
return (
compareByNetwork(a, b) ||
compareByName(a, b) ||
compareByPath(a, b) ||
compareByCreation(a, b)
export function orderAccounts(accounts: AccountJson[]): AccountJson[] {
return accounts.sort(
(a, b) => byChain(a, b) || byName(a, b) || byCreation(a, b)
)
}
function compareByNetwork(a: AccountJson, b: AccountJson): number {
function byChain(a: AccountJson, b: AccountJson): number {
return getChain(a?.genesisHash).localeCompare(getChain(b?.genesisHash))
}
function compareByName(a: AccountJson, b: AccountJson): number {
function byName(a: AccountJson, b: AccountJson): number {
return normalize(a.name).localeCompare(normalize(b.name))
}
function compareByPath(a: AccountJson, b: AccountJson): number {
return normalize(a.suri).localeCompare(normalize(b.suri))
}
function compareByCreation(a: AccountJson, b: AccountJson): number {
function byCreation(a: AccountJson, b: AccountJson): number {
return (a.whenCreated || Infinity) - (b.whenCreated || Infinity)
}
......
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