Skip to content

Commit 5e14cd4

Browse files
authored
Merge pull request #359 from avinxshKD/fix/session-restore-local-storage
fix: restore session tabs on reload, remove allgs Set divergence
2 parents 8f09613 + b1b80b7 commit 5e14cd4

4 files changed

Lines changed: 22 additions & 56 deletions

File tree

src/GraphWorkspace.jsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@ import ConfirmModal from './component/modals/ConfirmModal';
44
import SearchPanel from './component/SearchPanel';
55
import { actionType as T } from './reducer';
66
import './graphWorkspace.css';
7-
// import localStorageManager from './graph-builder/local-storage-manager';
7+
import localStorageManager from './graph-builder/local-storage-manager';
88
import TabBar from './component/TabBar';
99
import Graph from './GraphArea';
1010

1111
const GraphComp = (props) => {
1212
const graphContainerRef = React.useRef();
1313
const { dispatcher, superState } = props;
14-
// const [loadedFromStorage, setLoadedFromStorage] = React.useState(false);
1514

16-
// The functionality for loading graphs from previous sessions is currently on hold.
17-
// useEffect(() => {
18-
// const allSavedGs = localStorageManager.getAllGraphs().map((graphID) => ({
19-
// graphID,
20-
// }));
21-
// dispatcher({
22-
// type: T.ADD_GRAPH_BULK,
23-
// payload: allSavedGs,
24-
// });
25-
// // setLoadedFromStorage(true);
26-
// }, []);
15+
React.useEffect(() => {
16+
const allIDs = localStorageManager.getAllGraphs();
17+
const validIDs = allIDs.filter((id) => typeof id === 'string' && id && localStorageManager.get(id) !== null);
18+
if (validIDs.length !== allIDs.length) {
19+
allIDs.filter((id) => !validIDs.includes(id)).forEach((id) => localStorageManager.remove(id));
20+
}
21+
if (validIDs.length === 0) return;
22+
dispatcher({ type: T.ADD_GRAPH_BULK, payload: validIDs.map((graphID) => ({ graphID })) });
23+
}, []);
2724

2825
// Remote server implementation - Not being used.
2926
// useEffect(() => {

src/component/modals/ProjectDetails.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const ProjectDetails = ({ superState, dispatcher }) => {
3939
superState.curGraphInstance.setProjectAuthor(authorName);
4040
dispatcher({ type: T.SET_EDIT_DETAILS_MODAL, payload: false });
4141
}
42-
localStorageManager.saveAllgs();
4342
localStorageManager.setAuthorName(authorName);
4443
};
4544

src/graph-builder/local-storage-manager.js

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,10 @@ const localStorageRemove = (key) => {
5050
}
5151
};
5252

53-
const getSet = (ALL_GRAPHS) => {
54-
if (!localStorageGet(ALL_GRAPHS)) {
55-
localStorageSet(ALL_GRAPHS, encodeBase64(JSON.stringify([])));
56-
}
57-
const raw = localStorageGet(ALL_GRAPHS);
58-
if (!raw) return new Set();
59-
const parsed = parseStoredJson(raw);
60-
if (!Array.isArray(parsed)) {
61-
localStorageSet(ALL_GRAPHS, encodeBase64(JSON.stringify([])));
62-
return new Set();
63-
}
64-
return new Set(parsed);
65-
};
66-
6753
const localStorageManager = {
6854
ALL_GRAPHS: window.btoa('ALL_GRAPHS'),
6955
AUTHOR_NAME: window.btoa('AUTHOR_NAME'),
7056

71-
allgs: getSet(window.btoa('ALL_GRAPHS')),
72-
73-
saveAllgs() {
74-
localStorageSet(this.ALL_GRAPHS, encodeBase64(JSON.stringify(Array.from(this.allgs))));
75-
},
76-
77-
addEmptyIfNot() {
78-
if (!localStorageGet(this.ALL_GRAPHS)) {
79-
localStorageSet(this.ALL_GRAPHS, encodeBase64(JSON.stringify([])));
80-
}
81-
},
82-
8357
get(id) {
8458
const raw = localStorageGet(id);
8559
if (raw === null) return null;
@@ -100,13 +74,15 @@ const localStorageManager = {
10074
}
10175
},
10276
remove(id) {
103-
if (this.allgs.delete(id)) this.saveAllgs();
77+
const list = this.getAllGraphs().filter((g) => g !== id);
78+
localStorageSet(this.ALL_GRAPHS, encodeBase64(JSON.stringify(list)));
10479
localStorageRemove(id);
10580
},
10681
addGraph(id) {
107-
if (this.allgs.has(id)) return;
108-
this.allgs.add(id);
109-
this.saveAllgs();
82+
const list = this.getAllGraphs();
83+
if (list.includes(id)) return;
84+
list.push(id);
85+
localStorageSet(this.ALL_GRAPHS, encodeBase64(JSON.stringify(list)));
11086
},
11187
getAllGraphs() {
11288
const raw = localStorageGet(this.ALL_GRAPHS);
@@ -119,17 +95,10 @@ const localStorageManager = {
11995
return parsed;
12096
},
12197
addToFront(id) {
122-
if (this.allgs.has(id)) return;
123-
this.allgs.add(id);
124-
const raw = localStorageGet(this.ALL_GRAPHS);
125-
if (!raw) return;
126-
const Garr = parseStoredJson(raw);
127-
if (!Array.isArray(Garr)) {
128-
this.saveAllgs();
129-
return;
130-
}
131-
Garr.unshift(id);
132-
localStorageSet(this.ALL_GRAPHS, encodeBase64(JSON.stringify(Garr)));
98+
const list = this.getAllGraphs();
99+
if (list.includes(id)) return;
100+
list.unshift(id);
101+
localStorageSet(this.ALL_GRAPHS, encodeBase64(JSON.stringify(list)));
133102
},
134103
getAuthorName() {
135104
return localStorageGet(this.AUTHOR_NAME) || '';

src/reducer/reducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ const reducer = (state, action) => {
109109
};
110110
}
111111
case T.ADD_GRAPH_BULK: {
112-
return { ...state, graphs: [...state.graphs, ...action.payload] };
112+
const newGraphs = action.payload.map((g) => ({ ...initialGraphState, ...g }));
113+
return { ...state, graphs: [...state.graphs, ...newGraphs], curGraphIndex: 0 };
113114
}
114115
case T.SET_CUR_INSTANCE: {
115116
return { ...state, curGraphInstance: action.payload };

0 commit comments

Comments
 (0)