mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
fix: adding new contexts and providers
This commit is contained in:
parent
f3748d09a5
commit
08b848c7d8
19
web/contexts/app-root/app-root-provider.tsx
Normal file
19
web/contexts/app-root/app-root-provider.tsx
Normal 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>;
|
||||||
|
};
|
2
web/contexts/app-root/index.ts
Normal file
2
web/contexts/app-root/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./app-root-provider";
|
||||||
|
export * from "./use-app-root";
|
8
web/contexts/app-root/use-app-root.tsx
Normal file
8
web/contexts/app-root/use-app-root.tsx
Normal 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;
|
||||||
|
};
|
2
web/contexts/page.context/index.ts
Normal file
2
web/contexts/page.context/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./page-provider";
|
||||||
|
export * from "./use-page";
|
20
web/contexts/page.context/page-provider.tsx
Normal file
20
web/contexts/page.context/page-provider.tsx
Normal 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>;
|
||||||
|
};
|
8
web/contexts/page.context/use-page.tsx
Normal file
8
web/contexts/page.context/use-page.tsx
Normal 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
8
web/hooks/use-page.tsx
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { useMobxStore } from "lib/mobx/store-provider";
|
||||||
|
|
||||||
|
const usePage = () => {
|
||||||
|
const { page } = useMobxStore();
|
||||||
|
return { ...page };
|
||||||
|
};
|
||||||
|
|
||||||
|
export default usePage;
|
@ -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";
|
||||||
|
@ -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 = (
|
||||||
|
@ -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();
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user