Repository navigation
Expand file tree
/
Copy pathstate.js
More file actions
77 lines (63 loc) · 2.21 KB
/
Copy pathstate.js
File metadata and controls
77 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const STATE_VERSION = 1;
const STORAGE_KEY = 'bits_path_state';
const STATE = {
version: STATE_VERSION,
currentSemester: 0,
revealedSemesters: [],
pathType: null,
forkChosen: false,
forkUIShown: false,
selections: {
},
activeSidebarContent: null,
activeGridSlot: null,
theme: 'light'
};
function saveState() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(STATE));
} catch (e) {
}
}
function loadState() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return;
const saved = JSON.parse(raw);
if (saved.version !== STATE_VERSION) {
console.warn(
`[STATE] localStorage version mismatch (saved: ${saved.version}, expected: ${STATE_VERSION}). ` +
'Resetting to defaults.'
);
localStorage.removeItem(STORAGE_KEY);
return;
}
STATE.currentSemester = typeof saved.currentSemester === 'number' ? saved.currentSemester : 0;
STATE.revealedSemesters = Array.isArray(saved.revealedSemesters) ? saved.revealedSemesters : [];
STATE.pathType = saved.pathType ?? null;
STATE.forkChosen = typeof saved.forkChosen === 'boolean' ? saved.forkChosen : false;
STATE.forkUIShown = typeof saved.forkUIShown === 'boolean' ? saved.forkUIShown : false;
STATE.selections = (saved.selections && typeof saved.selections === 'object') ? saved.selections : {};
STATE.activeSidebarContent = null;
STATE.activeGridSlot = null;
STATE.theme = (saved.theme === 'dark') ? 'dark' : 'light';
} catch (e) {
console.warn('[STATE] Failed to parse localStorage state. Resetting to defaults.');
localStorage.removeItem(STORAGE_KEY);
}
}
function resetState() {
localStorage.removeItem(STORAGE_KEY);
STATE.version = STATE_VERSION;
STATE.currentSemester = 0;
STATE.revealedSemesters = [];
STATE.pathType = null;
STATE.forkChosen = false;
STATE.forkUIShown = false;
STATE.selections = {};
STATE.activeSidebarContent = null;
STATE.activeGridSlot = null;
if (typeof renderApp === 'function') {
renderApp();
}
}