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

View File

@@ -2,8 +2,8 @@
// webview will hold state // webview will hold state
export interface ExtensionMessage { export interface ExtensionMessage {
type: "text" | "action" | "webviewState" type: "text" | "action" | "state"
text?: string text?: string
action?: "plusButtonTapped" | "settingsButtonTapped" 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 const message: ExtensionMessage = e.data
// switch message.type // switch message.type
switch (message.type) { switch (message.type) {
case "webviewState": case "state":
const shouldShowWelcome = !message.webviewState!.didOpenOnce || !message.webviewState!.apiKey const shouldShowWelcome = !message.state!.didOpenOnce || !message.state!.apiKey
setShowWelcome(shouldShowWelcome) setShowWelcome(shouldShowWelcome)
setApiKey(message.webviewState!.apiKey || "") setApiKey(message.state!.apiKey || "")
setMaxRequestsPerTask( setMaxRequestsPerTask(
message.webviewState!.maxRequestsPerTask !== undefined message.state!.maxRequestsPerTask !== undefined
? message.webviewState!.maxRequestsPerTask.toString() ? message.state!.maxRequestsPerTask.toString()
: "" : ""
) )
break break