Skip to content

Commit ca0a3dd

Browse files
committed
fix: preserve current page on browser refresh via URL hash routing
The SPA kept tab state only in JS memory, so refreshing reset to the main dashboard. Add hash-based routing so the active page (and config sub-tab) survives refresh and back/forward navigation. - showPage() writes the page to location.hash - showConfigPanel() persists the config sub-tab (#configuration/inverter) - restoreFromHash() restores page + sub-tab on load and hashchange
1 parent ea13b1b commit ca0a3dd

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎api/static/index.html‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,14 +2260,41 @@ <h4 style="margin-bottom:0.5rem;">${g.title}</h4>
22602260
if (page === 'historical') loadHistorical();
22612261
if (page === 'automations') initAutomation();
22622262
if (page === 'configuration') { loadConfiguration(); populateTimezones(); }
2263+
2264+
// Persist the current page in the URL hash so a browser refresh (or
2265+
// back/forward) restores the same page instead of resetting to main.
2266+
if (location.hash !== '#' + page) {
2267+
history.replaceState(null, '', '#' + page);
2268+
}
22632269
}
22642270
function showConfigPanel(name) {
22652271
document.querySelectorAll('.config-panel').forEach(p => p.classList.remove('active'));
22662272
document.getElementById('config-' + name).classList.add('active');
22672273
document.querySelectorAll('.config-tab').forEach(b => b.classList.toggle('active', b.dataset.config === name));
2274+
// Persist the config sub-tab in the hash (e.g. #configuration/inverter).
2275+
if (location.hash.startsWith('#configuration')) {
2276+
history.replaceState(null, '', '#configuration/' + name);
2277+
}
2278+
}
2279+
2280+
// Restore the page (and optional config sub-tab) from the URL hash.
2281+
function restoreFromHash() {
2282+
const hash = (location.hash || '').replace(/^#/, '');
2283+
if (!hash) return;
2284+
const [page, sub] = hash.split('/');
2285+
const validPages = ['main', 'detail', 'charts', 'historical', 'automations', 'configuration'];
2286+
if (validPages.includes(page)) {
2287+
showPage(page);
2288+
if (page === 'configuration' && sub) showConfigPanel(sub);
2289+
}
22682290
}
22692291

2292+
window.addEventListener('hashchange', restoreFromHash);
2293+
22702294
document.addEventListener('DOMContentLoaded', async () => {
2295+
// Restore the page from the URL hash (survives refresh / back / forward).
2296+
restoreFromHash();
2297+
22712298
document.querySelectorAll('#top-menu .menu-item').forEach(n => {
22722299
n.addEventListener('click', e => { e.preventDefault(); showPage(n.dataset.page); });
22732300
});

0 commit comments

Comments
 (0)