+ {connectionInProgress && (
+
+
+
+ )}
{showResourcesPanel && (
)}
- {showConnectionForm && (
+ {connectionFormOpened && (
{
- // TODO(VSCODE-489): Type connection form and post message to the webview controller.
- // Maintain connecting status.
- console.log('connect', connectionInfo);
- }}
- onClose={() => setShowConnectionForm(false)}
- open={showConnectionForm}
+ onConnectClicked={handleConnectClicked}
+ onClose={closeConnectionForm}
+ open={connectionFormOpened}
+ connectionErrorMessage={connectionErrorMessage}
/>
)}
- setShowConnectionForm(true)}
- />
+
);
diff --git a/src/views/webview-app/use-connection-form.ts b/src/views/webview-app/use-connection-form.ts
new file mode 100644
index 000000000..69827f124
--- /dev/null
+++ b/src/views/webview-app/use-connection-form.ts
@@ -0,0 +1,52 @@
+import { useEffect, useState } from 'react';
+import { v4 as uuidv4 } from 'uuid';
+import type { ConnectionInfo } from 'mongodb-data-service-legacy';
+import { sendConnectToExtension } from './vscode-api';
+import { MESSAGE_TYPES } from './extension-app-message-constants';
+import type { MESSAGE_FROM_EXTENSION_TO_WEBVIEW } from './extension-app-message-constants';
+
+export default function useConnectionForm() {
+ const [connectionInProgress, setConnectionInProgress] = useState(false);
+ const [connectionFormOpened, setConnectionFormOpened] = useState(false);
+ const [connectionAttemptId, setConnectionAttemptId] = useState('');
+ const [connectionErrorMessage, setConnectionErrorMessage] = useState('');
+
+ useEffect(() => {
+ const handleConnectResultResponse = (event) => {
+ const message: MESSAGE_FROM_EXTENSION_TO_WEBVIEW = event.data;
+ if (
+ message.command === MESSAGE_TYPES.CONNECT_RESULT &&
+ message.connectionAttemptId === connectionAttemptId
+ ) {
+ setConnectionInProgress(false);
+ if (message.connectionSuccess) {
+ setConnectionFormOpened(false);
+ } else {
+ setConnectionErrorMessage(message.connectionMessage);
+ }
+ }
+ };
+ window.addEventListener('message', handleConnectResultResponse);
+ () => window.removeEventListener('message', handleConnectResultResponse);
+ }, [connectionAttemptId]);
+
+ return {
+ connectionFormOpened,
+ connectionInProgress,
+ connectionErrorMessage,
+ openConnectionForm: () => setConnectionFormOpened(true),
+ closeConnectionForm: () => {
+ setConnectionFormOpened(false);
+ setConnectionErrorMessage('');
+ },
+ handleConnectClicked: (connectionInfo: ConnectionInfo) => {
+ // Clears the error message from previous connect attempt
+ setConnectionErrorMessage('');
+
+ const nextAttemptId = uuidv4();
+ setConnectionAttemptId(nextAttemptId);
+ setConnectionInProgress(true);
+ sendConnectToExtension(connectionInfo, nextAttemptId);
+ },
+ };
+}
diff --git a/src/views/webview-app/use-connection-status.ts b/src/views/webview-app/use-connection-status.ts
index 920a20a67..c570661bc 100644
--- a/src/views/webview-app/use-connection-status.ts
+++ b/src/views/webview-app/use-connection-status.ts
@@ -20,8 +20,6 @@ const useConnectionStatus = () => {
setConnectionStatus(message.connectionStatus);
setConnectionName(message.activeConnectionName);
}
- // TODO(VSCODE-489): Also listen on the new connected event whenever that is
- // implemented
};
window.addEventListener('message', handleConnectionStatusResponse);
diff --git a/src/views/webview-app/vscode-api.ts b/src/views/webview-app/vscode-api.ts
index cf664f4aa..c9f1465cf 100644
--- a/src/views/webview-app/vscode-api.ts
+++ b/src/views/webview-app/vscode-api.ts
@@ -1,3 +1,4 @@
+import type { ConnectionInfo } from 'mongodb-data-service-legacy';
import {
MESSAGE_TYPES,
type MESSAGE_FROM_WEBVIEW_TO_EXTENSION,
@@ -10,6 +11,17 @@ interface VSCodeApi {
declare const acquireVsCodeApi: () => VSCodeApi;
const vscode = acquireVsCodeApi();
+export const sendConnectToExtension = (
+ connectionInfo: ConnectionInfo,
+ connectionAttemptId: string
+) => {
+ vscode.postMessage({
+ command: MESSAGE_TYPES.CONNECT,
+ connectionInfo,
+ connectionAttemptId,
+ });
+};
+
export const renameActiveConnection = () => {
vscode.postMessage({
command: MESSAGE_TYPES.RENAME_ACTIVE_CONNECTION,
diff --git a/src/views/webviewController.ts b/src/views/webviewController.ts
index 7e89d12a1..ede66d1d2 100644
--- a/src/views/webviewController.ts
+++ b/src/views/webviewController.ts
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import path from 'path';
import crypto from 'crypto';
+import type { ConnectionInfo } from 'mongodb-data-service-legacy';
import type ConnectionController from '../connectionController';
import { ConnectionTypes } from '../connectionController';
@@ -109,7 +110,8 @@ export default class WebviewController {
this._themeChangedSubscription?.dispose();
}
- handleWebviewConnectAttempt = async (
+ // TODO: VSCODE-491 - Remove this entirely when getting rid of legacy
+ handleWebviewLegacyConnectAttempt = async (
panel: vscode.WebviewPanel,
rawConnectionModel: LegacyConnectionModel,
connectionAttemptId: string
@@ -150,6 +152,45 @@ export default class WebviewController {
}
};
+ handleWebviewConnectAttempt = async (
+ panel: vscode.WebviewPanel,
+ connectionInfo: ConnectionInfo,
+ connectionAttemptId: string
+ ) => {
+ try {
+ const { successfullyConnected, connectionErrorMessage } =
+ await this._connectionController.saveNewConnectionFromFormAndConnect(
+ connectionInfo,
+ ConnectionTypes.CONNECTION_FORM
+ );
+
+ try {
+ // The webview may have been closed in which case this will throw.
+ void panel.webview.postMessage({
+ command: MESSAGE_TYPES.CONNECT_RESULT,
+ connectionAttemptId,
+ connectionSuccess: successfullyConnected,
+ connectionMessage: successfullyConnected
+ ? `Successfully connected to ${this._connectionController.getActiveConnectionName()}.`
+ : connectionErrorMessage,
+ });
+ } catch (err) {
+ log.error('Unable to send connection result to webview', err);
+ }
+ } catch (error) {
+ void vscode.window.showErrorMessage(
+ `Unable to load connection: ${error}`
+ );
+
+ void panel.webview.postMessage({
+ command: MESSAGE_TYPES.CONNECT_RESULT,
+ connectionAttemptId,
+ connectionSuccess: false,
+ connectionMessage: `Unable to load connection: ${error}`,
+ });
+ }
+ };
+
handleWebviewOpenFilePickerRequest = async (
message: OpenFilePickerMessage,
panel: vscode.WebviewPanel
@@ -168,15 +209,24 @@ export default class WebviewController {
});
};
+ // eslint-disable-next-line complexity
handleWebviewMessage = async (
message: MESSAGE_FROM_WEBVIEW_TO_EXTENSION,
panel: vscode.WebviewPanel
): Promise