From 9cb84df35c4eb3dd647526f6c621d40d32efb243 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Mon, 5 Sep 2022 07:25:33 -0700 Subject: [PATCH] chore: refactor global store (#1241) --- apps/trading/components/navbar/navbar.tsx | 13 +++--- .../risk-notice-dialog.spec.tsx | 2 +- .../risk-notice-dialog/risk-notice-dialog.tsx | 17 ++++---- .../vega-wallet-container.tsx | 4 +- apps/trading/pages/_app.page.tsx | 19 ++++----- apps/trading/pages/index.page.tsx | 12 +++--- .../trading/pages/markets/[marketId].page.tsx | 18 ++++++--- apps/trading/pages/markets/index.page.tsx | 4 +- apps/trading/pages/markets/trade-grid.tsx | 25 +++++++----- apps/trading/stores/global.ts | 40 +++++-------------- tools/executors/next/build/impl.ts | 6 ++- 11 files changed, 72 insertions(+), 88 deletions(-) diff --git a/apps/trading/components/navbar/navbar.tsx b/apps/trading/components/navbar/navbar.tsx index c2d0a2bac..57d748361 100644 --- a/apps/trading/components/navbar/navbar.tsx +++ b/apps/trading/components/navbar/navbar.tsx @@ -13,10 +13,11 @@ interface NavbarProps { } export const Navbar = ({ theme, toggleTheme }: NavbarProps) => { - const store = useGlobalStore(); - const tradingPath = store.marketId - ? `/markets/${store.marketId}` - : '/markets'; + const { marketId, update } = useGlobalStore((store) => ({ + marketId: store.marketId, + update: store.update, + })); + const tradingPath = marketId ? `/markets/${marketId}` : '/markets'; return (
@@ -48,9 +49,7 @@ export const Navbar = ({ theme, toggleTheme }: NavbarProps) => { fixedBg="dark" /> { - store.setVegaWalletConnectDialog(open); - }} + setConnectDialog={(open) => update({ connectDialog: open })} />
diff --git a/apps/trading/components/risk-notice-dialog/risk-notice-dialog.spec.tsx b/apps/trading/components/risk-notice-dialog/risk-notice-dialog.spec.tsx index d335afd13..280fa89d7 100644 --- a/apps/trading/components/risk-notice-dialog/risk-notice-dialog.spec.tsx +++ b/apps/trading/components/risk-notice-dialog/risk-notice-dialog.spec.tsx @@ -7,7 +7,7 @@ beforeEach(() => { localStorage.clear(); useGlobalStore.setState((state) => ({ ...state, - vegaRiskNoticeDialog: false, + riskNoticeDialog: false, })); }); diff --git a/apps/trading/components/risk-notice-dialog/risk-notice-dialog.tsx b/apps/trading/components/risk-notice-dialog/risk-notice-dialog.tsx index bcccfab5b..de3acecad 100644 --- a/apps/trading/components/risk-notice-dialog/risk-notice-dialog.tsx +++ b/apps/trading/components/risk-notice-dialog/risk-notice-dialog.tsx @@ -8,28 +8,27 @@ import { useGlobalStore } from '../../stores'; export const RISK_ACCEPTED_KEY = 'vega-risk-accepted'; export const RiskNoticeDialog = () => { - const store = useGlobalStore(); + const { riskNoticeDialog, update } = useGlobalStore((store) => ({ + riskNoticeDialog: store.riskNoticeDialog, + update: store.update, + })); const { VEGA_ENV } = useEnvironment(); useEffect(() => { const isRiskAccepted = LocalStorage.getItem(RISK_ACCEPTED_KEY) === 'true'; if (!isRiskAccepted && VEGA_ENV === Networks.MAINNET) { - store.setVegaRiskNoticeDialog(true); + update({ riskNoticeDialog: true }); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [store.setVegaRiskNoticeDialog, VEGA_ENV]); + }, [update, VEGA_ENV]); const handleAcceptRisk = () => { - store.setVegaRiskNoticeDialog(false); + update({ riskNoticeDialog: false }); LocalStorage.setItem(RISK_ACCEPTED_KEY, 'true'); }; return ( - +

{t('Regulation may apply to use of this app')}

diff --git a/apps/trading/components/vega-wallet-container/vega-wallet-container.tsx b/apps/trading/components/vega-wallet-container/vega-wallet-container.tsx index 4dd834652..b7be659e7 100644 --- a/apps/trading/components/vega-wallet-container/vega-wallet-container.tsx +++ b/apps/trading/components/vega-wallet-container/vega-wallet-container.tsx @@ -9,7 +9,7 @@ interface VegaWalletContainerProps { } export const VegaWalletContainer = ({ children }: VegaWalletContainerProps) => { - const store = useGlobalStore(); + const { update } = useGlobalStore((store) => ({ update: store.update })); const { keypair } = useVegaWallet(); if (!keypair) { @@ -20,7 +20,7 @@ export const VegaWalletContainer = ({ children }: VegaWalletContainerProps) => { {t('Connect your Vega wallet')}