Skip to content

Commit 0c43c17

Browse files
authored
feat(webview): use Compass' connection form in the new overview page VSCODE-488 (#622)
1 parent f93f111 commit 0c43c17

File tree

11 files changed

+777
-84
lines changed

11 files changed

+777
-84
lines changed

package-lock.json

Lines changed: 641 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,8 @@
978978
"@fortawesome/react-fontawesome": "^0.2.0",
979979
"@iconify-icons/codicon": "^1.2.25",
980980
"@iconify/react": "^1.1.4",
981-
"@leafygreen-ui/logo": "^8.0.4",
982-
"@mongodb-js/compass-components": "^1.19.0",
981+
"@mongodb-js/compass-components": "^1.20.0",
982+
"@mongodb-js/connection-form": "^1.20.4",
983983
"@mongodb-js/mongodb-constants": "^0.7.1",
984984
"@mongosh/browser-runtime-electron": "^2.0.2",
985985
"@mongosh/i18n": "^2.0.2",

scripts/check-vsix-size.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const vsixFileName = path.resolve(
1212
);
1313
const size = fs.statSync(vsixFileName).size;
1414

15-
const maxSize = 7 * 1000000; // 7 MB
15+
const maxSize = 8 * 1000000; // 8 MB
1616

1717
if (size >= maxSize) {
1818
throw new Error(

src/test/suite/views/webview-app/connect-helper.test.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@ import { MESSAGE_TYPES } from '../../../../views/webview-app/extension-app-messa
88

99
describe('ConnectHelper test suite', function () {
1010
test('when rendered it should show both connection options', function () {
11-
render(<ConnectHelper />);
11+
render(
12+
<ConnectHelper
13+
onClickOpenConnectionForm={() => {
14+
/* noop */
15+
}}
16+
/>
17+
);
1218
expect(screen.getByLabelText('Connect with connection string')).to.not.be
1319
.null;
1420
expect(screen.getByLabelText('Open connection form')).to.not.be.null;
1521
});
1622

1723
test('when connecting with string, it should call vscode to open connection string input', function () {
1824
const postMessageStub = Sinon.stub(vscode, 'postMessage');
19-
render(<ConnectHelper />);
25+
render(
26+
<ConnectHelper
27+
onClickOpenConnectionForm={() => {
28+
/* noop */
29+
}}
30+
/>
31+
);
2032
screen.getByLabelText('Connect with connection string').click();
2133
expect(postMessageStub).to.have.been.calledWithExactly({
2234
command: MESSAGE_TYPES.OPEN_CONNECTION_STRING_INPUT,

src/test/suite/views/webview-app/jest-setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
const Enzyme = require('enzyme');
55
const Adapter = require('@wojtekmaj/enzyme-adapter-react-17');
66
const chai = require('chai');
7+
const { TextEncoder, TextDecoder } = require('util');
8+
79
chai.use(require('sinon-chai'));
810
Enzyme.configure({ adapter: new Adapter() });
911

1012
// eslint-disable-next-line no-undef
1113
jest.mock('@iconify-icons/codicon/book', () => {});
1214

15+
// Note applied with js dom so we do manually. (Required by node_modules/mongodb-connection-string-url/node_modules/whatwg-url/lib/encoding.js)
16+
Object.assign(global, { TextDecoder, TextEncoder });
17+
1318
global.vscodeFake = {
1419
postMessage: (message) => {},
1520
};

src/test/suite/views/webview-app/overview-page.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { expect } from 'chai';
33
import { cleanup, render, screen } from '@testing-library/react';
4+
45
import OverviewPage from '../../../../views/webview-app/overview-page';
56

67
describe('OverviewPage test suite', function () {
@@ -26,4 +27,14 @@ describe('OverviewPage test suite', function () {
2627
screen.getByLabelText('Close').click();
2728
expect(screen.queryByText('Product overview')).to.be.null;
2829
});
30+
31+
test('it renders the new connection form when opened', function () {
32+
render(<OverviewPage />);
33+
34+
const connectionFormTestId = 'connection-form-modal';
35+
expect(screen.queryByTestId(connectionFormTestId)).to.not.exist;
36+
37+
screen.getByText('Open form').click();
38+
expect(screen.getByTestId(connectionFormTestId)).to.exist;
39+
});
2940
});

src/views/webview-app/connect-helper.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ const getOSCommandShortcutName = (): string => {
5656
return 'Cmd';
5757
};
5858

59-
const ConnectHelper: React.FC = () => {
59+
const ConnectHelper: React.FC<{
60+
onClickOpenConnectionForm: () => void;
61+
}> = ({ onClickOpenConnectionForm }) => {
6062
return (
6163
<div className={containerStyles}>
6264
<div className={cardContainerStyles}>
@@ -89,9 +91,7 @@ const ConnectHelper: React.FC = () => {
8991
<Button
9092
aria-label="Open connection form"
9193
darkMode
92-
onClick={() => {
93-
// TODO
94-
}}
94+
onClick={onClickOpenConnectionForm}
9595
>
9696
Open form
9797
</Button>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import CompassConnectionForm from '@mongodb-js/connection-form';
3+
import { Modal, css, spacing } from '@mongodb-js/compass-components';
4+
import { v4 as uuidv4 } from 'uuid';
5+
6+
const modalContentStyles = css({
7+
// Override LeafyGreen width to accommodate the strict connection-form size.
8+
width: `${spacing[6] * 12}px !important`,
9+
});
10+
11+
const formContainerStyles = css({
12+
padding: spacing[3],
13+
position: 'relative',
14+
display: 'flex',
15+
flexDirection: 'column',
16+
});
17+
18+
function createNewConnectionInfo() {
19+
return {
20+
id: uuidv4(),
21+
connectionOptions: {
22+
connectionString: 'mongodb://localhost:27017',
23+
},
24+
};
25+
}
26+
27+
const initialConnectionInfo = createNewConnectionInfo();
28+
29+
const ConnectionForm: React.FunctionComponent<{
30+
onConnectClicked: (onConnectClicked: unknown) => void;
31+
onClose: () => void;
32+
open: boolean;
33+
}> = ({ onConnectClicked, onClose, open }) => {
34+
return (
35+
<Modal
36+
// Warning: This property may be removed in future
37+
// modal releases.
38+
contentClassName={modalContentStyles}
39+
setOpen={() => onClose()}
40+
open={open}
41+
data-testid="connection-form-modal"
42+
size="large"
43+
>
44+
<div className={formContainerStyles}>
45+
<CompassConnectionForm
46+
onConnectClicked={onConnectClicked}
47+
initialConnectionInfo={initialConnectionInfo}
48+
preferences={{
49+
protectConnectionStrings: false,
50+
forceConnectionOptions: [],
51+
showKerberosPasswordField: false,
52+
showOIDCDeviceAuthFlow: false,
53+
enableOidc: false,
54+
enableDebugUseCsfleSchemaMap: false,
55+
protectConnectionStringsForNewConnections: false,
56+
showOIDCAuth: false,
57+
showKerberosAuth: false,
58+
showCSFLE: false,
59+
}}
60+
/>
61+
</div>
62+
</Modal>
63+
);
64+
};
65+
66+
export { ConnectionForm };

src/views/webview-app/legacy/components/mongodb-logo/mongodb-logo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { MongoDBLogo as LeafyGreenMongoDBLogo } from '@leafygreen-ui/logo';
3+
import { MongoDBLogo as LeafyGreenMongoDBLogo } from '@mongodb-js/compass-components';
44

55
import styles from './mongodb-logo.less';
66

src/views/webview-app/overview-page.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import React, { useCallback, useState } from 'react';
2-
import { HorizontalRule, css, spacing } from '@mongodb-js/compass-components';
1+
import React, { useCallback, useLayoutEffect, useState } from 'react';
2+
import {
3+
HorizontalRule,
4+
css,
5+
resetGlobalCSS,
6+
spacing,
7+
} from '@mongodb-js/compass-components';
8+
39
import OverviewHeader from './overview-header';
410
import ConnectionStatus from './connection-status';
511
import ConnectHelper from './connect-helper';
612
import AtlasCta from './atlas-cta';
713
import ResourcesPanel from './resources-panel/panel';
14+
import { ConnectionForm } from './connection-form';
815

916
const pageStyles = css({
1017
width: '90%',
@@ -21,6 +28,7 @@ const pageStyles = css({
2128

2229
const OverviewPage: React.FC = () => {
2330
const [showResourcesPanel, setShowResourcesPanel] = useState(false);
31+
const [showConnectionForm, setShowConnectionForm] = useState(false);
2432
const handleResourcesPanelClose = useCallback(
2533
() => setShowResourcesPanel(false),
2634
[]
@@ -29,15 +37,35 @@ const OverviewPage: React.FC = () => {
2937
() => setShowResourcesPanel(true),
3038
[]
3139
);
40+
41+
useLayoutEffect(() => {
42+
// TODO(VSCODE-490): Move this reset css call to the top level entry point
43+
// of the app and out of the react lifecycle.
44+
resetGlobalCSS();
45+
}, []);
46+
3247
return (
3348
<div className={pageStyles}>
3449
{showResourcesPanel && (
3550
<ResourcesPanel onClose={handleResourcesPanelClose} />
3651
)}
52+
{showConnectionForm && (
53+
<ConnectionForm
54+
onConnectClicked={(connectionInfo) => {
55+
// TODO(VSCODE-489): Type connection form and post message to the webview controller.
56+
// Maintain connecting status.
57+
console.log('connect', connectionInfo);
58+
}}
59+
onClose={() => setShowConnectionForm(false)}
60+
open={showConnectionForm}
61+
/>
62+
)}
3763
<OverviewHeader onResourcesClick={handleResourcesClick} />
3864
<HorizontalRule />
3965
<ConnectionStatus />
40-
<ConnectHelper />
66+
<ConnectHelper
67+
onClickOpenConnectionForm={() => setShowConnectionForm(true)}
68+
/>
4169
<AtlasCta />
4270
</div>
4371
);

0 commit comments

Comments
 (0)