Skip to content

Commit 072d079

Browse files
addaleaxgribnoysup
andauthored
chore(compass-shell): convert to new plugin interface COMPASS-7399 (#5086)
Co-authored-by: Sergey Petushkov <[email protected]>
1 parent d61268a commit 072d079

File tree

12 files changed

+196
-159
lines changed

12 files changed

+196
-159
lines changed

package-lock.json

+9-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-home/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@mongodb-js/compass-databases-collections": "^1.19.1",
4646
"@mongodb-js/compass-find-in-page": "^4.19.1",
4747
"@mongodb-js/compass-import-export": "^7.19.1",
48+
"@mongodb-js/compass-shell": "^3.19.1",
4849
"compass-preferences-model": "^2.15.6",
4950
"hadron-app-registry": "^9.0.14",
5051
"hadron-ipc": "^3.2.4",
@@ -62,6 +63,7 @@
6263
"@mongodb-js/compass-databases-collections": "^1.19.1",
6364
"@mongodb-js/compass-find-in-page": "^4.19.1",
6465
"@mongodb-js/compass-import-export": "^7.19.1",
66+
"@mongodb-js/compass-shell": "^3.19.1",
6567
"compass-preferences-model": "^2.15.6",
6668
"hadron-app-registry": "^9.0.14",
6769
"hadron-ipc": "^3.2.4",

packages/compass-home/src/components/home.spec.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,26 @@ const getComponent = (name: string) => {
2222
return TestComponent;
2323
};
2424

25-
const createDataService = () => ({});
25+
const createDataService = () => ({
26+
getConnectionString() {
27+
return { hosts: ['localhost:27020'] };
28+
},
29+
getConnectionOptions() {
30+
return {};
31+
},
32+
getMongoClientConnectionOptions() {
33+
return {};
34+
},
35+
getLastSeenTopology() {
36+
return {
37+
type: 'Unknown',
38+
servers: [],
39+
setName: 'foo',
40+
};
41+
},
42+
on() {},
43+
off() {},
44+
});
2645

2746
describe('Home [Component]', function () {
2847
before(function () {
@@ -112,6 +131,7 @@ describe('Home [Component]', function () {
112131
);
113132
dataServiceDisconnectedSpy = sinon.fake.resolves(true);
114133
const dataService = {
134+
...createDataService(),
115135
disconnect: dataServiceDisconnectedSpy,
116136
addReauthenticationHandler: sinon.stub(),
117137
};

packages/compass-home/src/components/workspace.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { css } from '@mongodb-js/compass-components';
44
import WorkspaceContent from './workspace-content';
55
import type Namespace from '../types/namespace';
66
import { useAppRegistryComponent } from 'hadron-app-registry';
7+
import { CompassShellPlugin } from '@mongodb-js/compass-shell';
78

89
const verticalSplitStyles = css({
910
width: '100vw',
@@ -40,7 +41,6 @@ export default function Workspace({
4041
namespace: Namespace;
4142
}): React.ReactElement {
4243
const SidebarComponent = useAppRegistryComponent('Sidebar.Component');
43-
const GlobalShellComponent = useAppRegistryComponent('Global.Shell');
4444

4545
return (
4646
<>
@@ -54,7 +54,7 @@ export default function Workspace({
5454
</div>
5555
</div>
5656
<div className={shellContainerStyles}>
57-
{GlobalShellComponent && <GlobalShellComponent />}
57+
<CompassShellPlugin />
5858
</div>
5959
</div>
6060
</>

packages/compass-shell/package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"main": "dist/index.js",
2727
"compass:main": "src/index.ts",
28+
"types": "dist/src/index.d.ts",
2829
"exports": {
2930
"browser": "./dist/browser.js",
3031
"require": "./dist/index.js"
@@ -33,9 +34,11 @@
3334
".": "./src/index.ts"
3435
},
3536
"scripts": {
37+
"bootstrap": "npm run postcompile",
3638
"prepublishOnly": "npm run compile && compass-scripts check-exports-exist",
3739
"compile": "npm run webpack -- --mode production",
3840
"webpack": "webpack-compass",
41+
"postcompile": "tsc --emitDeclarationOnly",
3942
"start": "npm run webpack serve -- --mode development",
4043
"analyze": "npm run webpack -- --mode production --analyze",
4144
"typecheck": "tsc --noEmit",
@@ -58,18 +61,20 @@
5861
"@mongodb-js/compass-components": "^1.19.0",
5962
"@mongodb-js/compass-logging": "^1.2.6",
6063
"@mongodb-js/compass-utils": "^0.5.5",
61-
"@mongodb-js/mongodb-redux-common": "^2.0.15",
6264
"@mongosh/node-runtime-worker-thread": "^2.0.2",
63-
"compass-preferences-model": "^2.15.6"
65+
"compass-preferences-model": "^2.15.6",
66+
"hadron-app-registry": "^9.0.14",
67+
"mongodb-data-service": "^22.15.1"
6468
},
6569
"peerDependencies": {
6670
"@mongodb-js/compass-user-data": "^0.1.9",
6771
"@mongodb-js/compass-components": "^1.19.0",
6872
"@mongodb-js/compass-logging": "^1.2.6",
6973
"@mongodb-js/compass-utils": "^0.5.5",
70-
"@mongodb-js/mongodb-redux-common": "^2.0.15",
7174
"@mongosh/node-runtime-worker-thread": "^2.0.2",
7275
"compass-preferences-model": "^2.15.6",
76+
"hadron-app-registry": "^9.0.14",
77+
"mongodb-data-service": "^22.15.1",
7378
"react": "^17.0.2"
7479
},
7580
"devDependencies": {
@@ -85,9 +90,7 @@
8590
"electron": "^25.9.3",
8691
"enzyme": "^3.11.0",
8792
"eslint": "^7.25.0",
88-
"hadron-app-registry": "^9.0.14",
8993
"mocha": "^10.2.0",
90-
"mongodb-data-service": "^22.15.0",
9194
"nyc": "^15.1.0",
9295
"prop-types": "^15.7.2",
9396
"react-dom": "^17.0.2",

packages/compass-shell/src/components/compass-shell/compass-shell.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ export class CompassShell extends Component<
304304

305305
export default connect((state: RootState) => ({
306306
emitShellPluginOpened: () => {
307-
if (state.appRegistry && state.appRegistry.globalAppRegistry) {
308-
state.appRegistry.globalAppRegistry.emit('compass:compass-shell:opened');
309-
}
307+
state.runtime.appRegistry?.emit('compass:compass-shell:opened');
310308
},
311309
runtime: state.runtime ? state.runtime.runtime : null,
312310
}))(withPreferences(CompassShell, ['enableShell'], React));

packages/compass-shell/src/index.ts

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
import type AppRegistry from 'hadron-app-registry';
1+
import type { LoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
2+
import { createLoggerAndTelemetryLocator } from '@mongodb-js/compass-logging/provider';
3+
import { ShellPlugin, onActivated } from './plugin';
4+
import { registerHadronPlugin } from 'hadron-app-registry';
5+
import { dataServiceLocator } from 'mongodb-data-service/provider';
6+
import type { DataService } from 'mongodb-data-service';
27

3-
import createPlugin from './plugin';
4-
5-
const { store, Plugin } = createPlugin();
6-
7-
/**
8-
* Activate all the components in the Compass Shell package.
9-
* @param {Object} appRegistry - The appRegistry to activate this plugin with.
10-
**/
11-
function activate(appRegistry: AppRegistry): void {
12-
// Register the shell plugin's role in Compass.
13-
appRegistry.registerComponent('Global.Shell', Plugin);
14-
appRegistry.registerStore('CompassShell.Store', store);
8+
function activate(): void {
9+
// noop
1510
}
1611

17-
/**
18-
* Deactivate all the components in the Compass Shell package.
19-
* @param {Object} appRegistry - The appRegistry to deactivate this plugin with.
20-
**/
21-
function deactivate(appRegistry: AppRegistry): void {
22-
appRegistry.registerComponent('Global.Shell', Plugin);
23-
appRegistry.deregisterStore('CompassShell.Store');
12+
function deactivate(): void {
13+
// noop
2414
}
2515

26-
export default Plugin;
16+
export const CompassShellPlugin = registerHadronPlugin<
17+
unknown,
18+
{ logger: () => LoggerAndTelemetry; dataService: () => DataService }
19+
>(
20+
{
21+
name: 'CompassShell',
22+
component: ShellPlugin,
23+
activate: onActivated,
24+
},
25+
{
26+
logger: createLoggerAndTelemetryLocator('COMPASS-SHELL'),
27+
dataService: dataServiceLocator,
28+
}
29+
);
30+
2731
export { activate, deactivate };
2832
export { default as metadata } from '../package.json';

packages/compass-shell/src/modules/index.ts

-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { combineReducers } from 'redux';
2-
import appRegistry from '@mongodb-js/mongodb-redux-common/app-registry';
3-
import type { AppRegistry } from 'hadron-app-registry';
42
import type { RuntimeAction, RuntimeState } from './runtime';
53
import runtime from './runtime';
64

75
export interface RootState {
8-
appRegistry: {
9-
globalAppRegistry: AppRegistry | null;
10-
localAppRegistry: AppRegistry | null;
11-
};
126
runtime: RuntimeState;
137
}
148

159
export type RootAction = RuntimeAction;
1610

1711
const reducer = combineReducers({
18-
appRegistry,
1912
runtime,
2013
});
2114

packages/compass-shell/src/plugin.spec.jsx renamed to packages/compass-shell/src/plugin.spec.tsx

+36-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
2+
import type { ReactWrapper } from 'enzyme';
23
import { mount } from 'enzyme';
3-
import { EventEmitter } from 'events';
44
import { expect } from 'chai';
55

66
import { CompassShell } from './components/compass-shell';
7-
import createPlugin from './plugin';
8-
import CompassShellStore from './stores';
7+
import { CompassShellPlugin } from './index';
8+
import { AppRegistryProvider, globalAppRegistry } from 'hadron-app-registry';
9+
import { DataServiceProvider } from 'mongodb-data-service/provider';
910

1011
// Wait until a component is present that is rendered in a limited number
1112
// of microtask queue iterations. In particular, this does *not* wait for the
@@ -26,35 +27,51 @@ async function waitForAsyncComponent(wrapper, Component, attempts = 10) {
2627
}
2728

2829
describe('CompassShellPlugin', function () {
29-
it('returns a renderable plugin', async function () {
30-
const { Plugin } = createPlugin();
30+
const fakeDataService = {
31+
getMongoClientConnectionOptions() {},
32+
} as any;
33+
let wrapper: ReactWrapper | null;
3134

32-
const wrapper = mount(<Plugin />);
35+
afterEach(() => {
36+
wrapper?.unmount();
37+
wrapper = null;
38+
});
39+
it('returns a renderable plugin', async function () {
40+
wrapper = mount(
41+
<AppRegistryProvider>
42+
{/* global */}
43+
<AppRegistryProvider>
44+
{/* local */}
45+
<DataServiceProvider value={fakeDataService}>
46+
<CompassShellPlugin />
47+
</DataServiceProvider>
48+
</AppRegistryProvider>
49+
</AppRegistryProvider>
50+
);
3351

3452
const component = await waitForAsyncComponent(wrapper, CompassShell);
3553

3654
expect(component.exists()).to.equal(true);
3755
});
3856

39-
it('returns a CompassShellStore store', function () {
40-
const { store } = createPlugin();
41-
const appRegistry = new EventEmitter();
42-
store.onActivated(appRegistry);
43-
expect(store).to.be.instanceOf(CompassShellStore);
44-
});
45-
4657
it('emits an event on the app registry when it is expanded', async function () {
47-
const { store, Plugin } = createPlugin();
48-
49-
const appRegistry = new EventEmitter();
5058
let eventOccured = false;
51-
appRegistry.on('compass:compass-shell:opened', () => {
59+
globalAppRegistry.on('compass:compass-shell:opened', () => {
5260
eventOccured = true;
5361
});
5462

55-
store.onActivated(appRegistry);
63+
wrapper = mount(
64+
<AppRegistryProvider>
65+
{/* global */}
66+
<AppRegistryProvider>
67+
{/* local */}
68+
<DataServiceProvider value={fakeDataService}>
69+
<CompassShellPlugin />
70+
</DataServiceProvider>
71+
</AppRegistryProvider>
72+
</AppRegistryProvider>
73+
);
5674

57-
const wrapper = mount(<Plugin />);
5875
const shellComponentWrapper = await waitForAsyncComponent(
5976
wrapper,
6077
CompassShell

0 commit comments

Comments
 (0)