Skip to content

Commit 3aa2235

Browse files
authored
Add prompt when closing tab (#121)
- Add onbeforeunload listener at the app level to prompt when closing the tab - For testing, you need to have interacted with the page (click or keyboard) or else prompt will not show - Removed unused disconnect/restart session wiring Fixes #84
1 parent 7f15859 commit 3aa2235

File tree

5 files changed

+21
-51
lines changed

5 files changed

+21
-51
lines changed

packages/code-studio/src/dashboard/event-handlers/ConsoleEventHandler.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class ConsoleEventHandler {
1515

1616
this.handleSendCommand = this.handleSendCommand.bind(this);
1717
this.handleSettingsChanged = this.handleSettingsChanged.bind(this);
18-
this.handleDisconnectSession = this.handleDisconnectSession.bind(this);
19-
this.handleRestartSession = this.handleRestartSession.bind(this);
2018

2119
this.initialize();
2220
}
@@ -150,34 +148,16 @@ class ConsoleEventHandler {
150148
this.onSettingsChanged({ consoleCreatorSettings, consoleSettings });
151149
}
152150

153-
handleDisconnectSession() {
154-
const consolePanel = this.getConsolePanel();
155-
if (consolePanel && consolePanel.consoleContainerRef) {
156-
consolePanel.consoleContainerRef.closeSessionAndUpdateState();
157-
}
158-
}
159-
160-
handleRestartSession() {
161-
const consolePanel = this.getConsolePanel();
162-
if (consolePanel && consolePanel.consoleContainerRef) {
163-
consolePanel.consoleContainerRef.restartSession();
164-
}
165-
}
166-
167151
startListening() {
168152
const { eventHub } = this.layout;
169153
eventHub.on(ConsoleEvent.SEND_COMMAND, this.handleSendCommand);
170154
eventHub.on(ConsoleEvent.SETTINGS_CHANGED, this.handleSettingsChanged);
171-
eventHub.on(ConsoleEvent.DISCONNECT_SESSION, this.handleDisconnectSession);
172-
eventHub.on(ConsoleEvent.RESTART_SESSION, this.handleRestartSession);
173155
}
174156

175157
stopListening() {
176158
const { eventHub } = this.layout;
177159
eventHub.off(ConsoleEvent.SEND_COMMAND, this.handleSendCommand);
178160
eventHub.off(ConsoleEvent.SETTINGS_CHANGED, this.handleSettingsChanged);
179-
eventHub.off(ConsoleEvent.DISCONNECT_SESSION, this.handleDisconnectSession);
180-
eventHub.off(ConsoleEvent.RESTART_SESSION, this.handleRestartSession);
181161
}
182162
}
183163

packages/code-studio/src/dashboard/events/ConsoleEvent.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ class ConsoleEvent {
88
static SEND_COMMAND = 'ConsoleEvent.SEND_COMMAND';
99

1010
static SETTINGS_CHANGED = 'ConsoleEvent.SETTINGS_CHANGED';
11-
12-
static DISCONNECT_SESSION = 'ConsoleEvent.DISCONNECT_SESSION';
13-
14-
static RESTART_SESSION = 'ConsoleEvent.RESTART_SESSION';
1511
}
1612

1713
export default ConsoleEvent;

packages/code-studio/src/dashboard/events/TabEvent.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class TabEvent {
1010
static reload = 'TabEvent.reload';
1111

1212
static clearAllFilters = 'TabEvent.clearAllFilters';
13-
14-
static DISCONNECT_SESSION = 'TabEvent.DISCONNECT_SESSION';
15-
16-
static RESTART_SESSION = 'TabEvent.RESTART_SESSION';
1713
}
1814

1915
export default TabEvent;

packages/code-studio/src/main/AppMainContainer.jsx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ import {
2121
} from '@deephaven/redux';
2222
import { PromiseUtils } from '@deephaven/utils';
2323
import SettingsMenu from '../settings/SettingsMenu';
24-
import {
25-
ConsoleEvent,
26-
ControlEvent,
27-
InputFilterEvent,
28-
} from '../dashboard/events';
24+
import { ControlEvent, InputFilterEvent } from '../dashboard/events';
2925
import ToolType from '../tools/ToolType';
3026
import { IrisPropTypes } from '../include/prop-types';
3127
import AppControlsMenu from './AppControlsMenu';
@@ -91,6 +87,14 @@ const DEFAULT_LAYOUT_CONFIG = [
9187
];
9288

9389
export class AppMainContainer extends Component {
90+
static handleWindowBeforeUnload(event) {
91+
event.preventDefault();
92+
// returnValue is required for beforeReload event prompt
93+
// https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
94+
// eslint-disable-next-line no-param-reassign
95+
event.returnValue = '';
96+
}
97+
9498
constructor(props) {
9599
super(props);
96100
this.handleSettingsMenuHide = this.handleSettingsMenuHide.bind(this);
@@ -109,16 +113,22 @@ export class AppMainContainer extends Component {
109113
this.state = { showSettingsMenu: false };
110114
}
111115

112-
sendClearFilter() {
113-
this.emitLayoutEvent(InputFilterEvent.CLEAR_ALL_FILTERS);
116+
componentDidMount() {
117+
window.addEventListener(
118+
'beforeunload',
119+
AppMainContainer.handleWindowBeforeUnload
120+
);
114121
}
115122

116-
sendDisconnectSession() {
117-
this.emitLayoutEvent(ConsoleEvent.DISCONNECT_SESSION);
123+
componentWillUnmount() {
124+
window.removeEventListener(
125+
'beforeunload',
126+
AppMainContainer.handleWindowBeforeUnload
127+
);
118128
}
119129

120-
sendRestartSession() {
121-
this.emitLayoutEvent(ConsoleEvent.RESTART_SESSION);
130+
sendClearFilter() {
131+
this.emitLayoutEvent(InputFilterEvent.CLEAR_ALL_FILTERS);
122132
}
123133

124134
emitLayoutEvent(event, data = undefined) {
@@ -256,16 +266,6 @@ export class AppMainContainer extends Component {
256266
},
257267
shortcut: GLOBAL_SHORTCUTS.SAVE,
258268
},
259-
{
260-
action: () => {
261-
this.sendRestartSession();
262-
},
263-
},
264-
{
265-
action: () => {
266-
this.sendDisconnectSession();
267-
},
268-
},
269269
];
270270

271271
const tabBarMenu = (

packages/console/src/Console.test.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ function makeConsoleWrapper() {
1717
const wrapper = shallow(
1818
<Console
1919
commandHistoryStorage={commandHistoryStorage}
20-
closeSession={() => {}}
21-
restartSession={() => {}}
2220
focusCommandHistory={() => {}}
2321
openObject={() => {}}
2422
closeObject={() => {}}

0 commit comments

Comments
 (0)