grafana/public/app/features/explore/correlationEditLogic.ts
Kristina 9e0ca0d113
Correlations: Add transformations to Explore Editor (#75930)
* Add transformation add modal and use it

* Hook up saving

* Add transformation vars to var list, show added transformations

* Form validation

* Remove type assertion, start building out transformation data in helper (WIP)

* Style expression better, add delete logic

* Add ability to edit, additional styling on transformation card in helper

* simplify styling, conditionally run edit set up logic

* Keep more field information in function, integrate it with new editor

* Show default label on collapsed section, use deleteButton for confirmation of deleting transformations

* Change transformation add calculations from function to hook, add label to collapsed header, add transformation tooltip

* Make correlation and editor dirty state distinctive and integrate, WIP

* Track action pane for more detailed messaging/actions

* Better cancel modal logic

* Remove changes to adminsitration transformation editor

* Remove debugging line

* Remove unneeded comment

* Add in logic for closing editor mode

* Add tests for modal logic

* Use state to build vars list for helper

* WIP

* Fix labels and dirty state

* Fix bad message and stop exiting mode if discard action is performed

* Fix tests

* Update to not use unstable component and tweak default label
2023-11-02 15:09:30 -05:00

75 lines
2.4 KiB
TypeScript

import { template } from 'lodash';
import { CORRELATION_EDITOR_POST_CONFIRM_ACTION } from 'app/types';
enum CONSEQUENCES {
SOURCE_TARGET_CHANGE = 'cause the query in the right pane to be re-ran and links added to that data',
FULL_QUERY_LOSS = 'lose the changed query',
FULL_CORR_LOSS = 'cause the correlation in progress to be lost',
INVALID_VAR = 'remove the variables, and your changed query may no longer be valid',
}
// returns a string if the modal should show, with what the message string should be
// returns undefined if the modal shouldn't show
export const showModalMessage = (
action: CORRELATION_EDITOR_POST_CONFIRM_ACTION,
isActionLeft: boolean,
dirtyCorrelation: boolean,
dirtyQueryEditor: boolean
) => {
const messageTemplate = template(
'<%= actionStr %> will <%= consequenceStr %>. Would you like to save before continuing?'
);
let actionStr = '';
let consequenceStr = '';
// dirty correlation message always takes priority over dirty query
if (action === CORRELATION_EDITOR_POST_CONFIRM_ACTION.CLOSE_PANE) {
actionStr = 'Closing the pane';
if (isActionLeft) {
if (dirtyCorrelation) {
consequenceStr = CONSEQUENCES.FULL_CORR_LOSS;
} else if (dirtyQueryEditor) {
consequenceStr = CONSEQUENCES.SOURCE_TARGET_CHANGE;
} else {
return undefined;
}
} else {
// right pane close
if (dirtyCorrelation) {
consequenceStr = CONSEQUENCES.FULL_CORR_LOSS;
} else if (dirtyQueryEditor) {
consequenceStr = CONSEQUENCES.FULL_QUERY_LOSS;
} else {
return undefined;
}
}
} else if (action === CORRELATION_EDITOR_POST_CONFIRM_ACTION.CHANGE_DATASOURCE) {
actionStr = 'Changing the datasource';
if (isActionLeft) {
if (dirtyCorrelation) {
consequenceStr = CONSEQUENCES.FULL_CORR_LOSS;
} else {
return undefined;
}
} else {
// right datasource change
if (dirtyQueryEditor) {
consequenceStr = CONSEQUENCES.FULL_QUERY_LOSS;
} else {
return undefined;
}
}
} else if (action === CORRELATION_EDITOR_POST_CONFIRM_ACTION.CLOSE_EDITOR) {
actionStr = 'Closing the editor';
if (dirtyCorrelation) {
consequenceStr = CONSEQUENCES.FULL_CORR_LOSS;
} else if (dirtyQueryEditor) {
consequenceStr = CONSEQUENCES.INVALID_VAR;
} else {
return undefined;
}
}
return messageTemplate({ actionStr, consequenceStr });
};