-
Notifications
You must be signed in to change notification settings - Fork 90
feat(tree-explorer): add ability to set preset connections in settings.json VSCODE-665 #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
db87e39
06b1e90
d2d58e6
bf9cd64
0128835
0850375
bf39230
a72bad8
6cf0f1c
63c719d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,15 @@ import type { StorageController } from './storage'; | |
| import type { StatusView } from './views'; | ||
| import type TelemetryService from './telemetry/telemetryService'; | ||
| import { openLink } from './utils/linkHelper'; | ||
| import type { LoadedConnection } from './storage/connectionStorage'; | ||
| import type { | ||
| ConnectionSource, | ||
| LoadedConnection, | ||
| } from './storage/connectionStorage'; | ||
| import { ConnectionStorage } from './storage/connectionStorage'; | ||
| import LINKS from './utils/links'; | ||
| import { isAtlasStream } from 'mongodb-build-info'; | ||
| import { DocumentSource } from './documentSource'; | ||
| import type { ConnectionTreeItem } from './explorer'; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const packageJSON = require('../package.json'); | ||
|
|
@@ -161,7 +166,55 @@ export default class ConnectionController { | |
| }); | ||
| } | ||
|
|
||
| async openPresetConnectionsSettings( | ||
| originTreeItem: ConnectionTreeItem | undefined | ||
| ): Promise<void> { | ||
| this._telemetryService.trackPresetConnectionEdited({ | ||
| source: DocumentSource.DOCUMENT_SOURCE_TREEVIEW, | ||
| source_details: originTreeItem ? 'tree_item' : 'header', | ||
| }); | ||
| let source: ConnectionSource | undefined = originTreeItem?.source; | ||
| if (!source) { | ||
| const mdbConfiguration = vscode.workspace.getConfiguration('mdb'); | ||
|
|
||
| const presetConnections = mdbConfiguration?.inspect('presetConnections'); | ||
|
|
||
| if (presetConnections?.workspaceValue) { | ||
| source = 'workspaceSettings'; | ||
| } else if (presetConnections?.globalValue) { | ||
| source = 'globalSettings'; | ||
| } else { | ||
| // If no preset settings exist in workspace and global scope, | ||
| // set a default one inside the workspace and open it. | ||
| source = 'workspaceSettings'; | ||
| await mdbConfiguration.update('presetConnections', [ | ||
| { | ||
| name: 'Preset Database', | ||
| connectionString: 'mongodb://localhost:27017', | ||
| }, | ||
| ]); | ||
| } | ||
| } | ||
| switch (source) { | ||
| case 'globalSettings': | ||
| await vscode.commands.executeCommand( | ||
| 'workbench.action.openSettingsJson' | ||
| ); | ||
| break; | ||
| case 'workspaceSettings': | ||
| case 'user': | ||
| await vscode.commands.executeCommand( | ||
| 'workbench.action.openWorkspaceSettingsFile' | ||
| ); | ||
| break; | ||
| default: | ||
| throw new Error('Unknown preset connection source'); | ||
| } | ||
| } | ||
|
|
||
| async loadSavedConnections(): Promise<void> { | ||
| this._connections = Object.create(null); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this fixing a bug where deleted saved connections would show up because they're not overridden in the for loop? Couldn't spot a test that verifies the behavior before was incorrect - am I just blind or should we add one?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this hasn't been an issue because we'd only run |
||
|
|
||
| const loadedConnections = await this._connectionStorage.loadConnections(); | ||
|
|
||
| for (const connection of loadedConnections) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,11 @@ import formatError from '../utils/formatError'; | |
| import { getImagesPath } from '../extensionConstants'; | ||
| import type TreeItemParent from './treeItemParentInterface'; | ||
| import StreamProcessorTreeItem from './streamProcessorTreeItem'; | ||
| import type { ConnectionSource } from '../storage/connectionStorage'; | ||
|
|
||
| export enum ConnectionItemContextValues { | ||
| disconnected = 'disconnectedConnectionTreeItem', | ||
| connected = 'connectedConnectionTreeItem', | ||
| } | ||
| export type ConnectionItemContextValue = `${'disconnected' | 'connected'}${ | ||
| | '' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [extreme nit] The leading
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gets automatically added by the linter 🤷 |
||
| | 'Preset'}ConnectionTreeItem`; | ||
|
|
||
| function getIconPath(isActiveConnection: boolean): { | ||
| light: string; | ||
|
|
@@ -39,7 +39,7 @@ export default class ConnectionTreeItem | |
| extends vscode.TreeItem | ||
| implements TreeItemParent, vscode.TreeDataProvider<ConnectionTreeItem> | ||
| { | ||
| contextValue = ConnectionItemContextValues.disconnected; | ||
| contextValue: ConnectionItemContextValue = 'disconnectedConnectionTreeItem'; | ||
|
|
||
| private _childrenCache: { | ||
| [key: string]: DatabaseTreeItem | StreamProcessorTreeItem; | ||
|
|
@@ -50,6 +50,7 @@ export default class ConnectionTreeItem | |
| connectionId: string; | ||
|
|
||
| isExpanded: boolean; | ||
| source: ConnectionSource; | ||
|
|
||
| constructor({ | ||
| connectionId, | ||
|
|
@@ -58,6 +59,7 @@ export default class ConnectionTreeItem | |
| connectionController, | ||
| cacheIsUpToDate, | ||
| childrenCache, | ||
| source, | ||
| }: { | ||
| connectionId: string; | ||
| collapsibleState: vscode.TreeItemCollapsibleState; | ||
|
|
@@ -67,21 +69,24 @@ export default class ConnectionTreeItem | |
| childrenCache: { | ||
| [key: string]: DatabaseTreeItem | StreamProcessorTreeItem; | ||
| }; // Existing cache. | ||
| source: ConnectionSource; | ||
| }) { | ||
| super( | ||
| connectionController.getSavedConnectionName(connectionId), | ||
| collapsibleState | ||
| ); | ||
|
|
||
| if ( | ||
| const isConnected = | ||
| connectionController.getActiveConnectionId() === connectionId && | ||
| !connectionController.isDisconnecting() && | ||
| !connectionController.isConnecting() | ||
| ) { | ||
| this.contextValue = ConnectionItemContextValues.connected; | ||
| } | ||
| !connectionController.isConnecting(); | ||
|
|
||
| this.contextValue = `${isConnected ? 'connected' : 'disconnected'}${ | ||
| source === 'user' ? '' : 'Preset' | ||
| }ConnectionTreeItem`; | ||
|
|
||
| this.connectionId = connectionId; | ||
| this.source = source; | ||
| this._connectionController = connectionController; | ||
| this.isExpanded = isExpanded; | ||
| this._childrenCache = childrenCache; | ||
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -204,7 +209,9 @@ export default class ConnectionTreeItem | |
| return Object.values(this._childrenCache); | ||
| } | ||
|
|
||
| private async _buildChildrenCacheForDatabases(dataService: DataService) { | ||
| private async _buildChildrenCacheForDatabases( | ||
| dataService: DataService | ||
| ): Promise<Record<string, DatabaseTreeItem>> { | ||
| const databases = await this.listDatabases(); | ||
| databases.sort((a: string, b: string) => a.localeCompare(b)); | ||
|
|
||
|
|
@@ -226,7 +233,9 @@ export default class ConnectionTreeItem | |
| return newChildrenCache; | ||
| } | ||
|
|
||
| private async _buildChildrenCacheForStreams(dataService: DataService) { | ||
| private async _buildChildrenCacheForStreams( | ||
| dataService: DataService | ||
| ): Promise<Record<string, StreamProcessorTreeItem>> { | ||
| const processors = await this.listStreamProcessors(); | ||
| processors.sort((a, b) => a.name.localeCompare(b.name)); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.