mars-v2-frontend/src/hooks/useAutoLend.ts
Bob van der Helm 74c9a41a8b
fixed refetchgin of account data (#747)
Co-authored-by: Linkie Link <linkielink.dev@gmail.com>
2024-01-24 17:06:12 +01:00

54 lines
1.9 KiB
TypeScript

import useAccounts from 'hooks/accounts/useAccounts'
import useCurrentAccount from 'hooks/accounts/useCurrentAccount'
import useAutoLendEnabledAccountIds from 'hooks/localStorage/useAutoLendEnabledAccountIds'
export default function useAutoLend(): {
autoLendEnabledAccountIds: string[]
enableAutoLend: (accountId: string) => void
disableAutoLend: (accountId: string) => void
isAutoLendEnabledForCurrentAccount: boolean
setAutoLendOnAllAccounts: (lendAssets: boolean) => void
enableAutoLendAccountId: (accountId: string) => void
} {
const { data: accounts } = useAccounts('default', undefined, false)
const currentAccount = useCurrentAccount()
const [autoLendEnabledAccountIds, setAutoLendEnabledAccountIds] = useAutoLendEnabledAccountIds()
const enableAutoLend = (accountId: string) => {
const setOfAccountIds = new Set(autoLendEnabledAccountIds)
setOfAccountIds.add(accountId)
setAutoLendEnabledAccountIds(Array.from(setOfAccountIds))
}
const disableAutoLend = (accountId: string) => {
const setOfAccountIds = new Set(autoLendEnabledAccountIds)
setOfAccountIds.delete(accountId)
setAutoLendEnabledAccountIds(Array.from(setOfAccountIds))
}
const isAutoLendEnabledForCurrentAccount = currentAccount
? autoLendEnabledAccountIds.includes(currentAccount.id)
: false
const setAutoLendOnAllAccounts = (lendAssets: boolean) => {
const allAccountIds = accounts ? accounts.map((account) => account.id) : []
setAutoLendEnabledAccountIds(lendAssets ? allAccountIds : [])
}
const enableAutoLendAccountId = (accountId: string) => {
const setOfAccountIds = new Set(autoLendEnabledAccountIds)
if (!setOfAccountIds.has(accountId)) setOfAccountIds.add(accountId)
setAutoLendEnabledAccountIds(Array.from(setOfAccountIds))
}
return {
autoLendEnabledAccountIds,
enableAutoLend,
disableAutoLend,
isAutoLendEnabledForCurrentAccount,
setAutoLendOnAllAccounts,
enableAutoLendAccountId,
}
}