[extension] Clean up sidebar provider

This commit is contained in:
orosmatthew 2023-10-17 12:58:34 -04:00
parent cb7383a437
commit 8691bcfcd1

View File

@ -25,48 +25,17 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
private readonly webUrl: string
) {}
public resolveWebviewView(webviewView: vscode.WebviewView) {
const webview = webviewView.webview;
webview.options = {
enableScripts: true,
localResourceRoots: [this.extensionUri]
};
webview.html = this.getHtmlForWebview(webview);
const webviewPostMessage = (m: WebviewMessageType) => {
webview.postMessage(m);
};
webview.onDidReceiveMessage(async (m: MessageType) => {
switch (m.msg) {
case 'onTestAndSubmit': {
if (this.context) {
BWPanel.show(this.context, this.webUrl);
}
break;
}
case 'onStartup': {
const token: string | undefined = this.context.globalState.get('token');
const teamData: TeamData | undefined = this.context.globalState.get('teamData');
if (token && teamData !== undefined) {
webviewPostMessage({
msg: 'onLogin',
data: teamData
});
}
break;
}
case 'onClone': {
await cloneAndOpenRepo(m.data.contestId, m.data.teamId);
break;
}
case 'onLogin': {
private async handleLogin(
teamName: string,
password: string,
webviewPostMessage: (m: WebviewMessageType) => void
) {
const res = await fetch(urlJoin(this.webUrl, '/api/team/login'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
teamname: m.data.teamName,
password: m.data.password
teamname: teamName,
password: password
})
});
const resData = await res.json();
@ -85,9 +54,9 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
}
this.context.globalState.update('teamData', data2.data);
webviewPostMessage({ msg: 'onLogin', data: data2.data });
break;
}
case 'onLogout': {
private async handleLogout(webviewPostMessage: (m: WebviewMessageType) => void) {
const sessionToken = this.context.globalState.get<string>('token');
if (sessionToken === undefined) {
webviewPostMessage({ msg: 'onLogout' });
@ -107,26 +76,51 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
webviewPostMessage({ msg: 'onLogout' });
this.context.globalState.update('token', undefined);
}
}
public resolveWebviewView(webviewView: vscode.WebviewView) {
const webview = webviewView.webview;
webview.options = {
enableScripts: true,
localResourceRoots: [this.extensionUri]
};
webview.html = this.getHtmlForWebview(webview);
const webviewPostMessage = (m: WebviewMessageType) => {
webview.postMessage(m);
};
webview.onDidReceiveMessage((m: MessageType) => {
switch (m.msg) {
case 'onTestAndSubmit': {
if (this.context) {
BWPanel.show(this.context, this.webUrl);
}
break;
}
case 'onStartup': {
const token = this.context.globalState.get<string>('token');
const teamData = this.context.globalState.get<TeamData>('teamData');
if (token !== undefined && teamData !== undefined) {
webviewPostMessage({
msg: 'onLogin',
data: teamData
});
}
break;
}
case 'onClone': {
cloneAndOpenRepo(m.data.contestId, m.data.teamId);
break;
}
case 'onLogin': {
this.handleLogin(m.data.teamName, m.data.password, webviewPostMessage);
break;
}
case 'onLogout': {
this.handleLogout(webviewPostMessage);
break;
}
// case 'onLogout': {
// this.context.globalState.update('token', null);
// break;
// }
// case 'onInfo': {
// if (!data.value) {
// return;
// }
// vscode.window.showInformationMessage(data.value);
// break;
// }
// case 'onError': {
// if (!data.value) {
// return;
// }
// vscode.window.showErrorMessage(data.value);
// break;
// }
}
});
}