Skip to content

Commit 94cba8d

Browse files
authored
chore(connections): replace connections registry and manager with a single interface COMPASS-8469 COMPASS-8470 (#6525)
chore(connections): replace connections registry and manager with a single interface
1 parent 32d3da5 commit 94cba8d

File tree

44 files changed

+284
-731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+284
-731
lines changed
Lines changed: 60 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { PureComponent } from 'react';
1+
import React, { useEffect } from 'react';
22
import { connect } from 'react-redux';
33
import {
44
Banner,
@@ -11,13 +11,9 @@ import {
1111
} from '@mongodb-js/compass-components';
1212
import { createView, changeViewName, close } from '../../modules/create-view';
1313
import type { CreateViewRootState } from '../../stores/create-view';
14-
import { withTelemetry } from '@mongodb-js/compass-telemetry/provider';
15-
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
16-
import {
17-
type ConnectionRepository,
18-
withConnectionRepository,
19-
type ConnectionInfo,
20-
} from '@mongodb-js/compass-connections/provider';
14+
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
15+
import type { ConnectionInfo } from '@mongodb-js/compass-connections/provider';
16+
import { useConnectionsListRef } from '@mongodb-js/compass-connections/provider';
2117

2218
const progressContainerStyles = css({
2319
display: 'flex',
@@ -32,108 +28,80 @@ type CreateViewModalProps = {
3228
name?: string;
3329
changeViewName: (name: string) => void;
3430
isDuplicating?: boolean;
35-
source?: string;
36-
pipeline?: unknown[];
3731
connectionId: ConnectionInfo['id'];
3832
isRunning?: boolean;
39-
error: Error | null;
40-
track: TrackFunction;
41-
connectionRepository: ConnectionRepository;
33+
error: string | null;
4234
};
4335

44-
class CreateViewModal extends PureComponent<CreateViewModalProps> {
45-
static defaultProps = {
46-
name: '',
47-
source: '',
48-
pipeline: [],
49-
isRunning: false,
50-
isVisible: false,
51-
isDuplicating: false,
52-
};
36+
const CreateViewModal: React.FunctionComponent<CreateViewModalProps> = ({
37+
createView,
38+
isVisible,
39+
closeModal,
40+
name,
41+
changeViewName,
42+
isDuplicating,
43+
connectionId,
44+
isRunning,
45+
error,
46+
}) => {
47+
const track = useTelemetry();
48+
const { getConnectionById } = useConnectionsListRef();
5349

54-
componentDidUpdate(prevProps: CreateViewModalProps) {
55-
if (prevProps.isVisible !== this.props.isVisible && this.props.isVisible) {
56-
const connectionInfo =
57-
this.props.connectionRepository.getConnectionInfoById(
58-
this.props.connectionId
59-
);
60-
this.props.track('Screen', { name: 'create_view_modal' }, connectionInfo);
50+
useEffect(() => {
51+
if (isVisible && connectionId) {
52+
track(
53+
'Screen',
54+
{ name: 'create_view_modal' },
55+
getConnectionById(connectionId)?.info
56+
);
6157
}
62-
}
63-
64-
onNameChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
65-
this.props.changeViewName(evt.currentTarget.value);
66-
};
67-
68-
onFormSubmit = () => {
69-
this.props.createView();
70-
};
58+
}, [isVisible, connectionId, getConnectionById, track]);
7159

72-
onCancel = () => {
73-
this.props.closeModal();
74-
};
75-
76-
/**
77-
* Render the save pipeline component.
78-
*/
79-
render() {
80-
return (
81-
<FormModal
82-
title={this.props.isDuplicating ? 'Duplicate View' : 'Create a View'}
83-
open={this.props.isVisible}
84-
onSubmit={this.onFormSubmit}
85-
onCancel={this.onCancel}
86-
submitButtonText="Create"
87-
data-testid="create-view-modal"
88-
>
89-
<TextInput
90-
data-testid="create-view-name"
91-
value={this.props.name || ''}
92-
onChange={this.onNameChange}
93-
label="Name"
94-
name="name"
95-
/>
96-
{this.props.error ? (
97-
<Banner variant="danger">{this.props.error.message}</Banner>
98-
) : null}
99-
{this.props.isRunning ? (
100-
<Body className={progressContainerStyles}>
101-
<SpinLoader />
102-
<span>Creating view&hellip;</span>
103-
</Body>
104-
) : null}
105-
</FormModal>
106-
);
107-
}
108-
}
60+
return (
61+
<FormModal
62+
title={isDuplicating ? 'Duplicate View' : 'Create a View'}
63+
open={isVisible}
64+
onSubmit={createView}
65+
onCancel={closeModal}
66+
submitButtonText="Create"
67+
data-testid="create-view-modal"
68+
>
69+
<TextInput
70+
data-testid="create-view-name"
71+
value={name || ''}
72+
onChange={(evt) => {
73+
changeViewName(evt.currentTarget.value);
74+
}}
75+
label="Name"
76+
name="name"
77+
/>
78+
{error ? <Banner variant="danger">{error}</Banner> : null}
79+
{isRunning ? (
80+
<Body className={progressContainerStyles}>
81+
<SpinLoader />
82+
<span>Creating view&hellip;</span>
83+
</Body>
84+
) : null}
85+
</FormModal>
86+
);
87+
};
10988

110-
/**
111-
* Map the store state to properties to pass to the components.
112-
*/
11389
const mapStateToProps = (state: CreateViewRootState) => ({
11490
isRunning: state.isRunning,
11591
isVisible: state.isVisible,
11692
isDuplicating: state.isDuplicating,
11793
name: state.name,
118-
error: state.error,
94+
error: state.error?.message ?? null,
11995
source: state.source,
12096
pipeline: state.pipeline,
12197
connectionId: state.connectionId,
12298
});
12399

124-
/**
125-
* Connect the redux store to the component.
126-
* (dispatch)
127-
*/
128-
const MappedCreateViewModal = withTelemetry(
129-
withConnectionRepository(
130-
connect(mapStateToProps, {
131-
createView,
132-
changeViewName,
133-
closeModal: close,
134-
})(CreateViewModal)
135-
)
136-
);
100+
const MappedCreateViewModal = connect(mapStateToProps, {
101+
createView,
102+
changeViewName,
103+
closeModal: close,
104+
})(CreateViewModal);
137105

138106
export default MappedCreateViewModal;
139107
export { CreateViewModal };

packages/compass-aggregations/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import CreateViewModal from './components/create-view-modal';
99
import {
1010
connectionInfoRefLocator,
1111
connectionScopedAppRegistryLocator,
12-
connectionsManagerLocator,
12+
connectionsLocator,
1313
dataServiceLocator,
1414
type DataServiceLocator,
1515
} from '@mongodb-js/compass-connections/provider';
@@ -27,7 +27,6 @@ import { workspacesServiceLocator } from '@mongodb-js/compass-workspaces/provide
2727
import { preferencesLocator } from 'compass-preferences-model/provider';
2828
import { atlasAiServiceLocator } from '@mongodb-js/compass-generative-ai/provider';
2929
import { pipelineStorageLocator } from '@mongodb-js/my-queries-storage/provider';
30-
import { connectionRepositoryAccessLocator } from '@mongodb-js/compass-connections/provider';
3130
import { AggregationsTabTitle } from './plugin-title';
3231

3332
const CompassAggregationsHadronPlugin = registerHadronPlugin(
@@ -71,8 +70,7 @@ export const CreateViewPlugin = registerHadronPlugin(
7170
activate: activateCreateViewPlugin,
7271
},
7372
{
74-
connectionsManager: connectionsManagerLocator,
75-
connectionRepository: connectionRepositoryAccessLocator,
73+
connections: connectionsLocator,
7674
logger: createLoggerLocator('COMPASS-CREATE-VIEW-UI'),
7775
track: telemetryLocator,
7876
workspaces: workspacesServiceLocator,

packages/compass-aggregations/src/modules/create-view/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,7 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
202202
return async (
203203
dispatch,
204204
getState,
205-
{
206-
globalAppRegistry,
207-
connectionsManager,
208-
connectionRepository,
209-
track,
210-
workspaces,
211-
}
205+
{ globalAppRegistry, connections, track, workspaces }
212206
) => {
213207
const {
214208
name: viewName,
@@ -222,8 +216,7 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
222216
dispatch(clearError());
223217

224218
try {
225-
const dataService =
226-
connectionsManager.getDataServiceForConnection(connectionId);
219+
const dataService = connections.getDataServiceForConnection(connectionId);
227220

228221
dispatch(toggleIsRunning(true));
229222
await dataService.createView(
@@ -236,7 +229,7 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
236229
track(
237230
'Aggregation Saved As View',
238231
{ num_stages: viewPipeline.length },
239-
connectionRepository.getConnectionInfoById(connectionId)
232+
connections.getConnectionById(connectionId)?.info
240233
);
241234
globalAppRegistry.emit('view-created', ns, {
242235
connectionId,

packages/compass-aggregations/src/stores/create-view.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ import reducer, { open } from '../modules/create-view';
77
import type AppRegistry from 'hadron-app-registry';
88
import type { Logger } from '@mongodb-js/compass-logging/provider';
99
import type { WorkspacesService } from '@mongodb-js/compass-workspaces/provider';
10-
import type {
11-
ConnectionRepositoryAccess,
12-
ConnectionsManager,
13-
} from '@mongodb-js/compass-connections/provider';
10+
import type { ConnectionsService } from '@mongodb-js/compass-connections/provider';
1411
import type { ActivateHelpers } from 'hadron-app-registry';
1512
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
1613

1714
type CreateViewServices = {
1815
globalAppRegistry: AppRegistry;
19-
connectionsManager: ConnectionsManager;
20-
connectionRepository: ConnectionRepositoryAccess;
16+
connections: ConnectionsService;
2117
logger: Logger;
2218
track: TrackFunction;
2319
workspaces: WorkspacesService;
@@ -53,8 +49,7 @@ export function activateCreateViewPlugin(
5349
_: unknown,
5450
{
5551
globalAppRegistry,
56-
connectionsManager,
57-
connectionRepository,
52+
connections,
5853
logger,
5954
track,
6055
workspaces,
@@ -63,8 +58,7 @@ export function activateCreateViewPlugin(
6358
) {
6459
const store = configureStore({
6560
globalAppRegistry,
66-
connectionsManager,
67-
connectionRepository,
61+
connections,
6862
logger,
6963
track,
7064
workspaces,

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import type { ActivateHelpers } from 'hadron-app-registry';
66
import { registerHadronPlugin } from 'hadron-app-registry';
77
import { MongoDBInstancesManagerContext } from './provider';
88
import { createInstancesStore } from './stores';
9-
import {
10-
connectionsManagerLocator,
11-
type ConnectionsManager,
12-
} from '@mongodb-js/compass-connections/provider';
9+
import type { ConnectionsService } from '@mongodb-js/compass-connections/provider';
10+
import { connectionsLocator } from '@mongodb-js/compass-connections/provider';
1311
import { type MongoDBInstancesManager } from './instances-manager';
1412

1513
interface MongoDBInstancesProviderProps {
@@ -37,19 +35,19 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
3735
activate(
3836
_: unknown,
3937
{
40-
connectionsManager,
38+
connections,
4139
logger,
4240
globalAppRegistry,
4341
}: {
44-
connectionsManager: ConnectionsManager;
42+
connections: ConnectionsService;
4543
logger: Logger;
4644
globalAppRegistry: AppRegistry;
4745
},
4846
helpers: ActivateHelpers
4947
) {
5048
const store = createInstancesStore(
5149
{
52-
connectionsManager,
50+
connections,
5351
logger,
5452
globalAppRegistry,
5553
},
@@ -65,6 +63,6 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
6563
},
6664
{
6765
logger: createLoggerLocator('COMPASS-INSTANCE-STORE'),
68-
connectionsManager: connectionsManagerLocator,
66+
connections: connectionsLocator,
6967
}
7068
);

0 commit comments

Comments
 (0)