Repository navigation
Expand file tree
/
Copy pathstorage.js
More file actions
68 lines (62 loc) · 1.58 KB
/
Copy pathstorage.js
File metadata and controls
68 lines (62 loc) · 1.58 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
(function () {
'use strict';
const KEY = 'fokuskoc_v1';
const SCHEMA_VERSION = 1;
const DEFAULT = {
schemaVersion: SCHEMA_VERSION,
goals: [],
tasks: [],
sessions: [],
checkins: [],
chatHistory: [],
settings: {
lang: 'tr',
pomodoro: { focus: 25, break: 5 },
},
};
function load() {
try {
const raw = localStorage.getItem(KEY);
if (!raw) return structuredClone(DEFAULT);
const data = JSON.parse(raw);
return { ...structuredClone(DEFAULT), ...data, settings: { ...DEFAULT.settings, ...(data.settings || {}) } };
} catch (e) {
console.warn('Storage load failed, resetting', e);
return structuredClone(DEFAULT);
}
}
function save(data) {
try {
if (data.chatHistory && data.chatHistory.length > 100) {
data.chatHistory = data.chatHistory.slice(-100);
}
localStorage.setItem(KEY, JSON.stringify(data));
} catch (e) {
console.warn('Storage save failed', e);
}
}
const db = {
_cache: null,
get(key) {
if (!this._cache) this._cache = load();
return key ? this._cache[key] : this._cache;
},
set(key, value) {
if (!this._cache) this._cache = load();
this._cache[key] = value;
save(this._cache);
},
update(key, fn) {
const current = this.get(key);
this.set(key, fn(current));
},
reset() {
this._cache = structuredClone(DEFAULT);
save(this._cache);
},
uid() {
return Date.now().toString(36) + Math.random().toString(36).slice(2, 7);
},
};
window.db = db;
})();