2024-03-06 13:09:14 +00:00
|
|
|
import set from "lodash/set";
|
2024-01-18 10:19:54 +00:00
|
|
|
import { action, computed, makeObservable, observable, runInAction } from "mobx";
|
2024-01-22 11:37:32 +00:00
|
|
|
import { computedFn } from "mobx-utils";
|
2024-01-18 10:19:54 +00:00
|
|
|
// types
|
|
|
|
import {
|
|
|
|
THomeDashboardResponse,
|
|
|
|
TWidget,
|
|
|
|
TWidgetFiltersFormData,
|
|
|
|
TWidgetStatsResponse,
|
|
|
|
TWidgetKeys,
|
|
|
|
TWidgetStatsRequestParams,
|
|
|
|
} from "@plane/types";
|
2024-05-08 17:31:20 +00:00
|
|
|
// services
|
|
|
|
import { DashboardService } from "@/services/dashboard.service";
|
|
|
|
// stores
|
|
|
|
import { RootStore } from "@/store/root.store";
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
export interface IDashboardStore {
|
2024-03-11 15:42:28 +00:00
|
|
|
// error states
|
|
|
|
widgetStatsError: { [workspaceSlug: string]: Record<string, Record<TWidgetKeys, any | null>> };
|
2024-01-18 10:19:54 +00:00
|
|
|
// observables
|
|
|
|
homeDashboardId: string | null;
|
|
|
|
widgetDetails: { [workspaceSlug: string]: Record<string, TWidget[]> };
|
|
|
|
// {
|
|
|
|
// workspaceSlug: {
|
|
|
|
// dashboardId: TWidget[]
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
widgetStats: { [workspaceSlug: string]: Record<string, Record<TWidgetKeys, TWidgetStatsResponse>> };
|
|
|
|
// {
|
|
|
|
// workspaceSlug: {
|
|
|
|
// dashboardId: {
|
|
|
|
// widgetKey: TWidgetStatsResponse;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// computed
|
|
|
|
homeDashboardWidgets: TWidget[] | undefined;
|
|
|
|
// computed actions
|
|
|
|
getWidgetDetails: (workspaceSlug: string, dashboardId: string, widgetKey: TWidgetKeys) => TWidget | undefined;
|
2024-01-22 15:20:30 +00:00
|
|
|
getWidgetStats: <T>(workspaceSlug: string, dashboardId: string, widgetKey: TWidgetKeys) => T | undefined;
|
2024-03-11 15:42:28 +00:00
|
|
|
getWidgetStatsError: (workspaceSlug: string, dashboardId: string, widgetKey: TWidgetKeys) => any | null;
|
2024-01-18 10:19:54 +00:00
|
|
|
// actions
|
|
|
|
fetchHomeDashboardWidgets: (workspaceSlug: string) => Promise<THomeDashboardResponse>;
|
|
|
|
fetchWidgetStats: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
dashboardId: string,
|
|
|
|
params: TWidgetStatsRequestParams
|
|
|
|
) => Promise<TWidgetStatsResponse>;
|
|
|
|
updateDashboardWidget: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
dashboardId: string,
|
|
|
|
widgetId: string,
|
|
|
|
data: Partial<TWidget>
|
|
|
|
) => Promise<any>;
|
|
|
|
updateDashboardWidgetFilters: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
dashboardId: string,
|
|
|
|
widgetId: string,
|
|
|
|
data: TWidgetFiltersFormData
|
|
|
|
) => Promise<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DashboardStore implements IDashboardStore {
|
2024-03-11 15:42:28 +00:00
|
|
|
// error states
|
|
|
|
widgetStatsError: { [workspaceSlug: string]: Record<string, Record<TWidgetKeys, any>> } = {};
|
2024-01-18 10:19:54 +00:00
|
|
|
// observables
|
|
|
|
homeDashboardId: string | null = null;
|
|
|
|
widgetDetails: { [workspaceSlug: string]: Record<string, TWidget[]> } = {};
|
|
|
|
widgetStats: { [workspaceSlug: string]: Record<string, Record<TWidgetKeys, TWidgetStatsResponse>> } = {};
|
|
|
|
// stores
|
|
|
|
routerStore;
|
|
|
|
issueStore;
|
|
|
|
// services
|
|
|
|
dashboardService;
|
|
|
|
|
|
|
|
constructor(_rootStore: RootStore) {
|
|
|
|
makeObservable(this, {
|
2024-03-11 15:42:28 +00:00
|
|
|
// error states
|
|
|
|
widgetStatsError: observable,
|
2024-01-18 10:19:54 +00:00
|
|
|
// observables
|
|
|
|
homeDashboardId: observable.ref,
|
|
|
|
widgetDetails: observable,
|
|
|
|
widgetStats: observable,
|
|
|
|
// computed
|
|
|
|
homeDashboardWidgets: computed,
|
|
|
|
// fetch actions
|
|
|
|
fetchHomeDashboardWidgets: action,
|
|
|
|
fetchWidgetStats: action,
|
|
|
|
// update actions
|
|
|
|
updateDashboardWidget: action,
|
|
|
|
updateDashboardWidgetFilters: action,
|
|
|
|
});
|
|
|
|
|
|
|
|
// router store
|
2024-05-08 17:31:20 +00:00
|
|
|
this.routerStore = _rootStore.router;
|
2024-01-18 10:19:54 +00:00
|
|
|
this.issueStore = _rootStore.issue.issues;
|
|
|
|
// services
|
|
|
|
this.dashboardService = new DashboardService();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get home dashboard widgets
|
2024-03-11 15:42:28 +00:00
|
|
|
* @returns {TWidget[] | undefined}
|
2024-01-18 10:19:54 +00:00
|
|
|
*/
|
|
|
|
get homeDashboardWidgets() {
|
|
|
|
const workspaceSlug = this.routerStore.workspaceSlug;
|
|
|
|
if (!workspaceSlug) return undefined;
|
|
|
|
const { homeDashboardId, widgetDetails } = this;
|
|
|
|
return homeDashboardId ? widgetDetails?.[workspaceSlug]?.[homeDashboardId] : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get widget details
|
2024-03-11 15:42:28 +00:00
|
|
|
* @param {string} workspaceSlug
|
|
|
|
* @param {string} dashboardId
|
|
|
|
* @param {TWidgetKeys} widgetKey
|
|
|
|
* @returns {TWidget | undefined}
|
2024-01-18 10:19:54 +00:00
|
|
|
*/
|
2024-01-22 11:37:32 +00:00
|
|
|
getWidgetDetails = computedFn((workspaceSlug: string, dashboardId: string, widgetKey: TWidgetKeys) => {
|
2024-01-18 10:19:54 +00:00
|
|
|
const widgets = this.widgetDetails?.[workspaceSlug]?.[dashboardId];
|
|
|
|
if (!widgets) return undefined;
|
|
|
|
return widgets.find((widget) => widget.key === widgetKey);
|
2024-01-22 11:37:32 +00:00
|
|
|
});
|
2024-01-18 10:19:54 +00:00
|
|
|
|
2024-01-22 15:20:30 +00:00
|
|
|
/**
|
|
|
|
* @description get widget stats
|
2024-03-11 15:42:28 +00:00
|
|
|
* @param {string} workspaceSlug
|
|
|
|
* @param {string} dashboardId
|
|
|
|
* @param {TWidgetKeys} widgetKey
|
|
|
|
* @returns {T | undefined}
|
2024-01-22 15:20:30 +00:00
|
|
|
*/
|
|
|
|
getWidgetStats = <T>(workspaceSlug: string, dashboardId: string, widgetKey: TWidgetKeys): T | undefined =>
|
|
|
|
(this.widgetStats?.[workspaceSlug]?.[dashboardId]?.[widgetKey] as unknown as T) ?? undefined;
|
|
|
|
|
2024-03-11 15:42:28 +00:00
|
|
|
/**
|
|
|
|
* @description get widget stats error
|
|
|
|
* @param {string} workspaceSlug
|
|
|
|
* @param {string} dashboardId
|
|
|
|
* @param {TWidgetKeys} widgetKey
|
|
|
|
* @returns {any | null}
|
|
|
|
*/
|
|
|
|
getWidgetStatsError = (workspaceSlug: string, dashboardId: string, widgetKey: TWidgetKeys) =>
|
|
|
|
this.widgetStatsError?.[workspaceSlug]?.[dashboardId]?.[widgetKey] ?? null;
|
|
|
|
|
2024-01-18 10:19:54 +00:00
|
|
|
/**
|
|
|
|
* @description fetch home dashboard details and widgets
|
2024-03-11 15:42:28 +00:00
|
|
|
* @param {string} workspaceSlug
|
|
|
|
* @returns {Promise<THomeDashboardResponse>}
|
2024-01-18 10:19:54 +00:00
|
|
|
*/
|
2024-03-11 15:42:28 +00:00
|
|
|
fetchHomeDashboardWidgets = async (workspaceSlug: string): Promise<THomeDashboardResponse> => {
|
2024-01-18 10:19:54 +00:00
|
|
|
try {
|
|
|
|
const response = await this.dashboardService.getHomeDashboardWidgets(workspaceSlug);
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.homeDashboardId = response.dashboard.id;
|
|
|
|
set(this.widgetDetails, [workspaceSlug, response.dashboard.id], response.widgets);
|
|
|
|
});
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
runInAction(() => {
|
|
|
|
this.homeDashboardId = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description fetch widget stats
|
2024-03-11 15:42:28 +00:00
|
|
|
* @param {string} workspaceSlug
|
|
|
|
* @param {string} dashboardId
|
|
|
|
* @param {TWidgetStatsRequestParams} widgetKey
|
2024-01-18 10:19:54 +00:00
|
|
|
* @returns widget stats
|
|
|
|
*/
|
|
|
|
fetchWidgetStats = async (workspaceSlug: string, dashboardId: string, params: TWidgetStatsRequestParams) =>
|
2024-03-11 15:42:28 +00:00
|
|
|
this.dashboardService
|
|
|
|
.getWidgetStats(workspaceSlug, dashboardId, params)
|
2024-05-08 17:31:20 +00:00
|
|
|
.then((res: any) => {
|
2024-03-11 15:42:28 +00:00
|
|
|
runInAction(() => {
|
|
|
|
if (res.issues) this.issueStore.addIssue(res.issues);
|
|
|
|
set(this.widgetStats, [workspaceSlug, dashboardId, params.widget_key], res);
|
|
|
|
set(this.widgetStatsError, [workspaceSlug, dashboardId, params.widget_key], null);
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
runInAction(() => {
|
|
|
|
set(this.widgetStatsError, [workspaceSlug, dashboardId, params.widget_key], error);
|
|
|
|
});
|
|
|
|
|
|
|
|
throw error;
|
2024-01-18 10:19:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update dashboard widget
|
2024-03-11 15:42:28 +00:00
|
|
|
* @param {string} dashboardId
|
|
|
|
* @param {string} widgetId
|
|
|
|
* @param {Partial<TWidget>} data
|
2024-01-18 10:19:54 +00:00
|
|
|
* @returns updated widget
|
|
|
|
*/
|
|
|
|
updateDashboardWidget = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
dashboardId: string,
|
|
|
|
widgetId: string,
|
|
|
|
data: Partial<TWidget>
|
|
|
|
): Promise<any> => {
|
|
|
|
// find all widgets in dashboard
|
|
|
|
const widgets = this.widgetDetails?.[workspaceSlug]?.[dashboardId];
|
|
|
|
if (!widgets) throw new Error("Dashboard not found");
|
|
|
|
// find widget index
|
|
|
|
const widgetIndex = widgets.findIndex((widget) => widget.id === widgetId);
|
|
|
|
// get original widget
|
|
|
|
const originalWidget = { ...widgets[widgetIndex] };
|
|
|
|
if (widgetIndex === -1) throw new Error("Widget not found");
|
|
|
|
|
|
|
|
try {
|
|
|
|
runInAction(() => {
|
|
|
|
this.widgetDetails[workspaceSlug][dashboardId][widgetIndex] = {
|
|
|
|
...widgets[widgetIndex],
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const response = await this.dashboardService.updateDashboardWidget(dashboardId, widgetId, data);
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
// revert changes
|
|
|
|
runInAction(() => {
|
|
|
|
this.widgetDetails[workspaceSlug][dashboardId][widgetIndex] = originalWidget;
|
|
|
|
});
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update dashboard widget filters
|
2024-03-11 15:42:28 +00:00
|
|
|
* @param {string} dashboardId
|
|
|
|
* @param {string} widgetId
|
|
|
|
* @param {TWidgetFiltersFormData} data
|
2024-01-18 10:19:54 +00:00
|
|
|
* @returns updated widget
|
|
|
|
*/
|
|
|
|
updateDashboardWidgetFilters = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
dashboardId: string,
|
|
|
|
widgetId: string,
|
|
|
|
data: TWidgetFiltersFormData
|
|
|
|
): Promise<TWidget> => {
|
|
|
|
const widgetDetails = this.getWidgetDetails(workspaceSlug, dashboardId, data.widgetKey);
|
|
|
|
if (!widgetDetails) throw new Error("Widget not found");
|
|
|
|
try {
|
|
|
|
const updatedWidget = {
|
|
|
|
...widgetDetails,
|
|
|
|
widget_filters: {
|
|
|
|
...widgetDetails.widget_filters,
|
|
|
|
...data.filters,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// update widget details optimistically
|
|
|
|
runInAction(() => {
|
|
|
|
set(
|
|
|
|
this.widgetDetails,
|
|
|
|
[workspaceSlug, dashboardId],
|
|
|
|
this.widgetDetails?.[workspaceSlug]?.[dashboardId]?.map((w) => (w.id === widgetId ? updatedWidget : w))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const response = await this.updateDashboardWidget(workspaceSlug, dashboardId, widgetId, {
|
|
|
|
filters: {
|
|
|
|
...widgetDetails.widget_filters,
|
|
|
|
...data.filters,
|
|
|
|
},
|
|
|
|
}).then((res) => res);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
// revert changes
|
|
|
|
runInAction(() => {
|
|
|
|
this.widgetDetails[workspaceSlug][dashboardId] = this.widgetDetails?.[workspaceSlug]?.[dashboardId]?.map((w) =>
|
|
|
|
w.id === widgetId ? widgetDetails : w
|
|
|
|
);
|
|
|
|
});
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|