This commit is contained in:
@@ -128,7 +128,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
function loadStore() {
|
||||
function loadStoreLocal() {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY_V3);
|
||||
if (!raw) {
|
||||
@@ -145,14 +145,44 @@
|
||||
}
|
||||
}
|
||||
|
||||
let persistTimer = null;
|
||||
|
||||
async function loadStoreFromBackend() {
|
||||
try {
|
||||
const res = await fetch("/api/dashboards", { credentials: "include" });
|
||||
if (!res.ok) {
|
||||
loadStoreLocal();
|
||||
return;
|
||||
}
|
||||
const data = await res.json();
|
||||
store.dashboards = Array.isArray(data.dashboards) ? data.dashboards : [];
|
||||
store.activeDashboardId = data.activeDashboardId || store.dashboards[0]?.id || null;
|
||||
if (!store.dashboards.length) bootstrapDefaultDashboard();
|
||||
else if (!store.activeDashboardId) store.activeDashboardId = store.dashboards[0].id;
|
||||
} catch {
|
||||
loadStoreLocal();
|
||||
}
|
||||
}
|
||||
|
||||
function persistStore() {
|
||||
localStorage.setItem(
|
||||
STORAGE_KEY_V3,
|
||||
JSON.stringify({
|
||||
dashboards: store.dashboards,
|
||||
activeDashboardId: store.activeDashboardId,
|
||||
})
|
||||
);
|
||||
clearTimeout(persistTimer);
|
||||
persistTimer = setTimeout(syncStoreToBackend, 400);
|
||||
}
|
||||
|
||||
async function syncStoreToBackend() {
|
||||
try {
|
||||
await fetch("/api/dashboards", {
|
||||
credentials: "include",
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
dashboards: store.dashboards,
|
||||
activeDashboardId: store.activeDashboardId,
|
||||
}),
|
||||
});
|
||||
} catch {
|
||||
/* keep in-memory state; retry on next persist */
|
||||
}
|
||||
}
|
||||
|
||||
async function loadUserGroups() {
|
||||
@@ -765,7 +795,7 @@
|
||||
}
|
||||
|
||||
async function init() {
|
||||
loadStore();
|
||||
await loadStoreFromBackend();
|
||||
await loadUserGroups();
|
||||
bindEvents();
|
||||
setView("list");
|
||||
|
||||
Reference in New Issue
Block a user