From bba2d01df0c541ca8cfe89753098ff42919036ab Mon Sep 17 00:00:00 2001 From: Matthew Runyon Date: Thu, 30 Mar 2023 11:08:52 -0500 Subject: [PATCH] fix: Save or discard a changed notebook does not close modal on first click (#1188) Fixes #1187 --- .../src/panels/NotebookPanel.tsx | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/dashboard-core-plugins/src/panels/NotebookPanel.tsx b/packages/dashboard-core-plugins/src/panels/NotebookPanel.tsx index d070296fc6..4aafc40bae 100644 --- a/packages/dashboard-core-plugins/src/panels/NotebookPanel.tsx +++ b/packages/dashboard-core-plugins/src/panels/NotebookPanel.tsx @@ -119,8 +119,6 @@ interface NotebookPanelState { scriptCode: string; itemName?: string; - - shouldPromptClose: boolean; } class NotebookPanel extends Component { @@ -274,8 +272,6 @@ class NotebookPanel extends Component { showSaveAsModal: false, scriptCode: '', - - shouldPromptClose: true, }; log.debug('constructor', props, this.state); @@ -348,6 +344,13 @@ class NotebookPanel extends Component { this.initTabClasses(tab); } + /** + * Adds a beforeClose handler to check if a notebook needs to be saved + * Call panel close with force if the check can be skipped + * + * Note that firing a close event manually may trigger before state update occurs + * In those instances, use force + */ initTabCloseOverride() { const { glContainer } = this.props; glContainer.beforeClose((options?: CloseOptions) => { @@ -355,8 +358,8 @@ class NotebookPanel extends Component { return true; } - const { changeCount, savedChangeCount, shouldPromptClose } = this.state; - if (changeCount !== savedChangeCount && shouldPromptClose) { + const { changeCount, savedChangeCount } = this.state; + if (changeCount !== savedChangeCount) { this.setState({ showCloseModal: true }); return false; } @@ -589,21 +592,21 @@ class NotebookPanel extends Component { } handleCloseDiscard(): void { - this.setState({ shouldPromptClose: false, showCloseModal: false }); + this.setState({ showCloseModal: false }); const { glContainer } = this.props; - glContainer.close(); + glContainer.close({ force: true }); } handleCloseSave(): void { - this.setState({ shouldPromptClose: false, showCloseModal: false }); + this.setState({ showCloseModal: false }); if (this.save()) { const { glContainer } = this.props; - glContainer.close(); + glContainer.close({ force: true }); } } handleCloseCancel(): void { - this.setState({ shouldPromptClose: true, showCloseModal: false }); + this.setState({ showCloseModal: false }); } /** @@ -1032,7 +1035,7 @@ class NotebookPanel extends Component { } runCommand(command?: string): void { - if (command === undefined) { + if (command === undefined || command === '') { log.debug('Ignoring empty command.'); return; }