add lastUpdatedAt to improve useMangoAccount rerendering trigger

This commit is contained in:
Tyler Shipe 2021-11-30 16:04:55 -05:00
parent 4eb7cd3934
commit 2d824c2213
9 changed files with 36 additions and 25 deletions

View File

@ -34,8 +34,7 @@ export default function AccountInfo() {
const connected = useMangoStore((s) => s.wallet.connected) const connected = useMangoStore((s) => s.wallet.connected)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
const mangoAccount = useMangoAccount() const { mangoAccount, initialLoad } = useMangoAccount()
const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad)
const marketConfig = useMangoStore((s) => s.selectedMarket.config) const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const mangoClient = useMangoStore((s) => s.connection.client) const mangoClient = useMangoStore((s) => s.connection.client)
const actions = useMangoStore((s) => s.actions) const actions = useMangoStore((s) => s.actions)
@ -164,7 +163,7 @@ export default function AccountInfo() {
{t('equity')} {t('equity')}
</div> </div>
<div className="text-th-fgd-1"> <div className="text-th-fgd-1">
{isLoading ? <DataLoader /> : formatUsdValue(+equity)} {initialLoad ? <DataLoader /> : formatUsdValue(+equity)}
</div> </div>
</div> </div>
<div className="flex justify-between pb-3"> <div className="flex justify-between pb-3">
@ -172,7 +171,7 @@ export default function AccountInfo() {
{t('leverage')} {t('leverage')}
</div> </div>
<div className="text-th-fgd-1"> <div className="text-th-fgd-1">
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : mangoAccount ? ( ) : mangoAccount ? (
`${mangoAccount `${mangoAccount
@ -188,7 +187,7 @@ export default function AccountInfo() {
{t('collateral-available')} {t('collateral-available')}
</div> </div>
<div className={`text-th-fgd-1`}> <div className={`text-th-fgd-1`}>
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : mangoAccount ? ( ) : mangoAccount ? (
usdFormatter( usdFormatter(
@ -252,7 +251,7 @@ export default function AccountInfo() {
</div> </div>
</Tooltip> </Tooltip>
<div className={`flex items-center text-th-fgd-1`}> <div className={`flex items-center text-th-fgd-1`}>
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : mangoGroup ? ( ) : mangoGroup ? (
nativeToUi( nativeToUi(

View File

@ -21,10 +21,8 @@ import WithdrawModal from './WithdrawModal'
import { ExpandableRow } from './TableElements' import { ExpandableRow } from './TableElements'
import MobileTableHeader from './mobile/MobileTableHeader' import MobileTableHeader from './mobile/MobileTableHeader'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import useMangoAccount from '../hooks/useMangoAccount'
const BalancesTable = ({ showZeroBalances = false }) => { const BalancesTable = ({ showZeroBalances = false }) => {
useMangoAccount()
const { t } = useTranslation('common') const { t } = useTranslation('common')
const [showDepositModal, setShowDepositModal] = useState(false) const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false)

View File

@ -67,11 +67,10 @@ export default function MarketPosition() {
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
const mangoAccount = useMangoAccount() const { mangoAccount, initialLoad } = useMangoAccount()
const selectedMarket = useMangoStore((s) => s.selectedMarket.current) const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
const marketConfig = useMangoStore((s) => s.selectedMarket.config) const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const connected = useMangoStore((s) => s.wallet.connected) const connected = useMangoStore((s) => s.wallet.connected)
const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad)
const setMangoStore = useMangoStore((s) => s.set) const setMangoStore = useMangoStore((s) => s.set)
const price = useMangoStore((s) => s.tradeForm.price) const price = useMangoStore((s) => s.tradeForm.price)
const baseSymbol = marketConfig.baseSymbol const baseSymbol = marketConfig.baseSymbol
@ -164,7 +163,7 @@ export default function MarketPosition() {
) : null} ) : null}
<div className="flex items-center justify-between pb-3"> <div className="flex items-center justify-between pb-3">
<div className="font-normal text-th-fgd-3 leading-4">{t('size')}</div> <div className="font-normal text-th-fgd-3 leading-4">{t('size')}</div>
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : ( ) : (
<PerpSideBadge perpAccount={perpAccount}></PerpSideBadge> <PerpSideBadge perpAccount={perpAccount}></PerpSideBadge>
@ -175,7 +174,7 @@ export default function MarketPosition() {
{t('position-size')} {t('position-size')}
</div> </div>
<div className="text-th-fgd-1"> <div className="text-th-fgd-1">
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : basePosition ? ( ) : basePosition ? (
<span <span
@ -199,7 +198,7 @@ export default function MarketPosition() {
{t('notional-size')} {t('notional-size')}
</div> </div>
<div className="text-th-fgd-1"> <div className="text-th-fgd-1">
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : notionalSize ? ( ) : notionalSize ? (
formatUsdValue(Math.abs(notionalSize)) formatUsdValue(Math.abs(notionalSize))
@ -213,7 +212,7 @@ export default function MarketPosition() {
{t('average-entry')} {t('average-entry')}
</div> </div>
<div className="text-th-fgd-1"> <div className="text-th-fgd-1">
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : avgEntryPrice ? ( ) : avgEntryPrice ? (
formatUsdValue(avgEntryPrice) formatUsdValue(avgEntryPrice)
@ -227,7 +226,7 @@ export default function MarketPosition() {
{t('break-even')} {t('break-even')}
</div> </div>
<div className="text-th-fgd-1"> <div className="text-th-fgd-1">
{isLoading ? ( {initialLoad ? (
<DataLoader /> <DataLoader />
) : breakEvenPrice ? ( ) : breakEvenPrice ? (
formatUsdValue(breakEvenPrice) formatUsdValue(breakEvenPrice)
@ -243,7 +242,7 @@ export default function MarketPosition() {
</Tooltip.Content> </Tooltip.Content>
</Tooltip> </Tooltip>
<div className="flex items-center"> <div className="flex items-center">
{isLoading ? <DataLoader /> : <PnlText pnl={unsettledPnl} />} {initialLoad ? <DataLoader /> : <PnlText pnl={unsettledPnl} />}
{settling ? ( {settling ? (
<Loading className="ml-2" /> <Loading className="ml-2" />
) : ( ) : (

View File

@ -208,7 +208,7 @@ export default function Orderbook({ depth = 8 }) {
setOrderbookData(null) setOrderbookData(null)
} }
} }
}, 250) }, 500)
useEffect(() => { useEffect(() => {
lastOrderbookData.current = { lastOrderbookData.current = {

View File

@ -10,6 +10,7 @@ import TradeHistoryTable from './TradeHistoryTable'
import ManualRefresh from './ManualRefresh' import ManualRefresh from './ManualRefresh'
import Tabs from './Tabs' import Tabs from './Tabs'
import FeeDiscountsTable from './FeeDiscountsTable' import FeeDiscountsTable from './FeeDiscountsTable'
import useMangoAccount from '../hooks/useMangoAccount'
const TABS = [ const TABS = [
'Balances', 'Balances',
@ -22,8 +23,9 @@ const TABS = [
const UserInfoTabs = ({ activeTab, setActiveTab }) => { const UserInfoTabs = ({ activeTab, setActiveTab }) => {
const openOrders = useOpenOrders() const openOrders = useOpenOrders()
const { openPositions } = usePerpPositions() const { openPositions } = usePerpPositions()
const { mangoAccount } = useMangoAccount()
const connected = useMangoStore((s) => s.connection.current) const connected = useMangoStore((s) => s.connection.current)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const handleTabChange = (tabName) => { const handleTabChange = (tabName) => {
setActiveTab(tabName) setActiveTab(tabName)
} }

View File

@ -9,11 +9,12 @@ import useMangoStore from '../stores/useMangoStore'
import { i80f48ToPercent } from '../utils/index' import { i80f48ToPercent } from '../utils/index'
import { sumBy } from 'lodash' import { sumBy } from 'lodash'
import { I80F48 } from '@blockworks-foundation/mango-client/lib/src/fixednum' import { I80F48 } from '@blockworks-foundation/mango-client/lib/src/fixednum'
import useMangoAccount from './useMangoAccount'
export function useBalances(): Balances[] { export function useBalances(): Balances[] {
const balances = [] const balances = []
const { mangoAccount } = useMangoAccount()
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)

View File

@ -1,11 +1,19 @@
import useMangoStore from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import { MangoAccount } from '@blockworks-foundation/mango-client' import { MangoAccount } from '@blockworks-foundation/mango-client'
import shallow from 'zustand/shallow'
export default function useMangoAccount(): MangoAccount { export default function useMangoAccount(): {
const mangoAccount = useMangoStore( mangoAccount: MangoAccount
(state) => state.selectedMangoAccount.current, initialLoad: boolean
() => false } {
const { mangoAccount, initialLoad } = useMangoStore(
(state) => ({
mangoAccount: state.selectedMangoAccount.current,
lastUpdatedAt: state.selectedMangoAccount.lastUpdatedAt,
initialLoad: state.selectedMangoAccount.initialLoad,
}),
shallow
) )
return mangoAccount return { mangoAccount, initialLoad }
} }

View File

@ -79,7 +79,7 @@ export const collectPerpPosition = (
} }
const usePerpPositions = () => { const usePerpPositions = () => {
const mangoAccount = useMangoAccount() const { mangoAccount } = useMangoAccount()
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)

View File

@ -144,6 +144,7 @@ interface MangoStore extends State {
selectedMangoAccount: { selectedMangoAccount: {
current: MangoAccount | null current: MangoAccount | null
initialLoad: boolean initialLoad: boolean
lastUpdatedAt: number
} }
tradeForm: { tradeForm: {
side: 'buy' | 'sell' side: 'buy' | 'sell'
@ -226,6 +227,7 @@ const useMangoStore = create<MangoStore>((set, get) => {
selectedMangoAccount: { selectedMangoAccount: {
current: null, current: null,
initialLoad: true, initialLoad: true,
lastUpdatedAt: 0,
}, },
tradeForm: { tradeForm: {
side: 'buy', side: 'buy',
@ -468,9 +470,11 @@ const useMangoStore = create<MangoStore>((set, get) => {
connection, connection,
mangoClient.lastSlot mangoClient.lastSlot
) )
console.log('reloading mango account')
set((state) => { set((state) => {
state.selectedMangoAccount.current = reloadedMangoAccount state.selectedMangoAccount.current = reloadedMangoAccount
state.selectedMangoAccount.lastUpdatedAt = new Date().toISOString()
}) })
}, },
async reloadOrders() { async reloadOrders() {