11import { css } from '@emotion/css' ;
2- import React , { useCallback , useEffect , useReducer } from 'react' ;
2+ import React , { useCallback , useEffect , useReducer , useRef } from 'react' ;
33import {
44 ConnectionInfo ,
55 DataService ,
@@ -19,6 +19,7 @@ import {
1919 useAppRegistryRole ,
2020} from '../contexts/app-registry-context' ;
2121import updateTitle from '../modules/update-title' ;
22+ import ipc from 'hadron-ipc' ;
2223
2324const homeViewStyles = css ( {
2425 display : 'flex' ,
@@ -124,16 +125,14 @@ function reducer(state: State, action: Action): State {
124125}
125126
126127function hideCollectionSubMenu ( ) {
127- void import ( 'hadron-ipc' ) . then ( ( { ipcRenderer } ) => {
128- if ( ipcRenderer ) {
129- ipcRenderer . call ( 'window:hide-collection-submenu' ) ;
130- }
131- } ) ;
128+ void ipc . ipcRenderer ?. call ( 'window:hide-collection-submenu' ) ;
132129}
133130
134131function Home ( { appName } : { appName : string } ) : React . ReactElement | null {
135132 const appRegistry = useAppRegistryContext ( ) ;
136133 const connectRole = useAppRegistryRole ( AppRegistryRoles . APPLICATION_CONNECT ) ;
134+ const connectedDataService = useRef < DataService > ( ) ;
135+ const showNewConnectForm = process . env . USE_NEW_CONNECT_FORM === 'true' ;
137136
138137 const [
139138 {
@@ -152,6 +151,7 @@ function Home({ appName }: { appName: string }): React.ReactElement | null {
152151 ds : DataService ,
153152 connectionInfo : ConnectionInfo
154153 ) {
154+ connectedDataService . current = ds ;
155155 dispatch ( {
156156 type : 'connected' ,
157157 connectionTitle : getConnectionTitle ( connectionInfo ) || '' ,
@@ -282,7 +282,37 @@ function Home({ appName }: { appName: string }): React.ReactElement | null {
282282 } , [ isConnected , appName , connectionTitle , namespace ] ) ;
283283
284284 useEffect ( ( ) => {
285- // Setup listeners.
285+ async function handleDisconnectClicked ( ) {
286+ if ( ! connectedDataService . current ) {
287+ // We aren't connected.
288+ return ;
289+ }
290+
291+ await connectedDataService . current . disconnect ( ) ;
292+ connectedDataService . current = undefined ;
293+
294+ appRegistry . emit ( 'data-service-disconnected' ) ;
295+ }
296+
297+ function onDisconnect ( ) {
298+ void handleDisconnectClicked ( ) ;
299+ }
300+
301+ // TODO: Once we merge https://jira.mongodb.org/browse/COMPASS-5302
302+ // we can remove this check and handle the disconnect event here by default.
303+ if ( showNewConnectForm ) {
304+ // Setup ipc listener.
305+ ipc . ipcRenderer ?. on ( 'app:disconnect' , onDisconnect ) ;
306+
307+ return ( ) => {
308+ // Clean up the ipc listener.
309+ ipc . ipcRenderer ?. removeListener ( 'app:disconnect' , onDisconnect ) ;
310+ } ;
311+ }
312+ } , [ appRegistry , showNewConnectForm , onDataServiceDisconnected ] ) ;
313+
314+ useEffect ( ( ) => {
315+ // Setup app registry listeners.
286316 appRegistry . on ( 'instance-created' , onInstanceCreated ) ;
287317 appRegistry . on ( 'data-service-connected' , onDataServiceConnected ) ;
288318 appRegistry . on ( 'data-service-disconnected' , onDataServiceDisconnected ) ;
@@ -293,7 +323,7 @@ function Home({ appName }: { appName: string }): React.ReactElement | null {
293323 appRegistry . on ( 'all-collection-tabs-closed' , onAllTabsClosed ) ;
294324
295325 return ( ) => {
296- // Clean up the listeners.
326+ // Clean up the app registry listeners.
297327 appRegistry . removeListener ( 'instance-created' , onInstanceCreated ) ;
298328 appRegistry . removeListener (
299329 'data-service-connected' ,
@@ -325,8 +355,6 @@ function Home({ appName }: { appName: string }): React.ReactElement | null {
325355 ) ;
326356 }
327357
328- const showNewConnectForm = process . env . USE_NEW_CONNECT_FORM === 'true' ;
329-
330358 if ( showNewConnectForm ) {
331359 return (
332360 < div className = { homeViewStyles } data-test-id = "home-view" >
0 commit comments