Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/component/fileBrowser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/* eslint-disable react/state-in-constructor */
import React, { useEffect, useState } from 'react';
import FileBrowser, { FileRenderers, FolderRenderers } from 'react-keyed-file-browser';
import { toast } from 'react-toastify';
import { readFile, readTextFile } from '../toolbarActions/toolbarFunctions';
import { actionType as T } from '../reducer';
import ConfirmModal from './modals/ConfirmModal';
Expand Down Expand Up @@ -33,7 +34,11 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
// const allFiles = window.localStorage.getItem('fileList');
// setFileState({ files: allFiles });
// }
window.localStorage.setItem('fileList', JSON.stringify(fileState));
try {
window.localStorage.setItem('fileList', JSON.stringify(fileState));
} catch (e) {
toast.error(e.message);
}
}, [fileState]);

useEffect(() => {
Expand Down Expand Up @@ -235,7 +240,11 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
}));

setFileState(filesArray);
window.localStorage.setItem('fileList', JSON.stringify(filesArray));
try {
window.localStorage.setItem('fileList', JSON.stringify(filesArray));
} catch (e) {
toast.error(e.message);
}
if (filesArray.length > 0) {
dispatcher({ type: T.SET_DIR_NAME, payload: filesArray[0].key.split('/')[0] });
dispatcher({ type: T.SET_FILE_STATE, payload: filesArray });
Expand Down
68 changes: 51 additions & 17 deletions src/graph-builder/local-storage-manager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
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 (!window.localStorage.getItem(ALL_GRAPHS)) {
window.localStorage.setItem(ALL_GRAPHS, window.btoa(JSON.stringify([])));
if (!localStorageGet(ALL_GRAPHS)) {
localStorageSet(ALL_GRAPHS, window.btoa(JSON.stringify([])));
}
return new Set(JSON.parse(window.atob(window.localStorage.getItem(ALL_GRAPHS))));
const raw = localStorageGet(ALL_GRAPHS);
if (!raw) return new Set();
return new Set(JSON.parse(window.atob(raw)));
};

const localStorageManager = {
Expand All @@ -12,54 +41,59 @@ const localStorageManager = {
allgs: getSet(window.btoa('ALL_GRAPHS')),

saveAllgs() {
window.localStorage.setItem(this.ALL_GRAPHS, window.btoa(JSON.stringify(Array.from(this.allgs))));
localStorageSet(this.ALL_GRAPHS, window.btoa(JSON.stringify(Array.from(this.allgs))));
},

addEmptyIfNot() {
if (!window.localStorage.getItem(this.ALL_GRAPHS)) {
window.localStorage.setItem(this.ALL_GRAPHS, window.btoa(JSON.stringify([])));
if (!localStorageGet(this.ALL_GRAPHS)) {
localStorageSet(this.ALL_GRAPHS, window.btoa(JSON.stringify([])));
}
},

get(id) {
if (window.localStorage.getItem(id) === null) return null;
return JSON.parse(window.atob(window.localStorage.getItem(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);
window.localStorage.setItem(id, window.btoa(serializedJson));
localStorageSet(id, window.btoa(serializedJson));
},
remove(id) {
if (this.allgs.delete(id)) this.saveAllgs();
localStorage.removeItem(id);
localStorageRemove(id);
},
addGraph(id) {
if (this.allgs.has(id)) return;
this.allgs.add(id);
this.saveAllgs();
},
getAllGraphs() {
return JSON.parse(window.atob(window.localStorage.getItem(this.ALL_GRAPHS)));
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 Garr = JSON.parse(window.atob(window.localStorage.getItem(this.ALL_GRAPHS)));
const raw = localStorageGet(this.ALL_GRAPHS);
if (!raw) return;
const Garr = JSON.parse(window.atob(raw));
Garr.unshift(id);
window.localStorage.setItem(this.ALL_GRAPHS, window.btoa(JSON.stringify(Garr)));
localStorageSet(this.ALL_GRAPHS, window.btoa(JSON.stringify(Garr)));
},
getAuthorName() {
return localStorage.getItem(this.AUTHOR_NAME) || '';
return localStorageGet(this.AUTHOR_NAME) || '';
},
setAuthorName(authorName) {
localStorage.setItem(this.AUTHOR_NAME, authorName);
localStorageSet(this.AUTHOR_NAME, authorName);
},
clearGraph(id) {
window.localStorage.removeItem(id);
localStorageRemove(id);
},
getFileList() {
return localStorage.getItem('fileList') || '';
return localStorageGet('fileList') || '';
},
};
export default localStorageManager;