plane/apps/app/lib/mobx/store-provider.tsx
guru_sainath b6744dcd29
Chore: mobx setup and app sidebar and theme management (#1798)
* dev: Mobx integration for app sidebar and custom theme

* dev: Handled edge case and conditional rendering for mobx store
2023-08-08 12:50:27 +05:30

31 lines
798 B
TypeScript

import { createContext, useContext } from "react";
// mobx store
import { RootStore } from "store/root";
let rootStore: any = null;
export const MobxStoreContext = createContext(null);
const initializeStore = () => {
const _rootStore = rootStore ?? new RootStore();
if (typeof window === "undefined") return _rootStore;
if (!rootStore) rootStore = _rootStore;
return _rootStore;
};
export const MobxStoreProvider = ({ children }: any) => {
const store = initializeStore();
return <MobxStoreContext.Provider value={store}>{children}</MobxStoreContext.Provider>;
};
// hook
export const useMobxStore = () => {
const context = useContext(MobxStoreContext);
if (context === undefined) throw new Error("useMobxStore must be used within MobxStoreProvider");
return context;
};