2024-05-08 17:31:20 +00:00
|
|
|
import set from "lodash/set";
|
2024-05-14 15:24:49 +00:00
|
|
|
import { observable, action, computed, makeObservable, runInAction } from "mobx";
|
2024-05-17 15:02:40 +00:00
|
|
|
import {
|
|
|
|
IInstance,
|
|
|
|
IInstanceAdmin,
|
|
|
|
IInstanceConfiguration,
|
|
|
|
IFormattedInstanceConfiguration,
|
|
|
|
IInstanceInfo,
|
|
|
|
IInstanceConfig,
|
|
|
|
} from "@plane/types";
|
2024-05-08 17:31:20 +00:00
|
|
|
// helpers
|
|
|
|
import { EInstanceStatus, TInstanceStatus } from "@/helpers";
|
|
|
|
// services
|
|
|
|
import { InstanceService } from "@/services/instance.service";
|
|
|
|
// root store
|
2024-05-14 15:24:49 +00:00
|
|
|
import { RootStore } from "@/store/root.store";
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
export interface IInstanceStore {
|
|
|
|
// issues
|
|
|
|
isLoading: boolean;
|
2024-05-17 15:02:40 +00:00
|
|
|
error: any;
|
2024-05-08 17:31:20 +00:00
|
|
|
instanceStatus: TInstanceStatus | undefined;
|
|
|
|
instance: IInstance | undefined;
|
2024-05-17 15:02:40 +00:00
|
|
|
config: IInstanceConfig | undefined;
|
2024-05-08 17:31:20 +00:00
|
|
|
instanceAdmins: IInstanceAdmin[] | undefined;
|
|
|
|
instanceConfigurations: IInstanceConfiguration[] | undefined;
|
|
|
|
// computed
|
|
|
|
formattedConfig: IFormattedInstanceConfiguration | undefined;
|
|
|
|
// action
|
2024-05-17 15:02:40 +00:00
|
|
|
hydrate: (data: IInstanceInfo) => void;
|
|
|
|
fetchInstanceInfo: () => Promise<IInstanceInfo | undefined>;
|
|
|
|
updateInstanceInfo: (data: Partial<IInstance>) => Promise<IInstance | undefined>;
|
2024-05-08 17:31:20 +00:00
|
|
|
fetchInstanceAdmins: () => Promise<IInstanceAdmin[] | undefined>;
|
|
|
|
fetchInstanceConfigurations: () => Promise<IInstanceConfiguration[] | undefined>;
|
2024-05-14 15:24:49 +00:00
|
|
|
updateInstanceConfigurations: (data: Partial<IFormattedInstanceConfiguration>) => Promise<IInstanceConfiguration[]>;
|
2024-05-08 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class InstanceStore implements IInstanceStore {
|
|
|
|
isLoading: boolean = true;
|
2024-05-17 15:02:40 +00:00
|
|
|
error: any = undefined;
|
2024-05-08 17:31:20 +00:00
|
|
|
instanceStatus: TInstanceStatus | undefined = undefined;
|
|
|
|
instance: IInstance | undefined = undefined;
|
2024-05-17 15:02:40 +00:00
|
|
|
config: IInstanceConfig | undefined = undefined;
|
2024-05-08 17:31:20 +00:00
|
|
|
instanceAdmins: IInstanceAdmin[] | undefined = undefined;
|
|
|
|
instanceConfigurations: IInstanceConfiguration[] | undefined = undefined;
|
|
|
|
// service
|
|
|
|
instanceService;
|
|
|
|
|
|
|
|
constructor(private store: RootStore) {
|
|
|
|
makeObservable(this, {
|
|
|
|
// observable
|
|
|
|
isLoading: observable.ref,
|
2024-05-17 15:02:40 +00:00
|
|
|
error: observable.ref,
|
2024-05-08 17:31:20 +00:00
|
|
|
instanceStatus: observable,
|
|
|
|
instance: observable,
|
|
|
|
instanceAdmins: observable,
|
|
|
|
instanceConfigurations: observable,
|
|
|
|
// computed
|
|
|
|
formattedConfig: computed,
|
|
|
|
// actions
|
2024-05-14 15:24:49 +00:00
|
|
|
hydrate: action,
|
2024-05-08 17:31:20 +00:00
|
|
|
fetchInstanceInfo: action,
|
|
|
|
fetchInstanceAdmins: action,
|
|
|
|
updateInstanceInfo: action,
|
|
|
|
fetchInstanceConfigurations: action,
|
|
|
|
updateInstanceConfigurations: action,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.instanceService = new InstanceService();
|
|
|
|
}
|
|
|
|
|
2024-05-17 15:02:40 +00:00
|
|
|
hydrate = (data: IInstanceInfo) => {
|
|
|
|
if (data) {
|
|
|
|
this.instance = data.instance;
|
|
|
|
this.config = data.config;
|
|
|
|
}
|
2024-05-14 15:24:49 +00:00
|
|
|
};
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
/**
|
|
|
|
* computed value for instance configurations data for forms.
|
|
|
|
* @returns configurations in the form of {key, value} pair.
|
|
|
|
*/
|
|
|
|
get formattedConfig() {
|
|
|
|
if (!this.instanceConfigurations) return undefined;
|
|
|
|
return this.instanceConfigurations?.reduce((formData: IFormattedInstanceConfiguration, config) => {
|
|
|
|
formData[config.key] = config.value;
|
|
|
|
return formData;
|
|
|
|
}, {} as IFormattedInstanceConfiguration);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description fetching instance configuration
|
|
|
|
* @returns {IInstance} instance
|
|
|
|
*/
|
|
|
|
fetchInstanceInfo = async () => {
|
|
|
|
try {
|
|
|
|
if (this.instance === undefined) this.isLoading = true;
|
2024-05-17 15:02:40 +00:00
|
|
|
this.error = undefined;
|
|
|
|
const instanceInfo = await this.instanceService.getInstanceInfo();
|
2024-05-08 17:31:20 +00:00
|
|
|
// handling the new user popup toggle
|
2024-05-17 15:02:40 +00:00
|
|
|
if (this.instance === undefined && !instanceInfo?.instance?.workspaces_exist)
|
|
|
|
this.store.theme.toggleNewUserPopup();
|
2024-05-08 17:31:20 +00:00
|
|
|
runInAction(() => {
|
2024-05-17 15:02:40 +00:00
|
|
|
console.log("instanceInfo: ", instanceInfo);
|
2024-05-08 17:31:20 +00:00
|
|
|
this.isLoading = false;
|
2024-05-17 15:02:40 +00:00
|
|
|
this.instance = instanceInfo.instance;
|
|
|
|
this.config = instanceInfo.config;
|
2024-05-08 17:31:20 +00:00
|
|
|
});
|
2024-05-17 15:02:40 +00:00
|
|
|
return instanceInfo;
|
2024-05-08 17:31:20 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching the instance info");
|
|
|
|
this.isLoading = false;
|
2024-05-17 15:02:40 +00:00
|
|
|
this.error = { message: "Failed to fetch the instance info" };
|
2024-05-08 17:31:20 +00:00
|
|
|
this.instanceStatus = {
|
|
|
|
status: EInstanceStatus.ERROR,
|
|
|
|
};
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description updating instance information
|
2024-05-17 15:02:40 +00:00
|
|
|
* @param {Partial<IInstance>} data
|
2024-05-08 17:31:20 +00:00
|
|
|
* @returns void
|
|
|
|
*/
|
2024-05-17 15:02:40 +00:00
|
|
|
updateInstanceInfo = async (data: Partial<IInstance>) => {
|
2024-05-08 17:31:20 +00:00
|
|
|
try {
|
|
|
|
const instanceResponse = await this.instanceService.updateInstanceInfo(data);
|
|
|
|
if (instanceResponse) {
|
|
|
|
runInAction(() => {
|
|
|
|
if (this.instance) set(this.instance, "instance", instanceResponse);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return instanceResponse;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error updating the instance info");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description fetching instance admins
|
|
|
|
* @return {IInstanceAdmin[]} instanceAdmins
|
|
|
|
*/
|
|
|
|
fetchInstanceAdmins = async () => {
|
|
|
|
try {
|
|
|
|
const instanceAdmins = await this.instanceService.getInstanceAdmins();
|
|
|
|
if (instanceAdmins) runInAction(() => (this.instanceAdmins = instanceAdmins));
|
|
|
|
return instanceAdmins;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching the instance admins");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description fetching instance configurations
|
|
|
|
* @return {IInstanceAdmin[]} instanceConfigurations
|
|
|
|
*/
|
|
|
|
fetchInstanceConfigurations = async () => {
|
|
|
|
try {
|
|
|
|
const instanceConfigurations = await this.instanceService.getInstanceConfigurations();
|
|
|
|
if (instanceConfigurations) runInAction(() => (this.instanceConfigurations = instanceConfigurations));
|
|
|
|
return instanceConfigurations;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching the instance configurations");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description updating instance configurations
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
updateInstanceConfigurations = async (data: Partial<IFormattedInstanceConfiguration>) => {
|
|
|
|
try {
|
2024-05-14 15:24:49 +00:00
|
|
|
const response = await this.instanceService.updateInstanceConfigurations(data);
|
|
|
|
runInAction(() => {
|
|
|
|
this.instanceConfigurations = this.instanceConfigurations?.map((config) => {
|
|
|
|
const item = response.find((item) => item.key === config.key);
|
|
|
|
if (item) return item;
|
|
|
|
return config;
|
2024-05-08 17:31:20 +00:00
|
|
|
});
|
|
|
|
});
|
2024-05-14 15:24:49 +00:00
|
|
|
return response;
|
2024-05-08 17:31:20 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error updating the instance configurations");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|