diff --git a/app/src/components/AppContextProvider.tsx b/app/src/components/AppContextProvider.tsx index 55dc08b4a..88b799388 100644 --- a/app/src/components/AppContextProvider.tsx +++ b/app/src/components/AppContextProvider.tsx @@ -34,16 +34,12 @@ const defaultLanguage = Object.keys(languages).includes(browserLanguage) ? browserLanguage : "en"; -type mobileEditorTab = "text" | "graph"; - type TAppContext = { updateUserSettings: (newSettings: Partial) => void; theme: Theme; language: string; shareModal: boolean; setShareModal: Dispatch>; - mobileEditorTab: mobileEditorTab; - toggleMobileEditorTab: () => void; session: Session | null; customer?: CustomerInfo; customerIsLoading: boolean; @@ -64,15 +60,6 @@ const Provider = ({ children }: { children?: ReactNode }) => { LOCAL_STORAGE_SETTINGS_KEY, "{}" ); - const [mobileEditorTab, setMobileEditorTab] = - useState("text"); - const toggleMobileEditorTab = useCallback( - () => - setMobileEditorTab((currentTab) => - currentTab === "text" ? "graph" : "text" - ), - [] - ); const { settings, theme } = useMemo<{ settings: UserSettings; @@ -157,8 +144,6 @@ const Provider = ({ children }: { children?: ReactNode }) => { updateUserSettings, setShareModal, shareModal, - mobileEditorTab, - toggleMobileEditorTab, session, customer, customerIsLoading, diff --git a/app/src/components/EditWrapper.tsx b/app/src/components/EditWrapper.tsx index 2db9ba5e4..46f3bdf53 100644 --- a/app/src/components/EditWrapper.tsx +++ b/app/src/components/EditWrapper.tsx @@ -1,22 +1,22 @@ -import { ReactNode, useContext } from "react"; +import { ReactNode } from "react"; import { useFullscreen } from "../lib/hooks"; import { Box } from "../slang"; -import { AppContext } from "./AppContextProvider"; import styles from "./EditWrapper.module.css"; import MobileTabToggle from "./MobileTabToggle"; +import { useMobileStore } from "../lib/useMobileStore"; /** * Adds the wrapper for the toggle between the editor and the graph * on mobile. */ export function EditWrapper({ children }: { children: ReactNode }) { - const { mobileEditorTab } = useContext(AppContext); + const tab = useMobileStore((state) => state.tab); const isFullscreen = useFullscreen(); return ( {children} diff --git a/app/src/components/MobileTabToggle.tsx b/app/src/components/MobileTabToggle.tsx index 2ae7176a4..6c3034ca0 100644 --- a/app/src/components/MobileTabToggle.tsx +++ b/app/src/components/MobileTabToggle.tsx @@ -1,11 +1,11 @@ import { t } from "@lingui/macro"; -import { useContext } from "react"; import { Box } from "../slang"; -import { AppContext } from "./AppContextProvider"; +import { useMobileStore } from "../lib/useMobileStore"; export default function MobileTabToggle() { - const { toggleMobileEditorTab, mobileEditorTab } = useContext(AppContext); + const tab = useMobileStore((state) => state.tab); + const toggleTab = useMobileStore((state) => state.toggleTab); return ( - {mobileEditorTab === "graph" ? t`Editor` : t`Graph`} + {tab === "graph" ? t`Editor` : t`Graph`} diff --git a/app/src/lib/useMobileStore.ts b/app/src/lib/useMobileStore.ts new file mode 100644 index 000000000..069bb67a9 --- /dev/null +++ b/app/src/lib/useMobileStore.ts @@ -0,0 +1,27 @@ +import { create } from "zustand"; +import { persist } from "zustand/middleware"; + +export type MobileEditorTab = "text" | "graph"; + +export type MobileStore = { + tab: MobileEditorTab; + toggleTab: () => void; +}; + +/** + * Stores client-side state related to the editor. + */ +export const useMobileStore = create()( + persist( + (set) => ({ + tab: "text", + toggleTab: () => + set((state) => ({ + tab: state.tab === "text" ? "graph" : "text", + })), + }), + { + name: "ff-mobile-store", + } + ) +);