Skip to content

Commit

Permalink
Fix sidebar
Browse files Browse the repository at this point in the history
- fix amount field validation
  • Loading branch information
samchuk-vlad committed Dec 12, 2023
1 parent f93c7b7 commit 23ef739
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 47 deletions.
126 changes: 86 additions & 40 deletions src/components/providers/MyExtensionAccountsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
import React, { createContext, useContext, useEffect, useReducer, useState } from 'react'
import { nonEmptyArr } from '@subsocial/utils'
import { getAddressFromStorage, signOut, checkIsMulti, getCurrentWallet } from '../utils/index'
import React, {
createContext,
useContext,
useEffect,
useReducer,
useState,
} from 'react'
import { nonEmptyArr, isEmptyArray } from '@subsocial/utils'
import {
getAddressFromStorage,
signOut,
checkIsMulti,
getCurrentWallet,
} from '../utils/index'
import SignInModal from '../onlySearch/SignInModal'
import { InjectedAccountWithMeta } from '@polkadot/extension-inject/types'
import { useRouter } from 'next/router'
import { useAppDispatch, useAppSelector, AppDispatch } from '../../rtk/app/store'
import {
useAppDispatch,
useAppSelector,
AppDispatch,
} from '../../rtk/app/store'
import { selectAccounts } from '../../rtk/features/accounts/accountsSlice'
import { toGenericAccountId, getAddressesByDomain, toGenericAccountIds } from '../../rtk/app/util'
import { selectCurrentAccount, currentAccountActions } from '../../rtk/features/accounts/currentAccountSlice'
import { recheckStatuses, mobileWalletConection, desktopWalletConnect } from './utils'
import {
toGenericAccountId,
getAddressesByDomain,
toGenericAccountIds,
} from '../../rtk/app/util'
import {
selectCurrentAccount,
currentAccountActions,
} from '../../rtk/features/accounts/currentAccountSlice'
import {
recheckStatuses,
mobileWalletConection,
desktopWalletConnect,
} from './utils'
import { ExtensionAccountContext, Balances, Status } from './types'
import { useResponsiveSize } from '../responsive/ResponsiveContext'
import { BIGNUMBER_ZERO } from '../../config/app/consts'

const defaultCrowdloanBalances = {
kusama: BIGNUMBER_ZERO,
polkadot: BIGNUMBER_ZERO
polkadot: BIGNUMBER_ZERO,
}

export const defaultBalances = {
Expand All @@ -28,51 +54,60 @@ export const defaultBalances = {
export enum StepsEnum {
Disabled = -1,
SelectWallet,
SelectAccount
SelectAccount,
}

export const ExtensionAccountsContext = createContext<ExtensionAccountContext>({} as ExtensionAccountContext)
export const ExtensionAccountsContext = createContext<ExtensionAccountContext>(
{} as ExtensionAccountContext
)

export function ExtensionAccountProvider (props: React.PropsWithChildren<{}>) {
export function ExtensionAccountProvider(props: React.PropsWithChildren<{}>) {
const { isMobile } = useResponsiveSize()

const currentWallet = getCurrentWallet()

const [ currentStep, setCurrentStep ] = useState(currentWallet || isMobile ? StepsEnum.SelectAccount : StepsEnum.SelectWallet)
const [currentStep, setCurrentStep] = useState(
currentWallet || isMobile ? StepsEnum.SelectAccount : StepsEnum.SelectWallet
)

const currentAddress = useCurrentAccount()
const [ showSignInModal, setShowSignInModal ] = useState(false)
const [ myAccount, setMyAccount ] = useState<string>()
const [ isMulti, setIsMulti ] = useState(currentAddress ? checkIsMulti(currentAddress.join(',')) : false)
const [ balances, setBalances ] = useState<Balances>(defaultBalances)
const [ refreshBalances, setRefreshBalances ] = useState(false)
const [showSignInModal, setShowSignInModal] = useState(false)
const [myAccount, setMyAccount] = useState<string>()
const [isMulti, setIsMulti] = useState(
currentAddress ? checkIsMulti(currentAddress.join(',')) : false
)
const [balances, setBalances] = useState<Balances>(defaultBalances)
const [refreshBalances, setRefreshBalances] = useState(false)
const dispatch = useAppDispatch()

const { query: { address: addressOrDomainFromUrl } } = useRouter()
const {
query: { address: addressOrDomainFromUrl },
} = useRouter()

const openModal = () => setShowSignInModal(true)
const closeModal = () => setShowSignInModal(false)

const [ recheckId, recheck ] = useReducer(x => (x + 1) % 16384, 0)
const [recheckId, recheck] = useReducer((x) => (x + 1) % 16384, 0)

const [ status, setStatus ] = useState<Status>('LOADING')
const [status, setStatus] = useState<Status>('LOADING')

useEffect(() => {
const props = { dispatch, setStatus }
isMobile ? mobileWalletConection(props) : desktopWalletConnect(props)
}, [ recheckId ])
}, [recheckId])

useEffect(() => {
if (!recheckStatuses.includes(status)) return

const intervalId = setInterval(recheck, 1000)
return () => clearInterval(intervalId)
}, [ status ])

}, [status])

useEffect(() => {
const setAddresses = async () => {
const addressesFromUrl = await getAddressesByDomain(addressOrDomainFromUrl?.toString().split(','))
const addressesFromUrl = await getAddressesByDomain(
addressOrDomainFromUrl?.toString().split(',')
)
const addressFromStorage = getAddressFromStorage()

let address = addressesFromUrl?.join(',')
Expand All @@ -88,8 +123,7 @@ export function ExtensionAccountProvider (props: React.PropsWithChildren<{}>) {
}

setAddresses()

}, [ addressOrDomainFromUrl, status ])
}, [addressOrDomainFromUrl, status])

const obj: ExtensionAccountContext = {
extensionStatus: status,
Expand All @@ -99,7 +133,8 @@ export function ExtensionAccountProvider (props: React.PropsWithChildren<{}>) {
isMulti,
refreshBalances,
setRefreshBalances,
setBalances: (balances: Partial<Balances>) => setBalances(b => ({ ...b, ...balances })),
setBalances: (balances: Partial<Balances>) =>
setBalances((b) => ({ ...b, ...balances })),
setMyAddress: (address: string) => setMyAccount(address),
setIsMulti: (isMulti: boolean) => setIsMulti(isMulti),

Expand All @@ -110,7 +145,7 @@ export function ExtensionAccountProvider (props: React.PropsWithChildren<{}>) {

openModal,
closeModal,
setCurrentStep
setCurrentStep,
}

return (
Expand All @@ -124,43 +159,54 @@ export function ExtensionAccountProvider (props: React.PropsWithChildren<{}>) {
export const setCurrentAddress = (dispatch: AppDispatch, accounts: string) => {
const genericAccounts = toGenericAccountIds(accounts.split(',')).join(',')

dispatch({ type: currentAccountActions.setAccount.type, payload: genericAccounts })
dispatch({
type: currentAccountActions.setAccount.type,
payload: genericAccounts,
})
}

export function useExtensionAccountsContext () {
export function useExtensionAccountsContext() {
return useContext(ExtensionAccountsContext)
}

export function useMyExtensionAccount () {
export function useMyExtensionAccount() {
return useExtensionAccountsContext()
}

export function useIsMulti () {
export function useIsMulti() {
return useExtensionAccountsContext().isMulti
}

export const useIsMyAddress = (address?: string) => {
const extensionAccounts = useMyExtensionAddresses()

return extensionAccounts
.map(x => x.address)
.map((x) => x.address)
.includes(toGenericAccountId(address))
}

export function useMyExtensionAddresses () {
export function useMyExtensionAddresses() {
return useAppSelector<InjectedAccountWithMeta[]>(selectAccounts)
}

export function useCurrentAccount () {
const addresses = useAppSelector<string>(selectCurrentAccount)?.split(',') || getAddressFromStorage()
return addresses.filter(x => x)
export function useCurrentAccount() {
const addressFromRedux =
useAppSelector<string>(selectCurrentAccount)
?.split(',')
.filter((x) => x) || []

return isEmptyArray(addressFromRedux)
? getAddressFromStorage()?.split(',')
: addressFromRedux
}

export const useMyAddresses = (): string[] | undefined =>
useMyExtensionAccount().myAccount?.split(',').filter(x => x)
useMyExtensionAccount()
.myAccount?.split(',')
.filter((x) => x)

export const useMyAddress = (): string | undefined => {
const [ address ] = useMyAddresses() || []
const [address] = useMyAddresses() || []

return address ? toGenericAccountId(address) : undefined
}
Expand All @@ -174,7 +220,7 @@ export const useMyBalances = () => {
return useMyExtensionAccount().balances
}

export function useIsSignedIn () {
export function useIsSignedIn() {
return nonEmptyArr(useMyAddresses()) || !!getAddressFromStorage()
}

Expand Down
5 changes: 2 additions & 3 deletions src/components/transfer/form-items/TokenAmountFormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ export function TokenAmountFormItem ({

useEffect(() => {
const name = props.name?.toString()
if (name) {
if (defaultAmount)
setFieldsValue({ ['amount']: parseFloat(defaultAmount) })
if (name && defaultAmount) {
setFieldsValue({ ['amount']: parseFloat(defaultAmount) })
form.validateFields([ name ])
}
}, [ maxTransfer, defaultAmount ])
Expand Down
1 change: 0 additions & 1 deletion src/components/transfer/form-items/TokenSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type TokenSelectorProps = HTMLProps<HTMLDivElement> & {
showNetwork?: boolean
filterCrossChainBridgeable?: boolean
selectProps?: Omit<SearchableSelectProps, 'options'>
isModal?: boolean
}

export type TokenData = {
Expand Down
1 change: 0 additions & 1 deletion src/components/transfer/transferContent/TransferForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ export default function TransferForm ({
setValue={crossChain ? onCrossChainTokenChange : onTokenChange}
filterCrossChainBridgeable={crossChain}
showNetwork={!crossChain}
isModal={isModal}
/>
</Form.Item>
<TokenBalanceView
Expand Down
2 changes: 0 additions & 2 deletions src/rtk/features/balances/balancesSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ function* fetchBalancesByNetworkWorker (

const newBalances: BalancesEntity[] = yield all(dataMap)

console.log(newBalances)

yield put(balancesActions.fetchBalancesSuccess(newBalances))
} catch (error) {
log.error('Failed to fetch balances by network', error)
Expand Down

0 comments on commit 23ef739

Please sign in to comment.