import { css } from '@emotion/css'; import * as React from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { config, FetchError } from '@grafana/runtime'; import { Dashboard } from '@grafana/schema'; import { Button, ConfirmModal, Modal, useStyles2 } from '@grafana/ui'; import { t, Trans } from 'app/core/internationalization'; import { DashboardModel } from '../../state/DashboardModel'; import { SaveDashboardAsButton } from './SaveDashboardButton'; import { SaveDashboardModalProps } from './types'; import { useDashboardSave } from './useDashboardSave'; interface SaveDashboardErrorProxyProps { /** original dashboard */ dashboard: DashboardModel; /** dashboard save model with applied modifications, i.e. title */ dashboardSaveModel: Dashboard; error: FetchError; onDismiss: () => void; setErrorIsHandled: React.Dispatch>; } export const SaveDashboardErrorProxy = ({ dashboard, dashboardSaveModel, error, onDismiss, setErrorIsHandled, }: SaveDashboardErrorProxyProps) => { const { onDashboardSave } = useDashboardSave(); const isRestoreDashboardsEnabled = config.featureToggles.dashboardRestore; return ( <> {error.data && error.data.status === 'version-mismatch' && ( Someone else has updated this dashboard
Would you still like to save this dashboard? } confirmText="Save and overwrite" onConfirm={async () => { await onDashboardSave(dashboardSaveModel, { overwrite: true }, dashboard); onDismiss(); }} onDismiss={onDismiss} /> )} {error.data && error.data.status === 'name-exists' && ( <> {isRestoreDashboardsEnabled ? (

A dashboard with the same name in the selected folder already exists, including recently deleted dashboards.

Please choose a different name or folder.

) : ( A dashboard with the same name in selected folder already exists.
Would you still like to save this dashboard? } confirmText="Save and overwrite" onConfirm={async () => { await onDashboardSave(dashboardSaveModel, { overwrite: true }, dashboard); onDismiss(); }} onDismiss={onDismiss} /> )} )} {error.data && error.data.status === 'plugin-dashboard' && ( { setErrorIsHandled(true); onDismiss(); }} /> )} ); }; const ConfirmPluginDashboardSaveModal = ({ onDismiss, dashboard }: SaveDashboardModalProps) => { const { onDashboardSave } = useDashboardSave(); const styles = useStyles2(getConfirmPluginDashboardSaveModalStyles); return (
Your changes will be lost when you update the plugin.
Use Save As to create custom version.
); }; export const proxyHandlesError = (errorStatus: string) => { switch (errorStatus) { case 'version-mismatch': case 'name-exists': case 'plugin-dashboard': return true; default: return false; } }; const getConfirmPluginDashboardSaveModalStyles = (theme: GrafanaTheme2) => ({ modal: css({ width: '500px', }), modalText: css({ fontSize: theme.typography.h4.fontSize, color: theme.colors.text.primary, marginBottom: theme.spacing(4), paddingTop: theme.spacing(2), }), modalButtonRow: css({ marginBottom: '14px', 'a, button': { marginRight: theme.spacing(2), }, }), });