Skip to content

Commit 8512aa7

Browse files
authored
fix(web, connections): filter out paused clusters; cleanup connections on plugin deactivate COMPASS-8510 CLOUDP-284226 (#6500)
* fix(web, connections): filter out paused clusters; cleanup connections on plugin deactivate * chore(connections): fix test
1 parent 206341c commit 8512aa7

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import {
3+
createDefaultConnectionInfo,
4+
render,
5+
} from '@mongodb-js/testing-library-compass';
6+
import { expect } from 'chai';
7+
8+
describe('CompassConnections', function () {
9+
it('cleans-up connections when unmounted', async function () {
10+
const conn1 = createDefaultConnectionInfo();
11+
const conn2 = createDefaultConnectionInfo();
12+
13+
const result = render(
14+
<div>
15+
{/* it's a bit weird, but testing-library-compass already renders CompassConnections for us */}
16+
</div>,
17+
{ connections: [conn1, conn2] }
18+
);
19+
20+
await result.connectionsStore.actions.connect(conn1);
21+
22+
expect(
23+
result.connectionsStore.getState().connections.byId[conn1.id]
24+
).to.have.property('status', 'connected');
25+
26+
result.unmount();
27+
28+
expect(
29+
result.connectionsStore.getState().connections.byId[conn1.id]
30+
).to.have.property('status', 'disconnected');
31+
});
32+
});

packages/compass-connections/src/index.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { registerHadronPlugin } from 'hadron-app-registry';
22
import {
33
autoconnectCheck,
44
configureStore,
5+
disconnect,
56
loadConnections,
67
} from './stores/connections-store-redux';
78
import React, { useContext, useRef } from 'react';
@@ -46,7 +47,7 @@ const CompassConnectionsPlugin = registerHadronPlugin(
4647
activate(
4748
initialProps,
4849
{ logger, preferences, connectionStorage, track },
49-
helpers
50+
{ addCleanup, cleanup }
5051
) {
5152
const store = configureStore(initialProps.preloadStorageConnectionInfos, {
5253
logger,
@@ -67,9 +68,16 @@ const CompassConnectionsPlugin = registerHadronPlugin(
6768
}
6869
});
6970

71+
// Stop all connections on disconnect
72+
addCleanup(() => {
73+
for (const connectionId of store.getState().connections.ids) {
74+
store.dispatch(disconnect(connectionId));
75+
}
76+
});
77+
7078
return {
7179
store,
72-
deactivate: helpers.cleanup,
80+
deactivate: cleanup,
7381
context: ConnectionsStoreContext,
7482
};
7583
},

packages/compass-connections/src/stores/connections-store-redux.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,10 @@ function getCurrentConnectionInfo(
14231423
return state.connections.byId[connectionId]?.info;
14241424
}
14251425

1426+
function getCurrentConnectionStatus(state: State, connectionId: ConnectionId) {
1427+
return state.connections.byId[connectionId]?.status;
1428+
}
1429+
14261430
/**
14271431
* Returns the number of active connections. We count in-progress connections
14281432
* as "active" to make sure that the maximum connection allowed check takes
@@ -1967,15 +1971,21 @@ const cleanupConnection = (
19671971
'Initiating disconnect attempt'
19681972
);
19691973

1974+
const currentStatus = getCurrentConnectionStatus(getState(), connectionId);
1975+
19701976
// We specifically want to track Disconnected even when it's not really
19711977
// triggered by user at all, so we put it in the cleanup function that is
19721978
// called every time you disconnect, or remove a connection, or all of them,
1973-
// or close the app
1974-
track(
1975-
'Connection Disconnected',
1976-
{},
1977-
getCurrentConnectionInfo(getState(), connectionId)
1978-
);
1979+
// or close the app. Only track when connection is either connected or
1980+
// connecting, we might be calling this on something that was never
1981+
// connected
1982+
if (currentStatus === 'connected' || currentStatus === 'connecting') {
1983+
track(
1984+
'Connection Disconnected',
1985+
{},
1986+
getCurrentConnectionInfo(getState(), connectionId)
1987+
);
1988+
}
19791989

19801990
const { closeConnectionStatusToast } = getNotificationTriggers(
19811991
preferences.getPreferences().enableMultipleConnectionSystem

packages/compass-connections/src/stores/connections-store.spec.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ describe('useConnections', function () {
470470
preferences: defaultPreferences,
471471
});
472472

473+
await connectionsStore.actions.connect(mockConnections[0]);
474+
473475
result.current.removeConnection(mockConnections[0].id);
474476

475477
await waitFor(() => {

packages/compass-web/src/connection-storage.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ClusterDescription = {
3737
state: string;
3838
deploymentItemName: string;
3939
replicationSpecList?: ReplicationSpec[];
40+
isPaused?: boolean;
4041
};
4142

4243
export type ClusterDescriptionWithDataProcessingRegion = ClusterDescription & {
@@ -250,7 +251,7 @@ class AtlasCloudConnectionStorage
250251
// account in the UI for a special state of a deployment as
251252
// clusters can become inactive during their runtime and it's
252253
// valuable UI info to display
253-
return !!description.srvAddress;
254+
return !description.isPaused && !!description.srvAddress;
254255
})
255256
.map(async (description) => {
256257
// Even though nds/clusters will list serverless clusters, to get

0 commit comments

Comments
 (0)