fix: adding new contexts and providers

This commit is contained in:
sriram veeraghanta 2023-12-13 02:03:29 +05:30
parent f3748d09a5
commit 08b848c7d8
15 changed files with 95 additions and 48 deletions

View File

@ -0,0 +1,19 @@
import { createContext } from "react";
// mobx store
import { AppRootStore, IAppRootStore } from "store/application";
let appRootStore: IAppRootStore = new AppRootStore();
export const AppRootStoreContext = createContext<IAppRootStore>(appRootStore);
const initializeStore = () => {
const _appRootStore: IAppRootStore = appRootStore ?? new AppRootStore();
if (typeof window === "undefined") return _appRootStore;
if (!appRootStore) appRootStore = _appRootStore;
return _appRootStore;
};
export const AppRootStoreProvider = ({ children }: any) => {
const store: IAppRootStore = initializeStore();
return <AppRootStoreContext.Provider value={store}>{children}</AppRootStoreContext.Provider>;
};

View File

@ -0,0 +1,2 @@
export * from "./app-root-provider";
export * from "./use-app-root";

View File

@ -0,0 +1,8 @@
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;
};

View File

@ -0,0 +1,2 @@
export * from "./page-provider";
export * from "./use-page";

View File

@ -0,0 +1,20 @@
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>(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 <PageContext.Provider value={store}>{children}</PageContext.Provider>;
};

View File

@ -0,0 +1,8 @@
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;
};

8
web/hooks/use-page.tsx Normal file
View File

@ -0,0 +1,8 @@
import { useMobxStore } from "lib/mobx/store-provider";
const usePage = () => {
const { page } = useMobxStore();
return { ...page };
};
export default usePage;

View File

@ -1,5 +1,3 @@
"use client";
import { createContext, useContext } from "react"; import { createContext, useContext } from "react";
// mobx store // mobx store
import { RootStore } from "store_legacy/root"; import { RootStore } from "store_legacy/root";

View File

@ -82,7 +82,7 @@ const PageDetailsPage: NextPageWithLayout = observer(() => {
description_html: newDescription, description_html: newDescription,
}) })
.then(() => { .then(() => {
mutatePageDetails((prevData) => ({ ...prevData, description_html: newDescription }) as IPage, false); mutatePageDetails((prevData) => ({ ...prevData, description_html: newDescription } as IPage), false);
}); });
}; };
@ -162,15 +162,12 @@ const PageDetailsPage: NextPageWithLayout = observer(() => {
}, [pageDetails?.description_html]); // TODO: Verify the exhaustive-deps warning }, [pageDetails?.description_html]); // TODO: Verify the exhaustive-deps warning
function createObjectFromArray(keys: string[], options: any): any { function createObjectFromArray(keys: string[], options: any): any {
return keys.reduce( return keys.reduce((obj, key) => {
(obj, key) => { if (options[key] !== undefined) {
if (options[key] !== undefined) { obj[key] = options[key];
obj[key] = options[key]; }
} return obj;
return obj; }, {} as { [key: string]: any });
},
{} as { [key: string]: any }
);
} }
const mutatePageDetailsHelper = ( const mutatePageDetailsHelper = (

View File

@ -158,8 +158,8 @@ const OnboardingPage: NextPageWithLayout = observer(() => {
currentUser?.first_name currentUser?.first_name
? `${currentUser?.first_name} ${currentUser?.last_name ?? ""}` ? `${currentUser?.first_name} ${currentUser?.last_name ?? ""}`
: value.length > 0 : value.length > 0
? value ? value
: currentUser?.email : currentUser?.email
} }
src={currentUser?.avatar} src={currentUser?.avatar}
size={35} size={35}
@ -174,8 +174,8 @@ const OnboardingPage: NextPageWithLayout = observer(() => {
{currentUser?.first_name {currentUser?.first_name
? `${currentUser?.first_name} ${currentUser?.last_name ?? ""}` ? `${currentUser?.first_name} ${currentUser?.last_name ?? ""}`
: value.length > 0 : value.length > 0
? value ? value
: null} : null}
</p> </p>
)} )}

View File

@ -1,6 +1,5 @@
import { observable, action, makeObservable, runInAction } from "mobx"; import { observable, action, makeObservable, runInAction } from "mobx";
// types // types
import { RootStore } from "../root.store";
import { IAppConfig } from "types/app"; import { IAppConfig } from "types/app";
// services // services
import { AppConfigService } from "services/app_config.service"; import { AppConfigService } from "services/app_config.service";
@ -14,13 +13,10 @@ export interface IAppConfigStore {
export class AppConfigStore implements IAppConfigStore { export class AppConfigStore implements IAppConfigStore {
// observables // observables
envConfig: IAppConfig | null = null; envConfig: IAppConfig | null = null;
// root store
rootStore;
// service // service
appConfigService; appConfigService;
constructor(_rootStore: RootStore) { constructor() {
makeObservable(this, { makeObservable(this, {
// observables // observables
envConfig: observable.ref, envConfig: observable.ref,
@ -28,9 +24,8 @@ export class AppConfigStore implements IAppConfigStore {
fetchAppConfig: action, fetchAppConfig: action,
}); });
this.appConfigService = new AppConfigService(); this.appConfigService = new AppConfigService();
this.rootStore = _rootStore;
} }
fetchAppConfig = async () => { fetchAppConfig = async () => {
try { try {
const config = await this.appConfigService.envConfig(); const config = await this.appConfigService.envConfig();

View File

@ -1,6 +1,4 @@
import { observable, action, makeObservable, computed } from "mobx"; import { observable, action, makeObservable, computed } from "mobx";
// types
import { RootStore } from "../root.store";
// services // services
import { ProjectService } from "services/project"; import { ProjectService } from "services/project";
import { PageService } from "services/page.service"; import { PageService } from "services/page.service";
@ -58,15 +56,13 @@ export class CommandPaletteStore implements ICommandPaletteStore {
isCreateIssueModalOpen: boolean = false; isCreateIssueModalOpen: boolean = false;
isDeleteIssueModalOpen: boolean = false; isDeleteIssueModalOpen: boolean = false;
isBulkDeleteIssueModalOpen: boolean = false; isBulkDeleteIssueModalOpen: boolean = false;
// root store
rootStore;
// service // service
projectService; projectService;
pageService; pageService;
createIssueStoreType: EProjectStore = EProjectStore.PROJECT; createIssueStoreType: EProjectStore = EProjectStore.PROJECT;
constructor(_rootStore: RootStore) { constructor() {
makeObservable(this, { makeObservable(this, {
// observable // observable
isCommandPaletteOpen: observable.ref, isCommandPaletteOpen: observable.ref,
@ -95,7 +91,6 @@ export class CommandPaletteStore implements ICommandPaletteStore {
toggleBulkDeleteIssueModal: action, toggleBulkDeleteIssueModal: action,
}); });
this.rootStore = _rootStore;
this.projectService = new ProjectService(); this.projectService = new ProjectService();
this.pageService = new PageService(); this.pageService = new PageService();
} }

View File

@ -1,7 +1,6 @@
import { RootStore } from "../root.store";
import { AppConfigStore, IAppConfigStore } from "./app-config.store"; import { AppConfigStore, IAppConfigStore } from "./app-config.store";
import { CommandPaletteStore, ICommandPaletteStore } from "./command-palette.store"; import { CommandPaletteStore, ICommandPaletteStore } from "./command-palette.store";
import { EventTrackerStore, IEventTrackerStore } from "./event-tracker.store"; // import { EventTrackerStore, IEventTrackerStore } from "./event-tracker.store";
import { InstanceStore, IInstanceStore } from "./instance.store"; import { InstanceStore, IInstanceStore } from "./instance.store";
import { RouterStore, IRouterStore } from "./router.store"; import { RouterStore, IRouterStore } from "./router.store";
import { ThemeStore, IThemeStore } from "./theme.store"; import { ThemeStore, IThemeStore } from "./theme.store";
@ -9,7 +8,7 @@ import { ThemeStore, IThemeStore } from "./theme.store";
export interface IAppRootStore { export interface IAppRootStore {
config: IAppConfigStore; config: IAppConfigStore;
commandPalette: ICommandPaletteStore; commandPalette: ICommandPaletteStore;
eventTracker: IEventTrackerStore; // eventTracker: IEventTrackerStore;
instance: IInstanceStore; instance: IInstanceStore;
theme: IThemeStore; theme: IThemeStore;
router: IRouterStore; router: IRouterStore;
@ -18,17 +17,17 @@ export interface IAppRootStore {
export class AppRootStore implements IAppRootStore { export class AppRootStore implements IAppRootStore {
config: IAppConfigStore; config: IAppConfigStore;
commandPalette: ICommandPaletteStore; commandPalette: ICommandPaletteStore;
eventTracker: IEventTrackerStore; // eventTracker: IEventTrackerStore;
instance: IInstanceStore; instance: IInstanceStore;
theme: IThemeStore; theme: IThemeStore;
router: IRouterStore; router: IRouterStore;
constructor(rootStore: RootStore) { constructor() {
this.config = new AppConfigStore(rootStore);
this.commandPalette = new CommandPaletteStore(rootStore);
this.eventTracker = new EventTrackerStore(rootStore);
this.instance = new InstanceStore(rootStore);
this.theme = new ThemeStore(rootStore);
this.router = new RouterStore(); this.router = new RouterStore();
this.config = new AppConfigStore();
this.commandPalette = new CommandPaletteStore();
// this.eventTracker = new EventTrackerStore(this.router);
this.instance = new InstanceStore();
this.theme = new ThemeStore();
} }
} }

View File

@ -1,6 +1,4 @@
import { observable, action, computed, makeObservable, runInAction } from "mobx"; import { observable, action, computed, makeObservable, runInAction } from "mobx";
// store
import { RootStore } from "../root.store";
// types // types
import { IInstance, IInstanceConfiguration, IFormattedInstanceConfiguration, IInstanceAdmin } from "types/instance"; import { IInstance, IInstanceConfiguration, IFormattedInstanceConfiguration, IInstanceAdmin } from "types/instance";
// services // services
@ -31,9 +29,8 @@ export class InstanceStore implements IInstanceStore {
configurations: IInstanceConfiguration[] | null = null; configurations: IInstanceConfiguration[] | null = null;
// service // service
instanceService; instanceService;
rootStore;
constructor(_rootStore: RootStore) { constructor() {
makeObservable(this, { makeObservable(this, {
// observable // observable
loader: observable.ref, loader: observable.ref,
@ -51,7 +48,6 @@ export class InstanceStore implements IInstanceStore {
updateInstanceConfigurations: action, updateInstanceConfigurations: action,
}); });
this.rootStore = _rootStore;
this.instanceService = new InstanceService(); this.instanceService = new InstanceService();
} }

View File

@ -10,7 +10,7 @@ import { PageService } from "services/page.service";
// types // types
import { IPage, IRecentPages } from "types"; import { IPage, IRecentPages } from "types";
// store // store
import { RootStore } from "./root.store"; import { AppRootStore } from "store/application";
export interface IPageStore { export interface IPageStore {
pages: Record<string, IPage>; pages: Record<string, IPage>;
@ -19,7 +19,7 @@ export interface IPageStore {
projectPages: IPage[] | undefined; projectPages: IPage[] | undefined;
favoriteProjectPages: IPage[] | undefined; favoriteProjectPages: IPage[] | undefined;
privateProjectPages: IPage[] | undefined; privateProjectPages: IPage[] | undefined;
sharedProjectPages: IPage[] | undefined; publicProjectPages: IPage[] | undefined;
recentProjectPages: IRecentPages | undefined; recentProjectPages: IRecentPages | undefined;
// archived pages computed // archived pages computed
archivedProjectPages: IPage[] | undefined; archivedProjectPages: IPage[] | undefined;
@ -49,7 +49,7 @@ export class PageStore {
// stores // stores
router; router;
constructor(_rootStore: RootStore) { constructor(appRootStore: AppRootStore) {
makeObservable(this, { makeObservable(this, {
pages: observable, pages: observable,
archivedPages: observable, archivedPages: observable,
@ -78,7 +78,7 @@ export class PageStore {
restorePage: action, restorePage: action,
}); });
// stores // stores
this.router = _rootStore.app.router; this.router = appRootStore.router;
// services // services
this.pageService = new PageService(); this.pageService = new PageService();
} }