Refactor webviewState to just state

This commit is contained in:
Saoud Rizwan
2024-07-07 21:19:16 -04:00
parent 2622777fc8
commit 62e6ad0a78
3 changed files with 13 additions and 13 deletions

View File

@@ -137,7 +137,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
switch (message.type) {
case "webviewDidLaunch":
await this.updateGlobalState("didOpenOnce", true)
await this.postWebviewState()
await this.postStateToWebview()
break
case "text":
// Code that should run in response to the hello message command
@@ -150,7 +150,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
break
case "apiKey":
await this.storeSecret("apiKey", message.text!)
await this.postWebviewState()
await this.postStateToWebview()
break
case "maxRequestsPerTask":
let result: number | undefined = undefined
@@ -161,7 +161,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
}
}
await this.updateGlobalState("maxRequestsPerTask", result)
await this.postWebviewState()
await this.postStateToWebview()
break
// Add more switch case statements here as more webview message commands
// are created within the webview context (i.e. inside media/main.js)
@@ -169,15 +169,15 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
})
}
private async postWebviewState() {
private async postStateToWebview() {
const [didOpenOnce, apiKey, maxRequestsPerTask] = await Promise.all([
this.getGlobalState("didOpenOnce") as Promise<boolean | undefined>,
this.getSecret("apiKey") as Promise<string | undefined>,
this.getGlobalState("maxRequestsPerTask") as Promise<number | undefined>,
])
this.postMessageToWebview({
type: "webviewState",
webviewState: { didOpenOnce: !!didOpenOnce, apiKey: apiKey, maxRequestsPerTask: maxRequestsPerTask },
type: "state",
state: { didOpenOnce: !!didOpenOnce, apiKey: apiKey, maxRequestsPerTask: maxRequestsPerTask },
})
}

View File

@@ -2,8 +2,8 @@
// webview will hold state
export interface ExtensionMessage {
type: "text" | "action" | "webviewState"
type: "text" | "action" | "state"
text?: string
action?: "plusButtonTapped" | "settingsButtonTapped"
webviewState?: { didOpenOnce: boolean, apiKey?: string, maxRequestsPerTask?: number }
state?: { didOpenOnce: boolean, apiKey?: string, maxRequestsPerTask?: number }
}

View File

@@ -27,13 +27,13 @@ const App: React.FC = () => {
const message: ExtensionMessage = e.data
// switch message.type
switch (message.type) {
case "webviewState":
const shouldShowWelcome = !message.webviewState!.didOpenOnce || !message.webviewState!.apiKey
case "state":
const shouldShowWelcome = !message.state!.didOpenOnce || !message.state!.apiKey
setShowWelcome(shouldShowWelcome)
setApiKey(message.webviewState!.apiKey || "")
setApiKey(message.state!.apiKey || "")
setMaxRequestsPerTask(
message.webviewState!.maxRequestsPerTask !== undefined
? message.webviewState!.maxRequestsPerTask.toString()
message.state!.maxRequestsPerTask !== undefined
? message.state!.maxRequestsPerTask.toString()
: ""
)
break