Skip to content

Commit 8ab00a5

Browse files
committed
Remove duplicated code.
1 parent eacbcb2 commit 8ab00a5

File tree

4 files changed

+18
-145
lines changed

4 files changed

+18
-145
lines changed

src/editors/editorsController.ts

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ import { PLAYGROUND_RESULT_SCHEME } from './playgroundResultProvider';
3333
import { StatusView } from '../views';
3434
import type { TelemetryService } from '../telemetry';
3535
import type { QueryWithCopilotCodeLensProvider } from './queryWithCopilotCodeLensProvider';
36-
import { createWebviewPanel, getWebviewHtml } from '../utils/webviewHelpers';
37-
import {
38-
PreviewMessageType,
39-
type SortOption,
40-
} from '../views/data-browsing-app/extension-app-message-constants';
4136

4237
const log = createLogger('editors controller');
4338

@@ -109,7 +104,6 @@ export default class EditorsController {
109104
_editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
110105
_collectionDocumentsCodeLensProvider: CollectionDocumentsCodeLensProvider;
111106
_queryWithCopilotCodeLensProvider: QueryWithCopilotCodeLensProvider;
112-
_activePreviewPanels: vscode.WebviewPanel[] = [];
113107

114108
constructor({
115109
context,
@@ -310,139 +304,6 @@ export default class EditorsController {
310304
}
311305
}
312306

313-
openCollectionPreview(
314-
namespace: string,
315-
documents: Document[],
316-
fetchDocuments?: (options?: {
317-
sort?: SortOption;
318-
limit?: number;
319-
}) => Promise<Document[]>,
320-
initialTotalCount?: number,
321-
getTotalCount?: () => Promise<number>,
322-
): boolean {
323-
log.info('Open collection preview', namespace);
324-
325-
try {
326-
const extensionPath = this._context.extensionPath;
327-
328-
const panel = createWebviewPanel({
329-
viewType: 'mongodbPreview',
330-
title: `Preview: ${namespace}`,
331-
extensionPath,
332-
});
333-
334-
panel.webview.html = getWebviewHtml({
335-
extensionPath,
336-
webview: panel.webview,
337-
scriptName: 'previewApp.js',
338-
title: 'Preview',
339-
});
340-
341-
panel.onDidDispose(() => this.onPreviewPanelClosed(panel));
342-
this._activePreviewPanels.push(panel);
343-
344-
// Keep track of current documents, sort option, and total count
345-
// Fetch limit is fixed - pagination is handled client-side
346-
const FETCH_LIMIT = 100;
347-
let currentDocuments = documents;
348-
let currentSort: SortOption = 'default';
349-
let totalCount = initialTotalCount ?? documents.length;
350-
351-
// Helper to send current documents to webview
352-
const sendDocuments = (): void => {
353-
void panel.webview.postMessage({
354-
command: PreviewMessageType.loadDocuments,
355-
documents: JSON.parse(EJSON.stringify(currentDocuments)),
356-
totalCount,
357-
});
358-
};
359-
360-
// Helper to handle errors
361-
const handleError = (operation: string, error: unknown): void => {
362-
log.error(`${operation} failed:`, error);
363-
void panel.webview.postMessage({
364-
command: PreviewMessageType.refreshError,
365-
error: formatError(error).message,
366-
});
367-
};
368-
369-
// Helper to fetch and update documents
370-
const fetchAndUpdateDocuments = async (
371-
operation: string,
372-
): Promise<void> => {
373-
if (fetchDocuments) {
374-
log.info(
375-
`${operation} with sort:`,
376-
currentSort,
377-
'limit:',
378-
FETCH_LIMIT,
379-
);
380-
currentDocuments = await fetchDocuments({
381-
sort: currentSort,
382-
limit: FETCH_LIMIT,
383-
});
384-
log.info(`${operation} complete, count:`, currentDocuments.length);
385-
}
386-
};
387-
388-
// Send documents to webview
389-
panel.webview.onDidReceiveMessage(
390-
async (message: { command: string; sort?: SortOption }) => {
391-
log.info('Preview received message:', message.command);
392-
393-
switch (message.command) {
394-
case PreviewMessageType.getDocuments:
395-
sendDocuments();
396-
break;
397-
398-
case PreviewMessageType.refreshDocuments:
399-
try {
400-
await fetchAndUpdateDocuments('Refreshing documents');
401-
if (getTotalCount) {
402-
totalCount = await getTotalCount();
403-
}
404-
sendDocuments();
405-
} catch (error) {
406-
handleError('Refresh documents', error);
407-
}
408-
break;
409-
410-
case PreviewMessageType.sortDocuments:
411-
try {
412-
if (message.sort) {
413-
currentSort = message.sort;
414-
await fetchAndUpdateDocuments('Sorting documents');
415-
}
416-
sendDocuments();
417-
} catch (error) {
418-
handleError('Sort documents', error);
419-
}
420-
break;
421-
422-
default:
423-
log.info('Unknown command:', message.command);
424-
break;
425-
}
426-
},
427-
);
428-
429-
return true;
430-
} catch (error) {
431-
void vscode.window.showErrorMessage(
432-
`Unable to open preview: ${formatError(error).message}`,
433-
);
434-
435-
return false;
436-
}
437-
}
438-
439-
private onPreviewPanelClosed(panel: vscode.WebviewPanel): void {
440-
const panelIndex = this._activePreviewPanels.indexOf(panel);
441-
if (panelIndex !== -1) {
442-
this._activePreviewPanels.splice(panelIndex, 1);
443-
}
444-
}
445-
446307
onViewMoreCollectionDocuments(
447308
operationId: string,
448309
connectionId: string,

src/explorer/documentListUtils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export const formatDocCount = (count: number): string => {
1818
return `${numeral(count).format('0a') as string}`.toUpperCase();
1919
};
2020

21-
export function getDocumentsIconPath(): { light: vscode.Uri; dark: vscode.Uri } {
21+
export function getDocumentsIconPath(): {
22+
light: vscode.Uri;
23+
dark: vscode.Uri;
24+
} {
2225
const LIGHT = path.join(getImagesPath(), 'light');
2326
const DARK = path.join(getImagesPath(), 'dark');
2427

@@ -38,4 +41,3 @@ export function getDocumentsTooltip(
3841
}
3942
return `${typeString} Documents`;
4043
}
41-

src/featureFlags.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const FEATURE_FLAGS = {
22
useOldConnectionForm: process.env.MDB_USE_OLD_CONNECTION_FORM === 'true',
3-
useEnhancedDataBrowsingExperience: process.env.MDB_USE_ENHANCED_DATA_BROWSING_EXPERIENCE === 'true',
3+
useEnhancedDataBrowsingExperience:
4+
process.env.MDB_USE_ENHANCED_DATA_BROWSING_EXPERIENCE === 'true',
45
};
56

67
export type FeatureFlag = keyof typeof FEATURE_FLAGS;

src/mdbExtensionController.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
import * as queryString from 'query-string';
6060
import { MCPController } from './mcp/mcpController';
6161
import formatError from './utils/formatError';
62+
import DataBrowsingController from './views/dataBrowsingController';
6263

6364
// Deep link command filtering: Commands are explicitly categorized as allowed or disallowed.
6465
// We use tests in mdbExtensionController.test.ts to enforce these lists being disjoint and complete.
@@ -177,6 +178,7 @@ export default class MDBExtensionController implements vscode.Disposable {
177178
_exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
178179
_participantController: ParticipantController;
179180
_mcpController: MCPController;
181+
_dataBrowsingController: DataBrowsingController;
180182

181183
constructor(
182184
context: vscode.ExtensionContext,
@@ -259,6 +261,10 @@ export default class MDBExtensionController implements vscode.Disposable {
259261
storageController: this._storageController,
260262
telemetryService: this._telemetryService,
261263
});
264+
this._dataBrowsingController = new DataBrowsingController({
265+
connectionController: this._connectionController,
266+
telemetryService: this._telemetryService,
267+
});
262268
this._editorsController.registerProviders();
263269
this._mcpController = new MCPController({
264270
context,
@@ -867,13 +873,15 @@ export default class MDBExtensionController implements vscode.Disposable {
867873
// Pass a function to get the total count
868874
const getTotalCount = (): Promise<number> => element.getTotalCount();
869875

870-
return this._editorsController.openCollectionPreview(
876+
this._dataBrowsingController.openDataBrowser(this._context, {
871877
namespace,
872878
documents,
873879
fetchDocuments,
874-
totalCount,
880+
initialTotalCount: totalCount,
875881
getTotalCount,
876-
);
882+
});
883+
884+
return true;
877885
},
878886
);
879887
this.registerCommand(
@@ -1219,6 +1227,7 @@ export default class MDBExtensionController implements vscode.Disposable {
12191227
this._telemetryService.deactivate();
12201228
this._editorsController.deactivate();
12211229
this._webviewController.deactivate();
1230+
this._dataBrowsingController.deactivate();
12221231
this._activeConnectionCodeLensProvider.deactivate();
12231232
this._connectionController.deactivate();
12241233
}

0 commit comments

Comments
 (0)