Skip to content

Commit 53e261d

Browse files
fix: make the usage/costs linkable and persistant place with page refresh
1 parent e1394ec commit 53e261d

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

internal/admin/dashboard/static/js/dashboard.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,35 @@ function dashboard() {
4646
usageLogProvider: '',
4747
usageDonutChart: null,
4848

49+
_parseRoute(pathname) {
50+
const path = pathname.replace(/\/$/, '');
51+
const rest = path.replace('/admin/dashboard', '').replace(/^\//, '');
52+
const parts = rest.split('/');
53+
const page = (['overview', 'usage', 'models'].includes(parts[0])) ? parts[0] : 'overview';
54+
const sub = parts[1] || null;
55+
return { page, sub };
56+
},
57+
4958
init() {
5059
this.apiKey = localStorage.getItem('gomodel_api_key') || '';
5160
this.theme = localStorage.getItem('gomodel_theme') || 'system';
5261
this.sidebarCollapsed = localStorage.getItem('gomodel_sidebar_collapsed') === 'true';
5362
this.applyTheme();
5463

55-
// Parse initial page from URL path
56-
const path = window.location.pathname.replace(/\/$/, '');
57-
const slug = path.split('/').pop();
58-
this.page = (slug === 'models') ? 'models' : (slug === 'usage') ? 'usage' : 'overview';
64+
// Parse initial page and sub-path from URL
65+
const { page, sub } = this._parseRoute(window.location.pathname);
66+
this.page = page;
67+
if (page === 'usage' && sub === 'costs') this.usageMode = 'costs';
5968

6069
// Handle browser back/forward
6170
window.addEventListener('popstate', () => {
62-
const p = window.location.pathname.replace(/\/$/, '');
63-
const s = p.split('/').pop();
64-
this.page = (s === 'models') ? 'models' : (s === 'usage') ? 'usage' : 'overview';
65-
if (this.page === 'overview') this.renderChart();
66-
if (this.page === 'usage') { this.fetchUsagePage(); }
71+
const { page: p, sub: s } = this._parseRoute(window.location.pathname);
72+
this.page = p;
73+
if (p === 'usage') {
74+
this.usageMode = s === 'costs' ? 'costs' : 'tokens';
75+
this.fetchUsagePage();
76+
}
77+
if (p === 'overview') this.renderChart();
6778
});
6879

6980
// Re-render chart when system theme changes (only matters in 'system' mode)
@@ -302,6 +313,7 @@ function dashboard() {
302313

303314
navigate(page) {
304315
this.page = page;
316+
if (page === 'usage') this.usageMode = 'tokens';
305317
history.pushState(null, '', '/admin/dashboard/' + page);
306318
if (page === 'overview') this.renderChart();
307319
if (page === 'usage') { this.fetchUsagePage(); }
@@ -678,6 +690,8 @@ function dashboard() {
678690

679691
toggleUsageMode(mode) {
680692
this.usageMode = mode;
693+
const url = mode === 'costs' ? '/admin/dashboard/usage/costs' : '/admin/dashboard/usage';
694+
history.pushState(null, '', url);
681695
this.renderDonutChart();
682696
},
683697

0 commit comments

Comments
 (0)