Files
vscode-getdeepin-quota/src/extension.ts
T
2026-04-22 15:28:45 +08:00

105 lines
3.1 KiB
TypeScript

import * as vscode from 'vscode';
import { StorageService } from './storageService';
import { AuthService, QuotaService } from './authService';
import { StatusBarManager, parseQuotaResponse } from './statusBar';
import { QuotaResponse, QuotaDisplay } from './types';
let storageService: StorageService;
let authService: AuthService;
let quotaService: QuotaService;
let statusBar: StatusBarManager;
let currentQuota: QuotaDisplay | null = null;
export async function activate(context: vscode.ExtensionContext) {
storageService = new StorageService(context);
authService = new AuthService(storageService);
quotaService = new QuotaService(authService);
statusBar = new StatusBarManager(() => {
vscode.commands.executeCommand('getdeepin-quota.showPopup');
});
const autoLoggedIn = await authService.autoLogin();
if (autoLoggedIn) {
await refreshQuota();
} else {
statusBar.updateNotLoggedIn();
}
context.subscriptions.push(
vscode.commands.registerCommand('getdeepin-quota.login', loginCommand),
vscode.commands.registerCommand('getdeepin-quota.logout', logoutCommand),
vscode.commands.registerCommand('getdeepin-quota.refresh', refreshQuota),
vscode.commands.registerCommand('getdeepin-quota.showPopup', showPopup),
statusBar
);
}
async function loginCommand(): Promise<void> {
const username = await vscode.window.showInputBox({
prompt: '请输入用户名',
placeHolder: '用户名'
});
if (!username) {
return;
}
const password = await vscode.window.showInputBox({
prompt: '请输入密码',
password: true
});
if (!password) {
return;
}
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: '正在登录...',
cancellable: false
},
async () => {
const success = await authService.login(username, password);
if (success) {
vscode.window.showInformationMessage('登录成功');
await refreshQuota();
} else {
vscode.window.showErrorMessage('登录失败,请检查用户名和密码');
statusBar.updateNotLoggedIn();
}
}
);
}
async function logoutCommand(): Promise<void> {
await authService.logout();
currentQuota = null;
statusBar.updateNotLoggedIn();
vscode.window.showInformationMessage('已登出');
}
async function refreshQuota(): Promise<void> {
const response = await quotaService.fetchQuota();
if (!response) {
statusBar.updateWithError();
return;
}
try {
const data = await response.json() as QuotaResponse;
currentQuota = parseQuotaResponse(data);
statusBar.updateWithQuota(currentQuota);
} catch (error) {
console.error('Parse quota response failed:', error);
statusBar.updateWithError();
}
}
function showPopup(): void {
statusBar.showPopup(currentQuota, authService.isLoggedIn());
}
export function deactivate() {}