Skip to content

Commit 9e1365c

Browse files
committed
Implement reusable useHasNonGenuineConnections hook
1 parent 0d287d9 commit 9e1365c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

packages/compass-app-stores/src/provider.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
dataServiceLocator,
33
useConnectionInfo,
4+
useConnectionIds,
45
} from '@mongodb-js/compass-connections/provider';
56
import {
67
createServiceLocator,
@@ -10,6 +11,7 @@ import type { MongoDBInstanceProps } from 'mongodb-instance-model';
1011
import { MongoDBInstance } from 'mongodb-instance-model';
1112
import React, {
1213
createContext,
14+
useCallback,
1315
useContext,
1416
useEffect,
1517
useMemo,
@@ -93,6 +95,46 @@ export const mongoDBInstanceLocator = createServiceLocator(
9395
'mongoDBInstanceLocator'
9496
);
9597

98+
/**
99+
* Hook to check if there are any connected non-genuine MongoDB connections.
100+
* @return true if any connected connection is to a non-genuine MongoDB server.
101+
*/
102+
export function useHasNonGenuineConnections(): boolean {
103+
const instancesManager = useContext(MongoDBInstancesManagerContext);
104+
105+
const hasNonGenuineConnections = useCallback(
106+
function () {
107+
if (!instancesManager) {
108+
return false;
109+
}
110+
return Array.from(instancesManager.listMongoDBInstances().values()).some(
111+
(instance) => {
112+
return !instance.genuineMongoDB.isGenuine;
113+
}
114+
);
115+
},
116+
[instancesManager]
117+
);
118+
119+
const [result, setResult] = useState(hasNonGenuineConnections);
120+
121+
useEffect(() => {
122+
if (instancesManager) {
123+
const updateResult = () => {
124+
setResult(hasNonGenuineConnections);
125+
};
126+
instancesManager.on('instance-started', updateResult);
127+
instancesManager.on('instance-removed', updateResult);
128+
return () => {
129+
instancesManager.off('instance-started', updateResult);
130+
instancesManager.off('instance-removed', updateResult);
131+
};
132+
}
133+
}, [instancesManager, setResult, hasNonGenuineConnections]);
134+
135+
return result;
136+
}
137+
96138
const NamespaceModelContext = React.createContext<Database | Collection | null>(
97139
null
98140
);

0 commit comments

Comments
 (0)