Skip to content

Commit 2b11c54

Browse files
committed
Implement reusable useHasNonGenuineConnections hook
1 parent f89f301 commit 2b11c54

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { MongoDBInstanceProps } from 'mongodb-instance-model';
1010
import { MongoDBInstance } from 'mongodb-instance-model';
1111
import React, {
1212
createContext,
13+
useCallback,
1314
useContext,
1415
useEffect,
1516
useMemo,
@@ -93,6 +94,46 @@ export const mongoDBInstanceLocator = createServiceLocator(
9394
'mongoDBInstanceLocator'
9495
);
9596

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

0 commit comments

Comments
 (0)