From 4104496b6c4fc8b00e2fe9cd7a8d81ad1b656e18 Mon Sep 17 00:00:00 2001 From: Matt Seddon Date: Tue, 14 Feb 2023 16:35:36 +1100 Subject: [PATCH] Clean up webview factory --- extension/src/connect/index.ts | 3 ++- extension/src/setup/index.ts | 3 ++- extension/src/webview/contract.ts | 4 ---- extension/src/webview/factory.ts | 29 +++++++---------------------- extension/src/webview/repository.ts | 3 +-- 5 files changed, 12 insertions(+), 30 deletions(-) diff --git a/extension/src/connect/index.ts b/extension/src/connect/index.ts index 3914c814da..d1a2207a08 100644 --- a/extension/src/connect/index.ts +++ b/extension/src/connect/index.ts @@ -13,6 +13,7 @@ import { openUrl } from '../vscode/external' import { ContextKey, setContextValue } from '../vscode/context' import { RegisteredCommands } from '../commands/external' import { Modal } from '../vscode/modal' +import { GLOBAL_WEBVIEW_DVCROOT } from '../webview/factory' export class Connect extends BaseRepository { public readonly viewKey = ViewKey.CONNECT @@ -20,7 +21,7 @@ export class Connect extends BaseRepository { private readonly secrets: SecretStorage constructor(context: ExtensionContext, webviewIcon: Resource) { - super('', webviewIcon) + super(GLOBAL_WEBVIEW_DVCROOT, webviewIcon) this.secrets = context.secrets diff --git a/extension/src/setup/index.ts b/extension/src/setup/index.ts index 12a83082ac..84466a7d04 100644 --- a/extension/src/setup/index.ts +++ b/extension/src/setup/index.ts @@ -43,6 +43,7 @@ import { EventName } from '../telemetry/constants' import { WorkspaceScale } from '../telemetry/collect' import { gitPath } from '../cli/git/constants' import { DOT_DVC } from '../cli/dvc/constants' +import { GLOBAL_WEBVIEW_DVCROOT } from '../webview/factory' export type SetupWebviewWebview = BaseWebview @@ -96,7 +97,7 @@ export class Setup webviewIcon: Resource, collectWorkspaceScale: () => Promise ) { - super('', webviewIcon) + super(GLOBAL_WEBVIEW_DVCROOT, webviewIcon) this.config = config this.dvcExecutor = dvcExecutor diff --git a/extension/src/webview/contract.ts b/extension/src/webview/contract.ts index 14cb8e9f36..ccb4f574f1 100644 --- a/extension/src/webview/contract.ts +++ b/extension/src/webview/contract.ts @@ -218,7 +218,3 @@ export type MessageToWebview = { export enum MessageToWebviewType { SET_DATA = 'setData' } - -export interface WebviewState { - dvcRoot: string -} diff --git a/extension/src/webview/factory.ts b/extension/src/webview/factory.ts index 7c8f35f1a9..13a44200c9 100644 --- a/extension/src/webview/factory.ts +++ b/extension/src/webview/factory.ts @@ -1,19 +1,19 @@ import { Uri, ViewColumn, WebviewPanel, window } from 'vscode' import { BaseWebview } from '.' import { ViewKey, WebviewDetails } from './constants' -import { WebviewState, WebviewData } from './contract' import { Resource } from '../resourceLocator' import { getWorkspaceRootUris } from '../vscode/workspaceFolders' -export const isValidDvcRoot = (dvcRoot?: string): dvcRoot is string => !!dvcRoot +export const GLOBAL_WEBVIEW_DVCROOT = 'n/a' + +const isValidDvcRoot = (dvcRoot?: string): dvcRoot is string => !!dvcRoot const create = ( viewKey: ViewKey, webviewPanel: WebviewPanel, - dvcRoot: string, - bypassDvcRoot?: boolean + dvcRoot: string ) => { - if (!bypassDvcRoot && !isValidDvcRoot(dvcRoot)) { + if (!isValidDvcRoot(dvcRoot)) { throw new Error(`trying to set invalid state into ${viewKey}`) } @@ -26,8 +26,7 @@ export const createWebview = async ( viewKey: ViewKey, dvcRoot: string, iconPath: Resource, - viewColumn?: ViewColumn, - bypassDvcRoot?: boolean + viewColumn?: ViewColumn ) => { const { title, distPath } = WebviewDetails[viewKey] @@ -44,21 +43,7 @@ export const createWebview = async ( webviewPanel.iconPath = iconPath - const view = create(viewKey, webviewPanel, dvcRoot, bypassDvcRoot) + const view = create(viewKey, webviewPanel, dvcRoot) await view.isReady() return view } - -export const restoreWebview = ( - viewKey: ViewKey, - webviewPanel: WebviewPanel, - state: WebviewState -): Promise> => { - return new Promise((resolve, reject) => { - try { - resolve(create(viewKey, webviewPanel, state.dvcRoot)) - } catch (error: unknown) { - reject(error) - } - }) -} diff --git a/extension/src/webview/repository.ts b/extension/src/webview/repository.ts index 2cb8349e04..ac0019ffb6 100644 --- a/extension/src/webview/repository.ts +++ b/extension/src/webview/repository.ts @@ -47,8 +47,7 @@ export abstract class BaseRepository< this.viewKey, this.dvcRoot, this.webviewIcon, - viewColumn, - [ViewKey.CONNECT, ViewKey.SETUP].includes(this.viewKey) + viewColumn ) this.setWebview(webview)