Ver código fonte

Refactor `webviewState` to just `state`

Saoud Rizwan 1 ano atrás
pai
commit
62e6ad0a78

+ 6 - 6
src/providers/SidebarProvider.ts

@@ -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 },
 		})
 	}
 

+ 2 - 2
src/shared/ExtensionMessage.ts

@@ -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 }
 }

+ 5 - 5
webview-ui/src/App.tsx

@@ -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