plane/web/store/dataMaps/workspace.data.store.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-03-05 09:38:18 +00:00
import { action, makeObservable, observable } from "mobx";
import { computedFn } from "mobx-utils";
import { set } from "lodash";
// store
2024-03-04 14:39:46 +00:00
import { DataStore } from ".";
2024-03-05 09:38:18 +00:00
import { WorkspaceModel } from "store/workspace.store";
// types
2024-03-04 14:39:46 +00:00
import { IWorkspace } from "@plane/types";
export interface IWorkspaceData {
2024-03-05 09:38:18 +00:00
// observables
2024-03-04 14:39:46 +00:00
workspaceMap: Record<string, WorkspaceModel>;
2024-03-05 09:38:18 +00:00
// actions
2024-03-05 07:59:28 +00:00
addWorkspace: (workspaces: IWorkspace) => void;
2024-03-04 14:39:46 +00:00
deleteWorkspace: (workspaceId: string) => void;
2024-03-05 09:38:18 +00:00
getWorkspaceById: (workspaceId: string) => WorkspaceModel | undefined;
2024-03-04 14:39:46 +00:00
}
export class WorkspaceData implements IWorkspaceData {
2024-03-05 09:38:18 +00:00
// observables
2024-03-04 14:39:46 +00:00
workspaceMap: Record<string, WorkspaceModel> = {};
// data store
dataStore;
constructor(_dataStore: DataStore) {
makeObservable(this, {
2024-03-05 09:38:18 +00:00
// observables
2024-03-04 14:39:46 +00:00
workspaceMap: observable,
2024-03-05 09:38:18 +00:00
// actions
addWorkspace: action,
deleteWorkspace: action,
2024-03-04 14:39:46 +00:00
});
this.dataStore = _dataStore;
}
2024-03-05 09:38:18 +00:00
/**
* @description add a workspace to the workspace map
* @param {IWorkspace} workspace
*/
addWorkspace = (workspace: IWorkspace) =>
2024-03-05 07:59:28 +00:00
set(this.workspaceMap, [workspace.id], new WorkspaceModel(workspace, this.dataStore));
2024-03-04 14:39:46 +00:00
2024-03-05 09:38:18 +00:00
/**
* @description delete a workspace from the workspace map
* @param {string} workspaceId
*/
deleteWorkspace = (workspaceId: string) => delete this.workspaceMap[workspaceId];
/**
* @description get a workspace model by its id
* @param {string} workspaceId
* @returns {WorkspaceModel | undefined} workspace model
*/
getWorkspaceById = computedFn((workspaceId: string) => this.workspaceMap[workspaceId]);
2024-03-04 14:39:46 +00:00
}