Repository navigation
Expand file tree
/
Copy pathlearn-progress.ts
More file actions
59 lines (51 loc) · 1.76 KB
/
Copy pathlearn-progress.ts
File metadata and controls
59 lines (51 loc) · 1.76 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
export const STORAGE_KEY = "ag-learn";
export const LEGACY_KEYS = ["ag-learn-read", "ag_learn_progress"];
export const READ_TICK = `
<svg viewBox="0 0 16 16" width="16" height="16" fill="none" aria-hidden="true">
<circle cx="8" cy="8" r="7" fill="rgba(0, 208, 132, 0.16)" stroke="currentColor" stroke-width="1.5"></circle>
<path d="M4.8 8.1l2 2 4.4-4.4" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
`;
export const UNREAD_TICK = `
<svg viewBox="0 0 16 16" width="16" height="16" fill="none" aria-hidden="true">
<circle cx="8" cy="8" r="7" stroke="currentColor" stroke-width="1.5" opacity=".35"></circle>
</svg>
`;
export function loadReadIds(): Set<string> {
try {
const storage = globalThis.localStorage;
const sources = [
storage.getItem(STORAGE_KEY),
...LEGACY_KEYS.map((key) => storage.getItem(key)),
];
const ids = new Set<string>();
for (const raw of sources) {
if (!raw) continue;
const parsed = JSON.parse(raw);
if (Array.isArray(parsed)) {
parsed.forEach((id) => {
if (typeof id === "string") ids.add(id);
});
continue;
}
if (parsed && typeof parsed === "object") {
for (const [trackId, lessons] of Object.entries(parsed)) {
if (!lessons || typeof lessons !== "object") continue;
for (const [lessonSlug, read] of Object.entries(lessons)) {
if (read) ids.add(`${trackId}/${lessonSlug}`);
}
}
}
}
return ids;
} catch {
return new Set();
}
}
export function persistReadIds(ids: Set<string>): void {
try {
globalThis.localStorage.setItem(STORAGE_KEY, JSON.stringify([...ids]));
} catch {
// localStorage unavailable
}
}