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 14, 2023
1 parent a78e1ef commit 75c4fed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
4 changes: 0 additions & 4 deletions extension/src/webview/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,3 @@ export type MessageToWebview<T extends WebviewData> = {
export enum MessageToWebviewType {
SET_DATA = 'setData'
}

export interface WebviewState {
dvcRoot: string
}
30 changes: 8 additions & 22 deletions extension/src/webview/factory.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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
const isValidDvcRoot = (dvcRoot?: string): dvcRoot is string => !!dvcRoot

const needsDvcRoot = (viewKey: ViewKey): boolean =>
![ViewKey.CONNECT, ViewKey.SETUP].includes(viewKey)

const create = (
viewKey: ViewKey,
webviewPanel: WebviewPanel,
dvcRoot: string,
bypassDvcRoot?: boolean
dvcRoot: string
) => {
if (!bypassDvcRoot && !isValidDvcRoot(dvcRoot)) {
if (needsDvcRoot(viewKey) && !isValidDvcRoot(dvcRoot)) {
throw new Error(`trying to set invalid state into ${viewKey}`)
}

Expand All @@ -26,8 +27,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 +44,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 75c4fed

Please sign in to comment.