Skip to content

Commit

Permalink
chore: make account management toggles use toggle funtions instead of…
Browse files Browse the repository at this point in the history
… setters
  • Loading branch information
woodenfurniture committed May 4, 2024
1 parent b77cd73 commit fb671b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
66 changes: 34 additions & 32 deletions src/components/ManageAccountsDrawer/components/ImportAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
selectHighestAccountNumberForChainId,
selectIsAnyAccountIdEnabled,
} from 'state/slices/selectors'
import { useAppDispatch, useAppSelector } from 'state/store'
import { store, useAppDispatch, useAppSelector } from 'state/store'

import { DrawerContentWrapper } from './DrawerContent'

Expand All @@ -49,7 +49,7 @@ type TableRowProps = {
accountIds: AccountId[]
accountNumber: number
asset: Asset
onAccountIdsActiveChange: (accountIds: AccountId[], isActive: boolean) => void
onToggleAccountIdsActive: (accountIds: AccountId[]) => void
}

type TableRowAccountProps = {
Expand Down Expand Up @@ -97,16 +97,15 @@ const TableRowAccount = forwardRef<TableRowAccountProps, 'div'>(({ asset, accoun
})

const TableRow = forwardRef<TableRowProps, 'div'>(
({ asset, accountNumber, accountIds, onAccountIdsActiveChange }, ref) => {
const isAccountEnabledInRedux = useAppSelector(state =>
selectIsAnyAccountIdEnabled(state, accountIds),
)
({ asset, accountNumber, accountIds, onToggleAccountIdsActive }, ref) => {
const isAccountEnabled = useAppSelector(state => selectIsAnyAccountIdEnabled(state, accountIds))

const [isAccountActive, toggleIsAccountActive] = useToggle(isAccountEnabledInRedux)
const [isAccountActive, toggleIsAccountActive] = useToggle(isAccountEnabled)

useEffect(() => {
onAccountIdsActiveChange(accountIds, isAccountActive)
}, [accountIds, isAccountActive, isAccountEnabledInRedux, onAccountIdsActiveChange])
const handleToggleIsAccountActive = useCallback(() => {
toggleIsAccountActive()
onToggleAccountIdsActive(accountIds)
}, [accountIds, onToggleAccountIdsActive, toggleIsAccountActive])

const firstAccount = useMemo(() => accountIds[0], [accountIds])
const otherAccounts = useMemo(() => accountIds.slice(1), [accountIds])
Expand All @@ -115,7 +114,7 @@ const TableRow = forwardRef<TableRowProps, 'div'>(
<>
<Tr opacity={isAccountActive ? '1' : '0.5'}>
<Td>
<Switch size='lg' isChecked={isAccountActive} onChange={toggleIsAccountActive} />
<Switch size='lg' isChecked={isAccountActive} onChange={handleToggleIsAccountActive} />
</Td>
<Td>
<RawText color='text.subtle'>{accountNumber}</RawText>
Expand All @@ -124,9 +123,9 @@ const TableRow = forwardRef<TableRowProps, 'div'>(
<TableRowAccount ref={ref} asset={asset} accountId={firstAccount} />
</Tr>
{otherAccounts.map(accountId => (
<Tr opacity={isAccountActive ? '1' : '0.5'}>
<Tr key={accountId} opacity={isAccountActive ? '1' : '0.5'}>
<Td colSpan={2} bg='background.surface.raised.base'></Td>
<TableRowAccount key={accountId} ref={ref} asset={asset} accountId={accountId} />
<TableRowAccount ref={ref} asset={asset} accountId={accountId} />
</Tr>
))}
</>
Expand Down Expand Up @@ -178,9 +177,7 @@ export const ImportAccounts = ({ chainId, onClose }: ImportAccountsProps) => {
)
},
}) > 0
const [accountIdActiveStateUpdate, setAccountIdActiveStateUpdate] = useState<
Record<string, boolean>
>({})
const [toggledActiveAccountIds, setToggledActiveAccountIds] = useState<Set<AccountId>>(new Set())

// initial fetch to detect the number of accounts based on the "first empty account" heuristic
const { data: allAccountIdsWithActivity } = useQuery(
Expand Down Expand Up @@ -215,20 +212,27 @@ export const ImportAccounts = ({ chainId, onClose }: ImportAccountsProps) => {
})
}, [accounts, chainId, queryClient, wallet, walletDeviceId])

const handleAccountIdsActiveChange = useCallback((accountIds: AccountId[], isActive: boolean) => {
setAccountIdActiveStateUpdate(previousState => {
const stateUpdate = accountIds.reduce((accumulator, accountId) => {
return { ...accumulator, [accountId]: isActive }
}, {})
return { ...previousState, ...stateUpdate }
const handleToggleAccountIdsActive = useCallback((accountIds: AccountId[]) => {
setToggledActiveAccountIds(previousState => {
const updatedState = new Set(previousState)
for (const accountId of accountIds) {
if (updatedState.has(accountId)) {
updatedState.delete(accountId)
} else {
updatedState.add(accountId)
}
}

return updatedState
})
}, [])

// TODO: Loading state
const handleDone = useCallback(async () => {
// for every new account that is active, fetch the account and upsert it into the redux state
for (const [accountId, isActive] of Object.entries(accountIdActiveStateUpdate)) {
if (!isActive) continue
for (const accountId of toggledActiveAccountIds) {
const isEnabled = selectIsAnyAccountIdEnabled(store.getState(), [accountId])
if (isEnabled) continue
await dispatch(portfolioApi.endpoints.getAccount.initiate({ accountId, upsertOnFetch: true }))
}

Expand All @@ -246,14 +250,12 @@ export const ImportAccounts = ({ chainId, onClose }: ImportAccountsProps) => {
}),
)

const hiddenAccountIds = Object.entries(accountIdActiveStateUpdate)
.filter(([_, isActive]) => !isActive)
.map(([accountId]) => accountId)

dispatch(portfolio.actions.setHiddenAccountIds(hiddenAccountIds))
for (const accountId of toggledActiveAccountIds) {
dispatch(portfolio.actions.toggleAccountIdHidden(accountId))
}

onClose()
}, [accountIdActiveStateUpdate, accounts, dispatch, onClose, walletDeviceId])
}, [toggledActiveAccountIds, accounts, dispatch, onClose, walletDeviceId])

const accountRows = useMemo(() => {
if (!asset) return null
Expand All @@ -265,11 +267,11 @@ export const ImportAccounts = ({ chainId, onClose }: ImportAccountsProps) => {
accountNumber={accountNumber}
accountIds={accountIds}
asset={asset}
onAccountIdsActiveChange={handleAccountIdsActiveChange}
onToggleAccountIdsActive={handleToggleAccountIdsActive}
/>
)
})
}, [accounts, asset, handleAccountIdsActiveChange])
}, [accounts, asset, handleToggleAccountIdsActive])

if (!asset) {
console.error(`No fee asset found for chainId: ${chainId}`)
Expand Down
13 changes: 11 additions & 2 deletions src/state/slices/portfolioSlice/portfolioSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,17 @@ export const portfolio = createSlice({
// add the `action.meta[SHOULD_AUTOBATCH]` field the enhancer needs
prepare: prepareAutoBatched<Portfolio>(),
},
setHiddenAccountIds: (draftState, { payload }: { payload: AccountId[] }) => {
draftState.hiddenAccountIds = payload
toggleAccountIdHidden: (draftState, { payload: accountId }: { payload: AccountId }) => {
const hiddenAccountIdsSet = new Set(draftState.hiddenAccountIds)
const isHidden = hiddenAccountIdsSet.has(accountId)

if (!isHidden) {
hiddenAccountIdsSet.add(accountId)
} else {
hiddenAccountIdsSet.delete(accountId)
}

draftState.hiddenAccountIds = Array.from(hiddenAccountIdsSet)
},
},
extraReducers: builder => builder.addCase(PURGE, () => initialState),
Expand Down

0 comments on commit fb671b3

Please sign in to comment.