forked from KathiraveluLab/DHGWorkflow
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlocal-storage-manager.js
More file actions
99 lines (90 loc) · 2.6 KB
/
Copy pathlocal-storage-manager.js
File metadata and controls
99 lines (90 loc) · 2.6 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { toast } from 'react-toastify';
const localStorageGet = (key) => {
try {
return window.localStorage.getItem(key);
} catch (e) {
toast.error(e.message);
return null;
}
};
const localStorageSet = (key, value) => {
try {
window.localStorage.setItem(key, value);
} catch (e) {
toast.error(e.message);
}
};
const localStorageRemove = (key) => {
try {
window.localStorage.removeItem(key);
} catch (e) {
toast.error(e.message);
}
};
const getSet = (ALL_GRAPHS) => {
if (!localStorageGet(ALL_GRAPHS)) {
localStorageSet(ALL_GRAPHS, window.btoa(JSON.stringify([])));
}
const raw = localStorageGet(ALL_GRAPHS);
if (!raw) return new Set();
return new Set(JSON.parse(window.atob(raw)));
};
const localStorageManager = {
ALL_GRAPHS: window.btoa('ALL_GRAPHS'),
AUTHOR_NAME: window.btoa('AUTHOR_NAME'),
allgs: getSet(window.btoa('ALL_GRAPHS')),
saveAllgs() {
localStorageSet(this.ALL_GRAPHS, window.btoa(JSON.stringify(Array.from(this.allgs))));
},
addEmptyIfNot() {
if (!localStorageGet(this.ALL_GRAPHS)) {
localStorageSet(this.ALL_GRAPHS, window.btoa(JSON.stringify([])));
}
},
get(id) {
const raw = localStorageGet(id);
if (raw === null) return null;
return JSON.parse(window.atob(raw));
},
save(id, graphContent) {
this.addGraph(id);
const serializedJson = JSON.stringify(graphContent);
localStorageSet(id, window.btoa(serializedJson));
},
remove(id) {
if (this.allgs.delete(id)) this.saveAllgs();
localStorageRemove(id);
},
addGraph(id) {
if (this.allgs.has(id)) return;
this.allgs.add(id);
this.saveAllgs();
},
getAllGraphs() {
const raw = localStorageGet(this.ALL_GRAPHS);
if (!raw) return [];
return JSON.parse(window.atob(raw));
},
addToFront(id) {
if (this.allgs.has(id)) return;
this.allgs.add(id);
const raw = localStorageGet(this.ALL_GRAPHS);
if (!raw) return;
const Garr = JSON.parse(window.atob(raw));
Garr.unshift(id);
localStorageSet(this.ALL_GRAPHS, window.btoa(JSON.stringify(Garr)));
},
getAuthorName() {
return localStorageGet(this.AUTHOR_NAME) || '';
},
setAuthorName(authorName) {
localStorageSet(this.AUTHOR_NAME, authorName);
},
clearGraph(id) {
localStorageRemove(id);
},
getFileList() {
return localStorageGet('fileList') || '';
},
};
export default localStorageManager;