Skip to content

Commit 8a5885c

Browse files
authored
chore(connection-form): split connection form from compass preferences (#5162)
1 parent 5c2b6af commit 8a5885c

18 files changed

+246
-190
lines changed

package-lock.json

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

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useState } from 'react';
1+
import React, { useCallback, useMemo, useState } from 'react';
22
import {
33
ImportConnectionsModal,
44
ExportConnectionsModal,
@@ -23,6 +23,7 @@ import { useConnections } from '../stores/connections-store';
2323
import { cloneDeep } from 'lodash';
2424
import ConnectionList from './connection-list/connection-list';
2525
import { LegacyConnectionsModal } from './legacy-connections-modal';
26+
import { usePreference } from 'compass-preferences-model';
2627

2728
const { log, mongoLogId } = createLoggerAndTelemetry(
2829
'mongodb-compass:connections:connections'
@@ -123,6 +124,47 @@ function Connections({
123124
[]
124125
);
125126

127+
const protectConnectionStrings = usePreference(
128+
'protectConnectionStrings',
129+
React
130+
);
131+
const forceConnectionOptions = usePreference('forceConnectionOptions', React);
132+
const showKerberosPasswordField = usePreference(
133+
'showKerberosPasswordField',
134+
React
135+
);
136+
const showOIDCDeviceAuthFlow = usePreference('showOIDCDeviceAuthFlow', React);
137+
const enableOidc = usePreference('enableOidc', React);
138+
const enableDebugUseCsfleSchemaMap = usePreference(
139+
'enableDebugUseCsfleSchemaMap',
140+
React
141+
);
142+
const protectConnectionStringsForNewConnections = usePreference(
143+
'protectConnectionStringsForNewConnections',
144+
React
145+
);
146+
147+
const preferences = useMemo(
148+
() => ({
149+
protectConnectionStrings,
150+
forceConnectionOptions,
151+
showKerberosPasswordField,
152+
showOIDCDeviceAuthFlow,
153+
enableOidc,
154+
enableDebugUseCsfleSchemaMap,
155+
protectConnectionStringsForNewConnections,
156+
}),
157+
[
158+
protectConnectionStrings,
159+
forceConnectionOptions,
160+
showKerberosPasswordField,
161+
showOIDCDeviceAuthFlow,
162+
enableOidc,
163+
enableDebugUseCsfleSchemaMap,
164+
protectConnectionStringsForNewConnections,
165+
]
166+
);
167+
126168
return (
127169
<div data-testid="connections-wrapper" className={connectStyles}>
128170
<ResizableSidebar>
@@ -166,6 +208,7 @@ function Connections({
166208
onSaveConnectionClicked={saveConnection}
167209
initialConnectionInfo={activeConnectionInfo}
168210
connectionErrorMessage={connectionErrorMessage}
211+
preferences={preferences}
169212
/>
170213
</ErrorBoundary>
171214
<FormHelp />

packages/compass-connections/src/stores/connections-store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,15 @@ export function useConnections({
541541
};
542542
}
543543

544+
const { forceConnectionOptions = [], browserCommandForOIDCAuth } =
545+
preferences.getPreferences();
546+
544547
const newConnectionDataService = await newConnectionAttempt.connect(
545548
adjustConnectionOptionsBeforeConnect({
546549
connectionOptions: connectionInfo.connectionOptions,
547550
defaultAppName: appName,
548551
notifyDeviceFlow,
552+
preferences: { forceConnectionOptions, browserCommandForOIDCAuth },
549553
})
550554
);
551555
connectingConnectionAttempt.current = undefined;

packages/connection-form/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"@mongodb-js/compass-editor": "^0.18.0",
5757
"@mongodb-js/connection-storage": "^0.6.6",
5858
"@testing-library/react-hooks": "^7.0.2",
59-
"compass-preferences-model": "^2.15.6",
6059
"lodash": "^4.17.21",
6160
"mongodb-build-info": "^1.7.0",
6261
"mongodb-connection-string-url": "^2.6.0",

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.spec.tsx

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { render, screen, fireEvent, cleanup } from '@testing-library/react';
33
import { expect } from 'chai';
44
import sinon from 'sinon';
55
import ConnectionStringUrl from 'mongodb-connection-string-url';
6-
import preferences from 'compass-preferences-model';
76

87
import AuthenticationGssapi from './authentication-gssapi';
98
import type { ConnectionFormError } from '../../../utils/validation';
109
import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form';
10+
import { ConnectionFormPreferencesContext } from '../../../hooks/use-connect-form-preferences';
1111

1212
function renderComponent({
1313
errors = [],
@@ -19,11 +19,15 @@ function renderComponent({
1919
updateConnectionFormField: UpdateConnectionFormField;
2020
}) {
2121
render(
22-
<AuthenticationGssapi
23-
errors={errors}
24-
connectionStringUrl={connectionStringUrl}
25-
updateConnectionFormField={updateConnectionFormField}
26-
/>
22+
<ConnectionFormPreferencesContext.Provider
23+
value={{ showKerberosPasswordField: true }}
24+
>
25+
<AuthenticationGssapi
26+
errors={errors}
27+
connectionStringUrl={connectionStringUrl}
28+
updateConnectionFormField={updateConnectionFormField}
29+
/>
30+
</ConnectionFormPreferencesContext.Provider>
2731
);
2832
}
2933

@@ -172,19 +176,6 @@ describe('AuthenticationGssapi Component', function () {
172176
});
173177

174178
describe('Kerberos password support', function () {
175-
let sandbox: sinon.SinonSandbox;
176-
177-
before(function () {
178-
sandbox = sinon.createSandbox();
179-
sandbox
180-
.stub(preferences, 'getPreferences')
181-
.returns({ showKerberosPasswordField: true } as any);
182-
});
183-
184-
after(function () {
185-
return sandbox.restore();
186-
});
187-
188179
describe('when password is not in the connection string', function () {
189180
beforeEach(function () {
190181
renderComponent({

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
RadioBox,
88
Checkbox,
99
} from '@mongodb-js/compass-components';
10-
import { usePreference } from 'compass-preferences-model';
1110

1211
import type ConnectionStringUrl from 'mongodb-connection-string-url';
1312
import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form';
@@ -18,6 +17,7 @@ import {
1817
getConnectionStringUsername,
1918
parseAuthMechanismProperties,
2019
} from '../../../utils/connection-string-helpers';
20+
import { useConnectionFormPreference } from '../../../hooks/use-connect-form-preferences';
2121

2222
const GSSAPI_CANONICALIZE_HOST_NAME_OPTIONS: Record<
2323
string,
@@ -56,9 +56,8 @@ function AuthenticationGSSAPI({
5656

5757
const [showPassword, setShowPassword] = useState<boolean>(false);
5858

59-
const showKerberosPasswordField = !!usePreference(
60-
'showKerberosPasswordField',
61-
React
59+
const showKerberosPasswordField = !!useConnectionFormPreference(
60+
'showKerberosPasswordField'
6261
);
6362

6463
useEffect(() => {

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.spec.tsx

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
import { expect } from 'chai';
1010
import sinon from 'sinon';
1111
import type { ConnectionOptions } from 'mongodb-data-service';
12-
import preferences from 'compass-preferences-model';
1312

1413
import ConnectionForm from '../../../';
1514

1615
const deviceAuthFlowText = 'Enable Device Authentication Flow';
1716

18-
async function renderConnectionForm(connectSpy) {
17+
async function renderConnectionForm(
18+
connectSpy,
19+
{ showOIDCDeviceAuthFlow }: { showOIDCDeviceAuthFlow: boolean }
20+
) {
1921
render(
2022
<ConnectionForm
2123
initialConnectionInfo={{
@@ -27,6 +29,7 @@ async function renderConnectionForm(connectSpy) {
2729
onConnectClicked={(connectionInfo) => {
2830
connectSpy(connectionInfo.connectionOptions);
2931
}}
32+
preferences={{ enableOidc: true, showOIDCDeviceAuthFlow }}
3033
/>
3134
);
3235

@@ -55,23 +58,10 @@ const openOptionsAccordion = () =>
5558

5659
describe('AuthenticationOIDC Connection Form', function () {
5760
let expectToConnectWith;
58-
let sandbox: sinon.SinonSandbox;
59-
let getPreferencesStub: sinon.SinonStub;
6061
let connectSpy: sinon.SinonSpy;
6162

6263
beforeEach(function () {
6364
connectSpy = sinon.spy();
64-
65-
sandbox = sinon.createSandbox();
66-
getPreferencesStub = sinon.stub();
67-
getPreferencesStub.returns({
68-
enableOidc: true,
69-
});
70-
// TODO(COMPASS-6803): Remove feature flag, remove this sandbox.
71-
sandbox
72-
.stub(preferences, 'getPreferences')
73-
.callsFake(() => getPreferencesStub());
74-
7565
expectToConnectWith = async (
7666
expected: ConnectionOptions | ((ConnectionOptions) => void)
7767
): Promise<void> => {
@@ -93,14 +83,14 @@ describe('AuthenticationOIDC Connection Form', function () {
9383
}
9484
};
9585
});
86+
9687
afterEach(function () {
97-
sandbox.restore();
9888
cleanup();
9989
});
10090

10191
describe('when rendered', function () {
10292
beforeEach(async function () {
103-
await renderConnectionForm(connectSpy);
93+
await renderConnectionForm(connectSpy, { showOIDCDeviceAuthFlow: false });
10494
});
10595

10696
it('handles principal (username) changes', async function () {
@@ -156,12 +146,7 @@ describe('AuthenticationOIDC Connection Form', function () {
156146

157147
describe('when rendered and the showOIDCDeviceAuthFlow setting is enabled', function () {
158148
beforeEach(async function () {
159-
getPreferencesStub.returns({
160-
enableOidc: true,
161-
showOIDCDeviceAuthFlow: true,
162-
});
163-
164-
await renderConnectionForm(connectSpy);
149+
await renderConnectionForm(connectSpy, { showOIDCDeviceAuthFlow: true });
165150
});
166151

167152
it('handles the enable device authentication flow checkbox', async function () {

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
} from '@mongodb-js/compass-components';
1010
import type ConnectionStringUrl from 'mongodb-connection-string-url';
1111
import type { ConnectionOptions } from 'mongodb-data-service';
12-
import { usePreference } from 'compass-preferences-model';
1312

1413
import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form';
1514
import type { ConnectionFormError } from '../../../utils/validation';
1615
import { errorMessageByFieldName } from '../../../utils/validation';
1716
import { getConnectionStringUsername } from '../../../utils/connection-string-helpers';
1817
import type { OIDCOptions } from '../../../utils/oidc-handler';
18+
import { useConnectionFormPreference } from '../../../hooks/use-connect-form-preferences';
1919

2020
type AuthFlowType = NonNullable<OIDCOptions['allowedFlows']>[number];
2121

@@ -47,9 +47,8 @@ function AuthenticationOIDC({
4747
const hasEnabledDeviceAuthFlow =
4848
!!connectionOptions.oidc?.allowedFlows?.includes?.('device-auth');
4949

50-
const showOIDCDeviceAuthFlow = !!usePreference(
51-
'showOIDCDeviceAuthFlow',
52-
React
50+
const showOIDCDeviceAuthFlow = !!useConnectionFormPreference(
51+
'showOIDCDeviceAuthFlow'
5352
);
5453

5554
return (

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-tab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from '@mongodb-js/compass-components';
1010
import type ConnectionStringUrl from 'mongodb-connection-string-url';
1111
import { AuthMechanism } from 'mongodb';
12-
import { usePreference } from 'compass-preferences-model';
1312
import type { ConnectionOptions } from 'mongodb-data-service';
1413

1514
import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form';
@@ -21,6 +20,7 @@ import AuthenticationGSSAPI from './authentication-gssapi';
2120
import AuthenticationPlain from './authentication-plain';
2221
import AuthenticationAWS from './authentication-aws';
2322
import AuthenticationOidc from './authentication-oidc';
23+
import { useConnectionFormPreference } from '../../../hooks/use-connect-form-preferences';
2424

2525
type AUTH_TABS =
2626
| 'DEFAULT' // Username/Password (SCRAM-SHA-1 + SCRAM-SHA-256 + DEFAULT)
@@ -108,7 +108,7 @@ function AuthenticationTab({
108108
updateConnectionFormField: UpdateConnectionFormField;
109109
connectionOptions: ConnectionOptions;
110110
}): React.ReactElement {
111-
const enableOIDC = !!usePreference('enableOidc', React);
111+
const enableOIDC = !!useConnectionFormPreference('enableOidc');
112112
const enabledAuthOptions = useMemo(() => {
113113
if (enableOIDC) {
114114
return options;

packages/connection-form/src/components/advanced-options-tabs/csfle-tab/csfle-tab.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import type {
3232
KMSField,
3333
} from '../../../utils/csfle-kms-fields';
3434
import { KMSProviderFields } from '../../../utils/csfle-kms-fields';
35-
import { usePreference } from 'compass-preferences-model';
35+
import { useConnectionFormPreference } from '../../../hooks/use-connect-form-preferences';
3636

3737
const kmsProviderComponentWrapperStyles = css({
3838
paddingLeft: spacing[3],
@@ -96,9 +96,8 @@ function CSFLETab({
9696
const autoEncryptionOptions =
9797
connectionOptions.fleOptions?.autoEncryption ?? {};
9898

99-
const enableSchemaMapDebugFlag = usePreference(
100-
'enableDebugUseCsfleSchemaMap',
101-
React
99+
const enableSchemaMapDebugFlag = useConnectionFormPreference(
100+
'enableDebugUseCsfleSchemaMap'
102101
);
103102

104103
const errors = errorsByFieldTab(errors_, 'csfle');

0 commit comments

Comments
 (0)