mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* this is an ok intermediate point * delete some unused actions + fix tag invalidation on folder save * remove prefetching for now (it creates a permanent subscription?!) * leave paginated fetch out of rtk query for now * ensure we're invalidating the cache correctly * fix dashboard saving * simplify * recursively invalidate children on rename * tidy up * don't need to invalidate tags on delete * don't need to invalidate on new either * make new refreshParents action * pageheader spacing * invalidate getFolder on move * bit of rearrangement
91 lines
3.4 KiB
TypeScript
91 lines
3.4 KiB
TypeScript
import { useAsyncFn } from 'react-use';
|
|
|
|
import { locationUtil } from '@grafana/data';
|
|
import { config, locationService, reportInteraction } from '@grafana/runtime';
|
|
import appEvents from 'app/core/app_events';
|
|
import { useAppNotification } from 'app/core/copy/appNotification';
|
|
import { contextSrv } from 'app/core/core';
|
|
import { updateDashboardName } from 'app/core/reducers/navBarTree';
|
|
import { useSaveDashboardMutation } from 'app/features/browse-dashboards/api/browseDashboardsAPI';
|
|
import { DashboardModel } from 'app/features/dashboard/state';
|
|
import { saveDashboard as saveDashboardApiCall } from 'app/features/manage-dashboards/state/actions';
|
|
import { useDispatch } from 'app/types';
|
|
import { DashboardSavedEvent } from 'app/types/events';
|
|
|
|
import { SaveDashboardOptions } from './types';
|
|
|
|
const saveDashboard = async (saveModel: any, options: SaveDashboardOptions, dashboard: DashboardModel) => {
|
|
let folderUid = options.folderUid;
|
|
if (folderUid === undefined) {
|
|
folderUid = dashboard.meta.folderUid ?? saveModel.folderUid;
|
|
}
|
|
|
|
const result = await saveDashboardApiCall({ ...options, folderUid, dashboard: saveModel });
|
|
// fetch updated access control permissions
|
|
await contextSrv.fetchUserPermissions();
|
|
return result;
|
|
};
|
|
|
|
export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
|
|
const dispatch = useDispatch();
|
|
const notifyApp = useAppNotification();
|
|
const [saveDashboardRtkQuery] = useSaveDashboardMutation();
|
|
const [state, onDashboardSave] = useAsyncFn(
|
|
async (clone: DashboardModel, options: SaveDashboardOptions, dashboard: DashboardModel) => {
|
|
try {
|
|
const queryResult = config.featureToggles.nestedFolders
|
|
? await saveDashboardRtkQuery({
|
|
dashboard: clone,
|
|
folderUid: options.folderUid ?? dashboard.meta.folderUid ?? clone.meta.folderUid,
|
|
message: options.message,
|
|
overwrite: options.overwrite,
|
|
})
|
|
: await saveDashboard(clone, options, dashboard);
|
|
const result = config.featureToggles.nestedFolders ? queryResult.data : queryResult;
|
|
dashboard.version = result.version;
|
|
dashboard.clearUnsavedChanges();
|
|
|
|
// important that these happen before location redirect below
|
|
appEvents.publish(new DashboardSavedEvent());
|
|
notifyApp.success('Dashboard saved');
|
|
if (isCopy) {
|
|
reportInteraction('grafana_dashboard_copied', {
|
|
name: dashboard.title,
|
|
url: result.url,
|
|
});
|
|
} else {
|
|
reportInteraction(`grafana_dashboard_${dashboard.id ? 'saved' : 'created'}`, {
|
|
name: dashboard.title,
|
|
url: result.url,
|
|
});
|
|
}
|
|
|
|
const currentPath = locationService.getLocation().pathname;
|
|
const newUrl = locationUtil.stripBaseFromUrl(result.url);
|
|
|
|
if (newUrl !== currentPath) {
|
|
setTimeout(() => locationService.replace(newUrl));
|
|
}
|
|
if (dashboard.meta.isStarred) {
|
|
dispatch(
|
|
updateDashboardName({
|
|
id: dashboard.uid,
|
|
title: dashboard.title,
|
|
url: newUrl,
|
|
})
|
|
);
|
|
}
|
|
return result;
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
notifyApp.error(error.message ?? 'Error saving dashboard');
|
|
}
|
|
throw error;
|
|
}
|
|
},
|
|
[dispatch, notifyApp]
|
|
);
|
|
|
|
return { state, onDashboardSave };
|
|
};
|