From b66ae418a0ad1a76b3b8b94f325856af5f3f4384 Mon Sep 17 00:00:00 2001 From: sriram veeraghanta Date: Wed, 13 Dec 2023 18:44:51 +0530 Subject: [PATCH] fix: context changes --- ...root-provider.tsx => app-root.context.tsx} | 8 +- web/contexts/app-root/index.ts | 2 - web/contexts/app-root/use-app-root.tsx | 8 - web/contexts/page.context.tsx | 21 +++ web/contexts/page.context/index.ts | 2 - web/contexts/page.context/page-provider.tsx | 20 --- web/contexts/page.context/use-page.tsx | 8 - .../projects/[projectId]/pages/index.tsx | 151 +++++++++--------- 8 files changed, 105 insertions(+), 115 deletions(-) rename web/contexts/{app-root/app-root-provider.tsx => app-root.context.tsx} (71%) delete mode 100644 web/contexts/app-root/index.ts delete mode 100644 web/contexts/app-root/use-app-root.tsx create mode 100644 web/contexts/page.context.tsx delete mode 100644 web/contexts/page.context/index.ts delete mode 100644 web/contexts/page.context/page-provider.tsx delete mode 100644 web/contexts/page.context/use-page.tsx diff --git a/web/contexts/app-root/app-root-provider.tsx b/web/contexts/app-root.context.tsx similarity index 71% rename from web/contexts/app-root/app-root-provider.tsx rename to web/contexts/app-root.context.tsx index 92a3f73ef..48ce0a411 100644 --- a/web/contexts/app-root/app-root-provider.tsx +++ b/web/contexts/app-root.context.tsx @@ -1,4 +1,4 @@ -import { createContext } from "react"; +import { createContext, useContext } from "react"; // mobx store import { AppRootStore, IAppRootStore } from "store/application"; @@ -17,3 +17,9 @@ export const AppRootStoreProvider = ({ children }: any) => { const store: IAppRootStore = initializeStore(); return {children}; }; + +export const useAppRootStore = () => { + const context = useContext(AppRootStoreContext); + if (context === undefined) throw new Error("useMobxStore must be used within MobxStoreProvider"); + return context; +}; diff --git a/web/contexts/app-root/index.ts b/web/contexts/app-root/index.ts deleted file mode 100644 index 416a8ec63..000000000 --- a/web/contexts/app-root/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./app-root-provider"; -export * from "./use-app-root"; diff --git a/web/contexts/app-root/use-app-root.tsx b/web/contexts/app-root/use-app-root.tsx deleted file mode 100644 index 363192716..000000000 --- a/web/contexts/app-root/use-app-root.tsx +++ /dev/null @@ -1,8 +0,0 @@ -import { useContext } from "react"; -import { AppRootStoreContext } from "./app-root-provider"; - -export const useAppRoot = () => { - const context = useContext(AppRootStoreContext); - if (context === undefined) throw new Error("useAppRoot must be used within AppRootStoreContext"); - return context; -}; diff --git a/web/contexts/page.context.tsx b/web/contexts/page.context.tsx new file mode 100644 index 000000000..cb83a687c --- /dev/null +++ b/web/contexts/page.context.tsx @@ -0,0 +1,21 @@ +import { createContext, useContext } from "react"; +// mobx store +import { PageStore } from "store/page.store"; +import { AppRootStore } from "store/application"; +import { useAppRootStore } from "./app-root.context"; + +export const PageContext = createContext(undefined); + +let pageStore: PageStore | undefined; + +export const PageStoreProvider = ({ children }: any) => { + const appRootStore = useAppRootStore(); + pageStore = pageStore ?? new PageStore(appRootStore); + return {children}; +}; + +export const usePage = () => { + const context = useContext(PageContext); + if (context === undefined) throw new Error("usePage must be used within AppRootStoreContext"); + return context; +}; diff --git a/web/contexts/page.context/index.ts b/web/contexts/page.context/index.ts deleted file mode 100644 index 78f8c7a77..000000000 --- a/web/contexts/page.context/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./page-provider"; -export * from "./use-page"; diff --git a/web/contexts/page.context/page-provider.tsx b/web/contexts/page.context/page-provider.tsx deleted file mode 100644 index 5026edabc..000000000 --- a/web/contexts/page.context/page-provider.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import { createContext } from "react"; -// mobx store -import { PageStore } from "store/page.store"; -import { AppRootStore } from "store/application"; - -let pageStore: PageStore = new PageStore(new AppRootStore()); - -export const PageContext = createContext(pageStore); - -const initializeStore = () => { - const _pageStore: PageStore = pageStore ?? new PageStore(pageStore); - if (typeof window === "undefined") return _pageStore; - if (!pageStore) pageStore = _pageStore; - return _pageStore; -}; - -export const AppRootStoreProvider = ({ children }: any) => { - const store: PageStore = initializeStore(); - return {children}; -}; diff --git a/web/contexts/page.context/use-page.tsx b/web/contexts/page.context/use-page.tsx deleted file mode 100644 index 85e3d1500..000000000 --- a/web/contexts/page.context/use-page.tsx +++ /dev/null @@ -1,8 +0,0 @@ -import { useContext } from "react"; -import { PageContext } from "./page-provider"; - -export const usePage = () => { - const context = useContext(PageContext); - if (context === undefined) throw new Error("useAppRoot must be used within AppRootStoreContext"); - return context; -}; diff --git a/web/pages/[workspaceSlug]/projects/[projectId]/pages/index.tsx b/web/pages/[workspaceSlug]/projects/[projectId]/pages/index.tsx index ff035be63..1f5b18ba9 100644 --- a/web/pages/[workspaceSlug]/projects/[projectId]/pages/index.tsx +++ b/web/pages/[workspaceSlug]/projects/[projectId]/pages/index.tsx @@ -17,6 +17,7 @@ import { PagesHeader } from "components/headers"; import { NextPageWithLayout } from "types/app"; // constants import { PAGE_TABS_LIST } from "constants/page"; +import { PageStoreProvider } from "contexts/page.context"; const AllPagesList = dynamic(() => import("components/pages").then((a) => a.AllPagesList), { ssr: false, @@ -82,81 +83,83 @@ const ProjectPagesPage: NextPageWithLayout = observer(() => { }; return ( - <> - {workspaceSlug && projectId && ( - setCreateUpdatePageModal(false)} - projectId={projectId.toString()} - /> - )} -
-
-

Pages

+ + <> + {workspaceSlug && projectId && ( + setCreateUpdatePageModal(false)} + projectId={projectId.toString()} + /> + )} +
+
+

Pages

+
+ { + switch (i) { + case 0: + return setPageTab("Recent"); + case 1: + return setPageTab("All"); + case 2: + return setPageTab("Favorites"); + case 3: + return setPageTab("Private"); + case 4: + return setPageTab("Shared"); + case 5: + return setPageTab("Archived"); + default: + return setPageTab("All"); + } + }} + > + +
+ {PAGE_TABS_LIST.map((tab) => ( + + `rounded-full border px-5 py-1.5 text-sm outline-none ${ + selected + ? "border-custom-primary bg-custom-primary text-white" + : "border-custom-border-200 bg-custom-background-100 hover:bg-custom-background-90" + }` + } + > + {tab.title} + + ))} +
+
+ + + + + + + + + + + + + + + + + + + + +
- { - switch (i) { - case 0: - return setPageTab("Recent"); - case 1: - return setPageTab("All"); - case 2: - return setPageTab("Favorites"); - case 3: - return setPageTab("Private"); - case 4: - return setPageTab("Shared"); - case 5: - return setPageTab("Archived"); - default: - return setPageTab("All"); - } - }} - > - -
- {PAGE_TABS_LIST.map((tab) => ( - - `rounded-full border px-5 py-1.5 text-sm outline-none ${ - selected - ? "border-custom-primary bg-custom-primary text-white" - : "border-custom-border-200 bg-custom-background-100 hover:bg-custom-background-90" - }` - } - > - {tab.title} - - ))} -
-
- - - - - - - - - - - - - - - - - - - - -
-
- + + ); });