Skip to content

Commit

Permalink
feat(compass-connections): useConnectionSupports(connectionId, connec…
Browse files Browse the repository at this point in the history
…tionFeature) hook COMPASS-8213 (#6185)

* useConnectionSupports hook

* tests

* use a selector
  • Loading branch information
lerouxb authored Sep 3, 2024
1 parent 610f668 commit a7496ff
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 1 deletion.
161 changes: 161 additions & 0 deletions packages/compass-connections/src/hooks/use-connection-supports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { useConnectionSupports } from './use-connection-supports';
import { type ConnectionInfo } from '@mongodb-js/connection-storage/provider';
import { expect } from 'chai';
import { renderHookWithConnections } from '../test';

const mockConnections: ConnectionInfo[] = [
{
id: 'no-atlasMetadata',
connectionOptions: {
connectionString: 'mongodb://foo',
},
},
{
id: 'host-cluster',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
clusterId: 'clusterId',
clusterType: 'host',
instanceSize: 'M10',
},
},
{
id: 'free-cluster',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
clusterId: 'clusterId',
clusterType: 'replicaSet',
instanceSize: 'M0',
},
},
{
id: 'serverless-cluster',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
clusterId: 'clusterId',
clusterType: 'serverless',
instanceSize: 'SERVERLESS_V2',
},
},
{
id: 'dedicated-replicaSet',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
clusterId: 'clusterId',
clusterType: 'replicaSet',
instanceSize: 'M10',
},
},
{
id: 'dedicated-sharded',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
clusterId: 'clusterId',
clusterType: 'cluster',
instanceSize: 'M10',
},
},
];

describe('useConnectionSupports', function () {
it('should return false if the connection does not exist', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('does-not-exist', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false if the connection has no atlasMetadata', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('no-atlasMetadata', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false for host cluster type', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('host-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false for serverless cluster type', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('serverless-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false for free/shared tier clusters', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('free-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return true for dedicated replicaSet clusters', function () {
const { result } = renderHookWithConnections(
() =>
useConnectionSupports('dedicated-replicaSet', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
});

it('should return true for dedicated sharded clusters', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('dedicated-sharded', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
});
});
45 changes: 45 additions & 0 deletions packages/compass-connections/src/hooks/use-connection-supports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useSelector } from '../stores/store-context';
import type { ConnectionState } from '../stores/connections-store-redux';

// only one for now
type ConnectionFeature = 'rollingIndexCreation';

function isFreeOrSharedTierCluster(instanceSize: string | undefined): boolean {
if (!instanceSize) {
return false;
}

return ['M0', 'M2', 'M5'].includes(instanceSize);
}

function supportsRollingIndexCreation(connection: ConnectionState) {
const atlasMetadata = connection.info?.atlasMetadata;

if (!atlasMetadata) {
return false;
}

const { clusterType, instanceSize } = atlasMetadata;
return (
!isFreeOrSharedTierCluster(instanceSize) &&
(clusterType === 'cluster' || clusterType === 'replicaSet')
);
}
export function useConnectionSupports(
connectionId: string,
connectionFeature: ConnectionFeature
): boolean {
return useSelector((state) => {
const connection = state.connections.byId[connectionId];

if (!connection) {
return false;
}

if (connectionFeature === 'rollingIndexCreation') {
return supportsRollingIndexCreation(connection);
}

return false;
});
}
2 changes: 1 addition & 1 deletion packages/compass-connections/src/stores/store-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const useDispatch = createDispatchHook(
/**
* @internal should not be directly exported from this package
*/
const useSelector: TypedUseSelectorHook<ConnectionsStore['state']> =
export const useSelector: TypedUseSelectorHook<ConnectionsStore['state']> =
createSelectorHook(ConnectionsStoreContext);

export const connect = ((
Expand Down

0 comments on commit a7496ff

Please sign in to comment.