From 2b6735b5f891e9228ba452fa1843cb8c320217e1 Mon Sep 17 00:00:00 2001 From: jaybuidl Date: Mon, 9 Oct 2023 17:45:24 +0100 Subject: [PATCH] refactor: extracted some props to a context --- web/src/app.tsx | 27 ++++++----- web/src/context/IsListProvider.tsx | 2 +- web/src/context/MenuProvider.tsx | 47 +++++++++++++++++++ web/src/layout/Header/DesktopHeader.tsx | 14 +++--- web/src/layout/Header/navbar/DappList.tsx | 8 ++-- web/src/layout/Header/navbar/Menu/Help.tsx | 5 +- .../FormContactDetails/index.tsx | 5 +- .../Menu/Settings/Notifications/index.tsx | 6 +-- .../Header/navbar/Menu/Settings/index.tsx | 7 +-- web/src/layout/Header/navbar/Menu/index.tsx | 5 +- web/src/layout/Header/navbar/index.tsx | 26 +++------- 11 files changed, 94 insertions(+), 58 deletions(-) create mode 100644 web/src/context/MenuProvider.tsx diff --git a/web/src/app.tsx b/web/src/app.tsx index a1f35108d..34bfd469f 100644 --- a/web/src/app.tsx +++ b/web/src/app.tsx @@ -14,6 +14,7 @@ import Cases from "./pages/Cases"; import Dashboard from "./pages/Dashboard"; import Courts from "./pages/Courts"; import DisputeTemplateView from "./pages/DisputeTemplateView"; +import MenuProvider from "./context/MenuProvider"; const App: React.FC = () => { return ( @@ -21,18 +22,20 @@ const App: React.FC = () => { - - - }> - } /> - } /> - } /> - } /> - } /> - Justice not found here ¯\_( ͡° ͜ʖ ͡°)_/¯} /> - - - + + + + }> + } /> + } /> + } /> + } /> + } /> + Justice not found here ¯\_( ͡° ͜ʖ ͡°)_/¯} /> + + + + diff --git a/web/src/context/IsListProvider.tsx b/web/src/context/IsListProvider.tsx index 1525f05a5..36b70d429 100644 --- a/web/src/context/IsListProvider.tsx +++ b/web/src/context/IsListProvider.tsx @@ -9,7 +9,7 @@ interface IIsListProvider { const Context = createContext({ isList: false, setIsList: () => { - // + // nop }, }); diff --git a/web/src/context/MenuProvider.tsx b/web/src/context/MenuProvider.tsx new file mode 100644 index 000000000..e2a783ccc --- /dev/null +++ b/web/src/context/MenuProvider.tsx @@ -0,0 +1,47 @@ +import React, { createContext, useContext } from "react"; +import { useToggle } from "react-use"; + +interface IMenuProvider { + isDappListOpen: boolean; + toggleIsDappListOpen: (nextValue?: any) => void; + + isHelpOpen: boolean; + toggleIsHelpOpen: (nextValue?: any) => void; + + isSettingsOpen: boolean; + toggleIsSettingsOpen: (nextValue?: any) => void; +} + +const nop = () => { + // nop +}; + +const Context = createContext({ + isDappListOpen: false, + toggleIsDappListOpen: nop, + isHelpOpen: false, + toggleIsHelpOpen: nop, + isSettingsOpen: false, + toggleIsSettingsOpen: nop, +}); + +const MenuProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) => { + const [isDappListOpen, toggleIsDappListOpen] = useToggle(false); + const [isHelpOpen, toggleIsHelpOpen] = useToggle(false); + const [isSettingsOpen, toggleIsSettingsOpen] = useToggle(false); + + const value = { + isDappListOpen, + toggleIsDappListOpen, + isHelpOpen, + toggleIsHelpOpen, + isSettingsOpen, + toggleIsSettingsOpen, + }; + + return {children}; +}; + +export const useMenu = () => useContext(Context); + +export default MenuProvider; diff --git a/web/src/layout/Header/DesktopHeader.tsx b/web/src/layout/Header/DesktopHeader.tsx index d118fa85b..7ccd96c93 100644 --- a/web/src/layout/Header/DesktopHeader.tsx +++ b/web/src/layout/Header/DesktopHeader.tsx @@ -1,7 +1,7 @@ import React from "react"; import styled, { css } from "styled-components"; +import { useMenu } from "../../context/MenuProvider"; import { landscapeStyle } from "styles/landscapeStyle"; -import { useToggle } from "react-use"; import { Link } from "react-router-dom"; import { useLockOverlayScroll } from "hooks/useLockOverlayScroll"; import KlerosSolutionsIcon from "svgs/menu-icons/kleros-solutions.svg"; @@ -76,9 +76,7 @@ const ConnectWalletContainer = styled.div` `; const DesktopHeader = () => { - const [isDappListOpen, toggleIsDappListOpen] = useToggle(false); - const [isHelpOpen, toggleIsHelpOpen] = useToggle(false); - const [isSettingsOpen, toggleIsSettingsOpen] = useToggle(false); + const { isDappListOpen, toggleIsDappListOpen, isSettingsOpen, isHelpOpen } = useMenu(); useLockOverlayScroll(isDappListOpen || isHelpOpen || isSettingsOpen); return ( @@ -107,15 +105,15 @@ const DesktopHeader = () => { - + {(isDappListOpen || isHelpOpen || isSettingsOpen) && ( - {isDappListOpen && } - {isHelpOpen && } - {isSettingsOpen && } + {isDappListOpen && } + {isHelpOpen && } + {isSettingsOpen && } )} diff --git a/web/src/layout/Header/navbar/DappList.tsx b/web/src/layout/Header/navbar/DappList.tsx index 17c231f0d..a4aadca1e 100644 --- a/web/src/layout/Header/navbar/DappList.tsx +++ b/web/src/layout/Header/navbar/DappList.tsx @@ -12,6 +12,7 @@ import POH from "svgs/icons/poh-image.png"; import Vea from "svgs/icons/vea.svg"; import Tokens from "svgs/icons/tokens.svg"; import Product from "./Product"; +import { useMenu } from "../../../context/MenuProvider"; const Header = styled.h1` display: flex; @@ -117,12 +118,9 @@ const ITEMS = [ }, ]; -interface IDappList { - toggleIsDappListOpen: () => void; -} - -const DappList: React.FC = ({ toggleIsDappListOpen }) => { +const DappList: React.FC = () => { const containerRef = useRef(null); + const { toggleIsDappListOpen } = useMenu(); useFocusOutside(containerRef, () => { toggleIsDappListOpen(); }); diff --git a/web/src/layout/Header/navbar/Menu/Help.tsx b/web/src/layout/Header/navbar/Menu/Help.tsx index c1bf4905d..49eddeefb 100644 --- a/web/src/layout/Header/navbar/Menu/Help.tsx +++ b/web/src/layout/Header/navbar/Menu/Help.tsx @@ -8,7 +8,7 @@ import Bug from "svgs/icons/bug.svg"; import ETH from "svgs/icons/eth.svg"; import Faq from "svgs/menu-icons/help.svg"; import Telegram from "svgs/socialmedia/telegram.svg"; -import { IHelp } from ".."; +import { useMenu } from "../../../../context/MenuProvider"; const Container = styled.div` display: flex; @@ -97,8 +97,9 @@ const ITEMS = [ }, ]; -const Help: React.FC = ({ toggleIsHelpOpen }) => { +const Help: React.FC = () => { const containerRef = useRef(null); + const { toggleIsHelpOpen } = useMenu(); useFocusOutside(containerRef, () => { toggleIsHelpOpen(); }); diff --git a/web/src/layout/Header/navbar/Menu/Settings/Notifications/FormContactDetails/index.tsx b/web/src/layout/Header/navbar/Menu/Settings/Notifications/FormContactDetails/index.tsx index 7bed3fa07..30b1f7625 100644 --- a/web/src/layout/Header/navbar/Menu/Settings/Notifications/FormContactDetails/index.tsx +++ b/web/src/layout/Header/navbar/Menu/Settings/Notifications/FormContactDetails/index.tsx @@ -6,7 +6,7 @@ import { uploadSettingsToSupabase } from "utils/uploadSettingsToSupabase"; import FormContact from "./FormContact"; import messages from "../../../../../../../consts/eip712-messages"; import { EMAIL_REGEX, TELEGRAM_REGEX } from "../../../../../../../consts/index"; -import { ISettings } from "../../../index"; +import { useMenu } from "../../../../../../../context/MenuProvider"; const FormContainer = styled.form` position: relative; @@ -27,13 +27,14 @@ const FormContactContainer = styled.div` margin-bottom: 24px; `; -const FormContactDetails: React.FC = ({ toggleIsSettingsOpen }) => { +const FormContactDetails: React.FC = () => { const [telegramInput, setTelegramInput] = useState(""); const [emailInput, setEmailInput] = useState(""); const [telegramIsValid, setTelegramIsValid] = useState(false); const [emailIsValid, setEmailIsValid] = useState(false); const { data: walletClient } = useWalletClient(); const { address } = useAccount(); + const { toggleIsSettingsOpen } = useMenu(); // TODO: after the user is authenticated, retrieve the current email/telegram from the database and populate the form diff --git a/web/src/layout/Header/navbar/Menu/Settings/Notifications/index.tsx b/web/src/layout/Header/navbar/Menu/Settings/Notifications/index.tsx index f2d1a4c94..e9ad8f281 100644 --- a/web/src/layout/Header/navbar/Menu/Settings/Notifications/index.tsx +++ b/web/src/layout/Header/navbar/Menu/Settings/Notifications/index.tsx @@ -1,9 +1,9 @@ import React from "react"; import styled from "styled-components"; -import { ISettings } from "../../../index"; import FormContactDetails from "./FormContactDetails"; import { EnsureChain } from "components/EnsureChain"; +import { useMenu } from "../../../../../../context/MenuProvider"; const Container = styled.div` display: flex; @@ -33,13 +33,13 @@ const EnsureChainContainer = styled.div` padding-bottom: 16px; `; -const NotificationSettings: React.FC = ({ toggleIsSettingsOpen }) => { +const NotificationSettings: React.FC = () => { return ( - + diff --git a/web/src/layout/Header/navbar/Menu/Settings/index.tsx b/web/src/layout/Header/navbar/Menu/Settings/index.tsx index 9a2726bb3..33c8ea847 100644 --- a/web/src/layout/Header/navbar/Menu/Settings/index.tsx +++ b/web/src/layout/Header/navbar/Menu/Settings/index.tsx @@ -6,7 +6,7 @@ import General from "./General"; import NotificationSettings from "./Notifications"; import { useFocusOutside } from "hooks/useFocusOutside"; import { Overlay } from "components/Overlay"; -import { ISettings } from "../../index"; +import { useMenu } from "../../../../../context/MenuProvider"; const Container = styled.div` display: flex; @@ -60,9 +60,10 @@ const TABS = [ }, ]; -const Settings: React.FC = ({ toggleIsSettingsOpen }) => { +const Settings: React.FC = () => { const [currentTab, setCurrentTab] = useState(0); const containerRef = useRef(null); + const { toggleIsSettingsOpen } = useMenu(); useFocusOutside(containerRef, () => toggleIsSettingsOpen()); return ( @@ -77,7 +78,7 @@ const Settings: React.FC = ({ toggleIsSettingsOpen }) => { setCurrentTab(n); }} /> - {currentTab === 0 ? : } + {currentTab === 0 ? : } ); diff --git a/web/src/layout/Header/navbar/Menu/index.tsx b/web/src/layout/Header/navbar/Menu/index.tsx index fb12c6e50..c83898c15 100644 --- a/web/src/layout/Header/navbar/Menu/index.tsx +++ b/web/src/layout/Header/navbar/Menu/index.tsx @@ -8,7 +8,7 @@ import LightModeIcon from "svgs/menu-icons/light-mode.svg"; import NotificationsIcon from "svgs/menu-icons/notifications.svg"; import SettingsIcon from "svgs/menu-icons/settings.svg"; import { useToggleTheme } from "hooks/useToggleThemeContext"; -import { IHelp, ISettings } from ".."; +import { useMenu } from "../../../../context/MenuProvider"; const Container = styled.div` display: flex; @@ -53,8 +53,9 @@ const ButtonContainer = styled.div` )} `; -const Menu: React.FC = ({ toggleIsHelpOpen, toggleIsSettingsOpen }) => { +const Menu: React.FC = () => { const [theme, toggleTheme] = useToggleTheme(); + const { toggleIsSettingsOpen, toggleIsHelpOpen } = useMenu(); const isLightTheme = theme === "light"; const buttons = [ diff --git a/web/src/layout/Header/navbar/index.tsx b/web/src/layout/Header/navbar/index.tsx index 5faf54147..270c08064 100644 --- a/web/src/layout/Header/navbar/index.tsx +++ b/web/src/layout/Header/navbar/index.tsx @@ -1,6 +1,6 @@ import React from "react"; import styled from "styled-components"; -import { useToggle } from "react-use"; +import { useMenu } from "../../../context/MenuProvider"; import { useAccount } from "wagmi"; import { useLockOverlayScroll } from "hooks/useLockOverlayScroll"; import { useOpenContext } from "../MobileHeader"; @@ -53,23 +53,9 @@ const DisconnectWalletButtonContainer = styled.div` align-items: center; `; -export interface ISettings { - toggleIsSettingsOpen: () => void; -} - -export interface IHelp { - toggleIsHelpOpen: () => void; -} - -export interface IDappList { - toggleIsDappListOpen: () => void; -} - const NavBar: React.FC = () => { const { isConnected } = useAccount(); - const [isDappListOpen, toggleIsDappListOpen] = useToggle(false); - const [isHelpOpen, toggleIsHelpOpen] = useToggle(false); - const [isSettingsOpen, toggleIsSettingsOpen] = useToggle(false); + const { isDappListOpen, toggleIsDappListOpen, isSettingsOpen, isHelpOpen } = useMenu(); const { isOpen } = useOpenContext(); useLockOverlayScroll(isOpen); @@ -95,16 +81,16 @@ const NavBar: React.FC = () => { )}
- +
{(isDappListOpen || isHelpOpen || isSettingsOpen) && ( - {isDappListOpen && } - {isHelpOpen && } - {isSettingsOpen && } + {isDappListOpen && } + {isHelpOpen && } + {isSettingsOpen && } )}