@ -1,9 +1,7 @@
import React , { useMemo , useState } from 'react' ;
import { useAsync } from 'react-use' ;
import { config , isFetchError } from '@grafana/runtime' ;
import { Drawer , Tab , TabsBar } from '@grafana/ui' ;
import { backendSrv } from 'app/core/services/backend_srv' ;
import { jsonDiff } from '../VersionHistory/utils' ;
@ -18,33 +16,24 @@ import { useDashboardSave } from './useDashboardSave';
export const SaveDashboardDrawer = ( { dashboard , onDismiss , onSaveSuccess , isCopy } : SaveDashboardModalProps ) = > {
const [ options , setOptions ] = useState < SaveDashboardOptions > ( { } ) ;
const previous = dashboard . getOriginalDashboard ( ) ;
const isProvisioned = dashboard . meta . provisioned ;
const isNew = dashboard . version === 0 ;
const previous = useAsync ( async ( ) = > {
if ( isNew ) {
return undefined ;
}
const result = await backendSrv . getDashboardByUid ( dashboard . uid ) ;
return result . dashboard ;
} , [ dashboard , isNew ] ) ;
const data = useMemo < SaveDashboardData > ( ( ) = > {
const clone = dashboard . getSaveModelClone ( {
saveTimerange : Boolean ( options . saveTimerange ) ,
saveVariables : Boolean ( options . saveVariables ) ,
} ) ;
if ( ! previous . value ) {
if ( ! previous ) {
return { clone , diff : { } , diffCount : 0 , hasChanges : false } ;
}
const cloneJSON = JSON . stringify ( clone , null , 2 ) ;
const cloneSafe = JSON . parse ( cloneJSON ) ; // avoids undefined issues
const diff = jsonDiff ( previous . value , cloneSafe ) ;
const diff = jsonDiff ( previous , cloneSafe ) ;
let diffCount = 0 ;
for ( const d of Object . values ( diff ) ) {
diffCount += d . length ;
@ -56,7 +45,7 @@ export const SaveDashboardDrawer = ({ dashboard, onDismiss, onSaveSuccess, isCop
diffCount ,
hasChanges : diffCount > 0 && ! isNew ,
} ;
} , [ dashboard , previous . value , options , isNew ] ) ;
} , [ dashboard , previous , options , isNew ] ) ;
const [ showDiff , setShowDiff ] = useState ( false ) ;
const { state , onDashboardSave } = useDashboardSave ( dashboard , isCopy ) ;
@ -69,7 +58,7 @@ export const SaveDashboardDrawer = ({ dashboard, onDismiss, onSaveSuccess, isCop
const renderSaveBody = ( ) = > {
if ( showDiff ) {
return < SaveDashboardDiff diff = { data . diff } oldValue = { previous . value } newValue = { data . clone } / > ;
return < SaveDashboardDiff diff = { data . diff } oldValue = { previous } newValue = { data . clone } / > ;
}
if ( isNew || isCopy ) {