[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 private readonly webUrl: string
) {} ) {}
public resolveWebviewView(webviewView: vscode.WebviewView) { private async handleLogin(
const webview = webviewView.webview; teamName: string,
webview.options = { password: string,
enableScripts: true, webviewPostMessage: (m: WebviewMessageType) => void
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': {
const res = await fetch(urlJoin(this.webUrl, '/api/team/login'), { const res = await fetch(urlJoin(this.webUrl, '/api/team/login'), {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
teamname: m.data.teamName, teamname: teamName,
password: m.data.password password: password
}) })
}); });
const resData = await res.json(); const resData = await res.json();
@ -85,9 +54,9 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
} }
this.context.globalState.update('teamData', data2.data); this.context.globalState.update('teamData', data2.data);
webviewPostMessage({ msg: 'onLogin', data: 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'); const sessionToken = this.context.globalState.get<string>('token');
if (sessionToken === undefined) { if (sessionToken === undefined) {
webviewPostMessage({ msg: 'onLogout' }); webviewPostMessage({ msg: 'onLogout' });
@ -107,26 +76,51 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
webviewPostMessage({ msg: 'onLogout' }); webviewPostMessage({ msg: 'onLogout' });
this.context.globalState.update('token', undefined); 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; 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;
// }
} }
}); });
} }