From d5d6c99743dbebbd3f68867c28a8d9a63face886 Mon Sep 17 00:00:00 2001 From: tjs Date: Thu, 31 Aug 2023 22:23:53 -0400 Subject: [PATCH] move width and height into mangostore --- components/MangoProvider.tsx | 17 ++++++++++++++++- components/TopBar.tsx | 4 +--- components/trade/TradeInfoTabs.tsx | 3 +-- hooks/useViewport.tsx | 25 +++++++++---------------- store/mangoStore.ts | 9 ++++++++- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/components/MangoProvider.tsx b/components/MangoProvider.tsx index 1df93315..00bf97fe 100644 --- a/components/MangoProvider.tsx +++ b/components/MangoProvider.tsx @@ -1,4 +1,4 @@ -import { useEffect } from 'react' +import { useCallback, useEffect } from 'react' import mangoStore from '@store/mangoStore' import { Keypair, PublicKey } from '@solana/web3.js' import { useRouter } from 'next/router' @@ -23,6 +23,21 @@ const HydrateStore = () => { const [, setLastWalletName] = useLocalStorageState(LAST_WALLET_NAME, '') + const handleWindowResize = useCallback(() => { + if (typeof window !== 'undefined') { + set((s) => { + s.window.width = window.innerWidth + s.window.height = window.innerHeight + }) + } + }, []) + // store the window width and height on resize + useEffect(() => { + handleWindowResize() + window.addEventListener('resize', handleWindowResize) + return () => window.removeEventListener('resize', handleWindowResize) + }, [handleWindowResize]) + useEffect(() => { if (wallet?.adapter) { setLastWalletName(wallet?.adapter.name) diff --git a/components/TopBar.tsx b/components/TopBar.tsx index 3210c0ca..91861188 100644 --- a/components/TopBar.tsx +++ b/components/TopBar.tsx @@ -21,7 +21,6 @@ import useOnlineStatus from 'hooks/useOnlineStatus' import { abbreviateAddress } from 'utils/formatting' import DepositWithdrawModal from './modals/DepositWithdrawModal' import { useViewport } from 'hooks/useViewport' -import { breakpoints } from 'utils/theme' import AccountsButton from './AccountsButton' import useUnownedAccount from 'hooks/useUnownedAccount' import NotificationsButton from './notifications/NotificationsButton' @@ -50,8 +49,7 @@ const TopBar = () => { const router = useRouter() const { query } = router - const { width } = useViewport() - const isMobile = width ? width < breakpoints.sm : false + const { isMobile } = useViewport() const { isUnownedAccount } = useUnownedAccount() const showUserSetup = mangoStore((s) => s.showUserSetup) diff --git a/components/trade/TradeInfoTabs.tsx b/components/trade/TradeInfoTabs.tsx index ce8fdac4..378f5bb7 100644 --- a/components/trade/TradeInfoTabs.tsx +++ b/components/trade/TradeInfoTabs.tsx @@ -20,8 +20,7 @@ const TradeInfoTabs = () => { const unsettledSpotBalances = useUnsettledSpotBalances() const unsettledPerpPositions = useUnsettledPerpPositions() const openPerpPositions = useOpenPerpPositions() - const { width } = useViewport() - const isMobile = width ? width < breakpoints.md : false + const { isMobile, width } = useViewport() const fillTabWidth = width ? width < breakpoints['2xl'] : false useEffect(() => { diff --git a/hooks/useViewport.tsx b/hooks/useViewport.tsx index 9aea9ed9..a498ded2 100644 --- a/hooks/useViewport.tsx +++ b/hooks/useViewport.tsx @@ -1,21 +1,14 @@ -import { useCallback, useEffect, useState } from 'react' +import mangoStore from '@store/mangoStore' +import { useMemo } from 'react' +import { breakpoints } from 'utils/theme' export const useViewport = () => { - const [width, setWidth] = useState(0) - const [height, setHeight] = useState(0) + const width = mangoStore((s) => s.window.width) + const height = mangoStore((s) => s.window.height) - const handleWindowResize = useCallback(() => { - if (typeof window !== 'undefined') { - setWidth(window.innerWidth) - setHeight(window.innerHeight) - } - }, []) + const isMobile = useMemo(() => { + return width ? width < breakpoints.sm : false + }, [width]) - useEffect(() => { - handleWindowResize() - window.addEventListener('resize', handleWindowResize) - return () => window.removeEventListener('resize', handleWindowResize) - }, [handleWindowResize]) - - return { width, height } + return { width, height, isMobile } } diff --git a/store/mangoStore.ts b/store/mangoStore.ts index eae041b3..d4d98fe0 100644 --- a/store/mangoStore.ts +++ b/store/mangoStore.ts @@ -45,7 +45,6 @@ import { MAX_PRIORITY_FEE_KEYS, OUTPUT_TOKEN_DEFAULT, PAGINATION_PAGE_LENGTH, - PRIORITY_FEE_KEY, RPC_PROVIDER_KEY, SWAP_MARGIN_KEY, } from '../utils/constants' @@ -253,6 +252,10 @@ export type MangoStore = { loading: boolean } } + window: { + width: number + height: number + } actions: { fetchAccountInterestTotals: (mangoAccountPk: string) => Promise fetchActivityFeed: ( @@ -421,6 +424,10 @@ const mangoStore = create()( loading: false, }, }, + window: { + width: 0, + height: 0, + }, actions: { fetchAccountInterestTotals: async (mangoAccountPk: string) => { const set = get().set