Skip to content

Commit

Permalink
Move mobile tab state into zustand
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-gordon committed Jan 2, 2024
1 parent fed1d2e commit 434d081
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
15 changes: 0 additions & 15 deletions app/src/components/AppContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ const defaultLanguage = Object.keys(languages).includes(browserLanguage)
? browserLanguage
: "en";

type mobileEditorTab = "text" | "graph";

type TAppContext = {
updateUserSettings: (newSettings: Partial<UserSettings>) => void;
theme: Theme;
language: string;
shareModal: boolean;
setShareModal: Dispatch<SetStateAction<boolean>>;
mobileEditorTab: mobileEditorTab;
toggleMobileEditorTab: () => void;
session: Session | null;
customer?: CustomerInfo;
customerIsLoading: boolean;
Expand All @@ -64,15 +60,6 @@ const Provider = ({ children }: { children?: ReactNode }) => {
LOCAL_STORAGE_SETTINGS_KEY,
"{}"
);
const [mobileEditorTab, setMobileEditorTab] =
useState<mobileEditorTab>("text");
const toggleMobileEditorTab = useCallback(
() =>
setMobileEditorTab((currentTab) =>
currentTab === "text" ? "graph" : "text"
),
[]
);

const { settings, theme } = useMemo<{
settings: UserSettings;
Expand Down Expand Up @@ -157,8 +144,6 @@ const Provider = ({ children }: { children?: ReactNode }) => {
updateUserSettings,
setShareModal,
shareModal,
mobileEditorTab,
toggleMobileEditorTab,
session,
customer,
customerIsLoading,
Expand Down
8 changes: 4 additions & 4 deletions app/src/components/EditWrapper.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Box
as="main"
className={styles.EditWrapper}
data-mobile-tab={mobileEditorTab}
data-mobile-tab={tab}
template="[main] minmax(0, 1fr) auto / [main] minmax(0, 1fr)"
>
{children}
Expand Down
10 changes: 5 additions & 5 deletions app/src/components/MobileTabToggle.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Box p={1} at={{ tablet: { display: "none" } }}>
<Box
Expand All @@ -14,10 +14,10 @@ export default function MobileTabToggle() {
color="palette-white-0"
rad={1}
p={3}
onClick={toggleMobileEditorTab}
onClick={toggleTab}
>
<span className="text-xs">
{mobileEditorTab === "graph" ? t`Editor` : t`Graph`}
{tab === "graph" ? t`Editor` : t`Graph`}
</span>
</Box>
</Box>
Expand Down
27 changes: 27 additions & 0 deletions app/src/lib/useMobileStore.ts
Original file line number Diff line number Diff line change
@@ -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<MobileStore>()(
persist(
(set) => ({
tab: "text",
toggleTab: () =>
set((state) => ({
tab: state.tab === "text" ? "graph" : "text",
})),
}),
{
name: "ff-mobile-store",
}
)
);

0 comments on commit 434d081

Please sign in to comment.