From 833072afd5cedd7d9cfb5d362ecf5e91f3e86f2e Mon Sep 17 00:00:00 2001 From: Nikos Kontakis Date: Tue, 24 Sep 2024 18:35:06 +0300 Subject: [PATCH] Address comments --- src/contexts/AccountsContext.tsx | 7 +++++-- src/contexts/NetworkContext.tsx | 14 ++++++------- src/hooks/useIdentity.tsx | 34 ++++++++++++-------------------- src/lib/utils.ts | 6 ------ src/pages/Delegate/index.tsx | 7 ------- 5 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/contexts/AccountsContext.tsx b/src/contexts/AccountsContext.tsx index dc36a0c..22713d1 100644 --- a/src/contexts/AccountsContext.tsx +++ b/src/contexts/AccountsContext.tsx @@ -21,6 +21,10 @@ export interface IAccountContext { selectAccount: (account: InjectedPolkadotAccount | undefined) => void } +type AccountAddressType = { + address: string +} + const AccountContext = createContext(undefined) const AccountContextProvider = ({ children }: AccountContextProps) => { @@ -50,8 +54,7 @@ const AccountContextProvider = ({ children }: AccountContextProps) => { useEffect(() => { if (localStorageAccount) { const account = accounts.find( - (account: { address: string }) => - account.address === localStorageAccount, + ({ address }: AccountAddressType) => address === localStorageAccount, ) if (account) { selectAccount(account) diff --git a/src/contexts/NetworkContext.tsx b/src/contexts/NetworkContext.tsx index 18d9345..ff953a8 100644 --- a/src/contexts/NetworkContext.tsx +++ b/src/contexts/NetworkContext.tsx @@ -46,7 +46,9 @@ export type SupportedNetworkNames = | 'kusama-lc' | NetworksFromConfig export type ApiType = TypedApi -export type PeopleApiType = TypedApi +export type PeopleApiType = TypedApi< + typeof dotPeople | typeof ksmPeople | typeof westendPeople +> export const descriptorName: Record = { polkadot: dot, @@ -71,12 +73,10 @@ export interface INetworkContext { lightClientLoaded: boolean isLight: boolean selectNetwork: (network: string, shouldResetAccountAddress?: boolean) => void - client: PolkadotClient | undefined - api: TypedApi | undefined - peopleApi: - | TypedApi - | undefined - peopleClient: PolkadotClient | undefined + client: PolkadotClient + api: TypedApi + peopleApi: PeopleApiType + peopleClient: PolkadotClient network?: SupportedNetworkNames peopleNetwork?: SupportedPeopleNetworkNames assetInfo: AssetType diff --git a/src/hooks/useIdentity.tsx b/src/hooks/useIdentity.tsx index 4833450..50cbc74 100644 --- a/src/hooks/useIdentity.tsx +++ b/src/hooks/useIdentity.tsx @@ -1,20 +1,11 @@ import { useNetwork } from '@/contexts/NetworkContext' -import { - AccountInfoIF, - acceptedJudgement, - notAcceptedJudgement, -} from '@/lib/utils' +import { AccountInfoIF, acceptedJudgement } from '@/lib/utils' import { DotPeopleQueries, IdentityJudgement } from '@polkadot-api/descriptors' import { Binary } from 'polkadot-api' import { useEffect, useState } from 'react' -const getJudgements = (judgements: [number, IdentityJudgement][]) => { - judgements.forEach((j) => { - if (acceptedJudgement.includes(j[1].type)) return true - if (notAcceptedJudgement.includes(j[1].type)) return false - }) - return false -} +const getJudgements = (judgements: [number, IdentityJudgement][]) => + judgements.some(([, j]) => acceptedJudgement.includes(j.type)) const dataToString = (value: number | string | Binary | undefined) => typeof value === 'object' ? value.asText() : (value ?? '') @@ -36,7 +27,7 @@ const mapRawIdentity = ( legal: dataToString(legal.value), matrix: dataToString(matrix.value), twitter: dataToString(twitter.value), - judgement: !!judgements.length || getJudgements(judgements), + judgement: getJudgements(judgements), } } @@ -46,15 +37,16 @@ export const useIdentity = (address: string | undefined) => { const { peopleApi } = useNetwork() useEffect(() => { - const retrieveIdentity = async () => { - if (!address) return - const id = await peopleApi?.query?.Identity.IdentityOf.getValue(address) - setIdentity({ - address, - ...mapRawIdentity(id), + if (!address || !peopleApi) return + + peopleApi.query.Identity.IdentityOf.getValue(address) + .then((id) => { + setIdentity({ + address, + ...mapRawIdentity(id), + }) }) - } - retrieveIdentity() + .catch(console.error) }, [address, peopleApi]) return identity diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 5440b36..3ec3eaa 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -142,9 +142,3 @@ export type AccountInfoIF = { } export const acceptedJudgement = ['Reasonable', 'FeePaid', 'KnownGood'] -export const notAcceptedJudgement = [ - 'Unknown', - 'OutOfDate', - 'LowQuality', - 'Erroneous', -] diff --git a/src/pages/Delegate/index.tsx b/src/pages/Delegate/index.tsx index 94061be..4070ff6 100644 --- a/src/pages/Delegate/index.tsx +++ b/src/pages/Delegate/index.tsx @@ -101,13 +101,6 @@ export const Delegate = () => { setAmountVisible('0') }, [api]) - useEffect(() => { - if (!address || delegate) return - - const res = getDelegateByAddress(address) - setDelegate(res) - }, [address, delegate, getDelegateByAddress]) - if (!delegate || !api) return
No delegate found
const onChangeAmount = (