Skip to content

Commit

Permalink
Clean up webview factory
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Feb 16, 2023
1 parent 02a0fc3 commit 7ea72df
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 30 deletions.
3 changes: 2 additions & 1 deletion extension/src/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import { openUrl } from '../vscode/external'
import { ContextKey, setContextValue } from '../vscode/context'
import { RegisteredCommands } from '../commands/external'
import { showInformation } from '../vscode/modal'
import { GLOBAL_WEBVIEW_DVCROOT } from '../webview/factory'

export class Connect extends BaseRepository<undefined> {
public readonly viewKey = ViewKey.CONNECT

private readonly secrets: SecretStorage

constructor(context: ExtensionContext, webviewIcon: Resource) {
super('', webviewIcon)
super(GLOBAL_WEBVIEW_DVCROOT, webviewIcon)

this.secrets = context.secrets

Expand Down
3 changes: 2 additions & 1 deletion extension/src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TSetupData>

Expand Down Expand Up @@ -96,7 +97,7 @@ export class Setup
webviewIcon: Resource,
collectWorkspaceScale: () => Promise<WorkspaceScale>
) {
super('', webviewIcon)
super(GLOBAL_WEBVIEW_DVCROOT, webviewIcon)

this.config = config
this.dvcExecutor = dvcExecutor
Expand Down
4 changes: 0 additions & 4 deletions extension/src/webview/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,3 @@ export type MessageToWebview<T extends WebviewData> = {
export enum MessageToWebviewType {
SET_DATA = 'setData'
}

export interface WebviewState {
dvcRoot: string
}
29 changes: 7 additions & 22 deletions extension/src/webview/factory.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}

Expand All @@ -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]

Expand All @@ -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 = <T extends WebviewData>(
viewKey: ViewKey,
webviewPanel: WebviewPanel,
state: WebviewState
): Promise<BaseWebview<T>> => {
return new Promise((resolve, reject) => {
try {
resolve(create(viewKey, webviewPanel, state.dvcRoot))
} catch (error: unknown) {
reject(error)
}
})
}
3 changes: 1 addition & 2 deletions extension/src/webview/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7ea72df

Please sign in to comment.