more interactive refresh and ux improvements

This commit is contained in:
blumia committed 2026-05-09 14:20:45 +08:00
1 parent 3f834ebd3d
commit 6afd4647ae
5 files changed
+234 -61

No files matched your search

+163 -32
View File
@@ -4,9 +4,17 @@ import { QuotaResponse, QuotaDisplay } from './types';
export class StatusBarManager {
private statusBarItem: vscode.StatusBarItem;
private currentPanel: vscode.WebviewPanel | undefined;
private onRefresh: () => Promise<QuotaDisplay | null>;
private onRefresh: () => Promise<{
data: QuotaDisplay | null;
lastRefreshAt: Date | null;
error: string | null;
}>;
constructor(onRefresh: () => Promise<QuotaDisplay | null>) {
constructor(onRefresh: () => Promise<{
data: QuotaDisplay | null;
lastRefreshAt: Date | null;
error: string | null;
}>) {
this.onRefresh = onRefresh;
this.statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right,
@@ -78,18 +86,23 @@ export class StatusBarManager {
return md;
}
showPopup(data: QuotaDisplay | null, isLoggedIn: boolean): void {
showPopup(
data: QuotaDisplay | null,
isLoggedIn: boolean,
lastRefreshAt: Date | null,
lastRefreshError: string | null
): void {
if (!isLoggedIn) {
this.showLoginPrompt();
return;
}
if (!data) {
this.showErrorPrompt();
this.showErrorPrompt(lastRefreshAt, lastRefreshError);
return;
}
this.showQuotaPanel(data);
this.showQuotaPanel(data, lastRefreshAt, lastRefreshError);
}
private showLoginPrompt(): void {
@@ -103,27 +116,38 @@ export class StatusBarManager {
});
}
private showErrorPrompt(): void {
private showErrorPrompt(lastRefreshAt: Date | null, lastRefreshError: string | null): void {
vscode.window.showQuickPick(
[
{ label: '$(refresh) 重试', id: 'refresh' },
{ label: '$(preview) 查看详情', id: 'details' },
{ label: '$(key) 重新设置 Token', id: 'login' }
],
{ placeHolder: '获取用量数据失败' }
{
placeHolder: lastRefreshError
? `获取用量数据失败: ${lastRefreshError}`
: '获取用量数据失败'
}
).then(selection => {
const item = selection as any;
if (item?.id === 'refresh') {
vscode.commands.executeCommand('getdeepin-quota.refresh');
} else if (item?.id === 'details') {
this.showQuotaPanel(null, lastRefreshAt, lastRefreshError);
} else if (item?.id === 'login') {
vscode.commands.executeCommand('getdeepin-quota.login');
}
});
}
private showQuotaPanel(data: QuotaDisplay): void {
private showQuotaPanel(
data: QuotaDisplay | null,
lastRefreshAt: Date | null,
lastRefreshError: string | null
): void {
if (this.currentPanel) {
this.currentPanel.reveal(vscode.ViewColumn.Active);
this.currentPanel.webview.html = this.generatePanelHtml(data);
this.currentPanel.webview.html = this.generatePanelHtml(data, lastRefreshAt, lastRefreshError);
return;
}
@@ -135,13 +159,24 @@ export class StatusBarManager {
);
this.currentPanel = panel;
panel.webview.html = this.generatePanelHtml(data);
panel.webview.html = this.generatePanelHtml(data, lastRefreshAt, lastRefreshError);
panel.webview.onDidReceiveMessage(async (message) => {
if (message.command === 'refresh') {
const newData = await this.onRefresh();
if (newData && this.currentPanel) {
this.currentPanel.webview.html = this.generatePanelHtml(newData);
try {
const result = await this.onRefresh();
if (this.currentPanel === panel) {
this.currentPanel.webview.html = this.generatePanelHtml(
result.data,
result.lastRefreshAt,
result.error
);
return;
}
} finally {
if (this.currentPanel === panel) {
void this.currentPanel.webview.postMessage({ command: 'refreshFinished' });
}
}
}
});
@@ -151,9 +186,16 @@ export class StatusBarManager {
});
}
private generatePanelHtml(data: QuotaDisplay): string {
private generatePanelHtml(
data: QuotaDisplay | null,
lastRefreshAt: Date | null,
lastRefreshError: string | null
): string {
const formatTokens = (n: number) => n.toLocaleString('zh-CN');
const formatPercent = (n: number) => n.toFixed(1);
const formatTime = (time: Date | null) => time
? time.toLocaleString('zh-CN', { hour12: false })
: '暂无';
const renderQuotaSection = (
label: string,
@@ -203,6 +245,39 @@ export class StatusBarManager {
return html;
};
const refreshMetaHtml = `
<div class="meta-row">
<span class="meta-label">最后刷新</span>
<span class="meta-value">${formatTime(lastRefreshAt)}</span>
</div>
${lastRefreshError ? `
<div class="error-banner">
<strong>刷新失败</strong>
<div>${lastRefreshError}</div>
</div>` : ''}`;
const quotaContentHtml = data ? `
${renderQuotaSection(
data.domestic.label,
data.domestic.remainingDailyPercent,
data.domestic.remainingMonthlyPercent,
data.domestic.hasDailyLimit,
data.domestic.totalTokens,
data.domestic.models
)}
${renderQuotaSection(
data.global.label,
data.global.remainingDailyPercent,
data.global.remainingMonthlyPercent,
data.global.hasDailyLimit,
data.global.totalTokens,
data.global.models
)}` : `
<div class="empty-state">
当前没有可展示的额度数据,请检查 Token 或稍后重试。
</div>`;
return `
<!DOCTYPE html>
<html>
@@ -227,6 +302,37 @@ export class StatusBarManager {
.user-info {
color: var(--vscode-descriptionForeground);
}
.meta-row {
display: flex;
gap: 8px;
margin-bottom: 16px;
color: var(--vscode-descriptionForeground);
}
.meta-label {
min-width: 60px;
}
.meta-value {
color: var(--vscode-foreground);
}
.error-banner {
margin-bottom: 20px;
padding: 12px 14px;
border-radius: 6px;
border: 1px solid var(--vscode-inputValidation-errorBorder);
background-color: var(--vscode-inputValidation-errorBackground);
color: var(--vscode-inputValidation-errorForeground);
}
.error-banner strong {
display: block;
margin-bottom: 4px;
}
.empty-state {
margin: 20px 0 24px;
padding: 16px;
border: 1px dashed var(--vscode-panel-border);
border-radius: 6px;
color: var(--vscode-descriptionForeground);
}
.quota-section {
margin-bottom: 24px;
}
@@ -294,45 +400,70 @@ export class StatusBarManager {
border-radius: 4px;
cursor: pointer;
font-size: 14px;
display: inline-flex;
align-items: center;
gap: 6px;
}
.refresh-btn:hover {
background-color: var(--vscode-button-hoverBackground);
}
.refresh-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
display: inline-block;
width: 14px;
height: 14px;
border: 2px solid var(--vscode-button-foreground);
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.6s linear infinite;
}
</style>
</head>
<body>
<div class="header">
<h1>AI 模型用量统计</h1>
${data ? `
<div class="user-info">
<strong>${data.user.display_name}</strong> (${data.user.job_id}) · ${data.user.email}
</div>
</div>` : ''}
</div>
${renderQuotaSection(
data.domestic.label,
data.domestic.remainingDailyPercent,
data.domestic.remainingMonthlyPercent,
data.domestic.hasDailyLimit,
data.domestic.totalTokens,
data.domestic.models
)}
${refreshMetaHtml}
${renderQuotaSection(
data.global.label,
data.global.remainingDailyPercent,
data.global.remainingMonthlyPercent,
data.global.hasDailyLimit,
data.global.totalTokens,
data.global.models
)}
${quotaContentHtml}
<div class="actions">
<button class="refresh-btn" onclick="refresh()">刷新数据</button>
<button id="refresh-btn" class="refresh-btn" onclick="refresh()">刷新数据</button>
</div>
<script>
const vscode = acquireVsCodeApi();
function setRefreshing(refreshing) {
const btn = document.getElementById('refresh-btn');
if (!btn) {
return;
}
btn.disabled = refreshing;
btn.innerHTML = refreshing
? '<span class="spinner"></span>刷新中...'
: '刷新数据';
}
window.addEventListener('message', (event) => {
if (event.data?.command === 'refreshFinished') {
setRefreshing(false);
}
});
function refresh() {
setRefreshing(true);
vscode.postMessage({ command: 'refresh' });
}
</script>