1+ import { toast } from 'react-toastify' ;
2+
3+ const lsGet = ( key ) => {
4+ try {
5+ return window . localStorage . getItem ( key ) ;
6+ } catch ( e ) {
7+ toast . error ( e . message ) ;
8+ return null ;
9+ }
10+ } ;
11+
12+ const lsSet = ( key , value ) => {
13+ try {
14+ window . localStorage . setItem ( key , value ) ;
15+ } catch ( e ) {
16+ toast . error ( e . message ) ;
17+ }
18+ } ;
19+
20+ const lsRemove = ( key ) => {
21+ try {
22+ window . localStorage . removeItem ( key ) ;
23+ } catch ( e ) {
24+ toast . error ( e . message ) ;
25+ }
26+ } ;
27+
128const getSet = ( ALL_GRAPHS ) => {
2- if ( ! window . localStorage . getItem ( ALL_GRAPHS ) ) {
3- window . localStorage . setItem ( ALL_GRAPHS , window . btoa ( JSON . stringify ( [ ] ) ) ) ;
29+ if ( ! lsGet ( ALL_GRAPHS ) ) {
30+ lsSet ( ALL_GRAPHS , window . btoa ( JSON . stringify ( [ ] ) ) ) ;
431 }
5- return new Set ( JSON . parse ( window . atob ( window . localStorage . getItem ( ALL_GRAPHS ) ) ) ) ;
32+ const raw = lsGet ( ALL_GRAPHS ) ;
33+ if ( ! raw ) return new Set ( ) ;
34+ return new Set ( JSON . parse ( window . atob ( raw ) ) ) ;
635} ;
736
837const localStorageManager = {
@@ -12,54 +41,59 @@ const localStorageManager = {
1241 allgs : getSet ( window . btoa ( 'ALL_GRAPHS' ) ) ,
1342
1443 saveAllgs ( ) {
15- window . localStorage . setItem ( this . ALL_GRAPHS , window . btoa ( JSON . stringify ( Array . from ( this . allgs ) ) ) ) ;
44+ lsSet ( this . ALL_GRAPHS , window . btoa ( JSON . stringify ( Array . from ( this . allgs ) ) ) ) ;
1645 } ,
1746
1847 addEmptyIfNot ( ) {
19- if ( ! window . localStorage . getItem ( this . ALL_GRAPHS ) ) {
20- window . localStorage . setItem ( this . ALL_GRAPHS , window . btoa ( JSON . stringify ( [ ] ) ) ) ;
48+ if ( ! lsGet ( this . ALL_GRAPHS ) ) {
49+ lsSet ( this . ALL_GRAPHS , window . btoa ( JSON . stringify ( [ ] ) ) ) ;
2150 }
2251 } ,
2352
2453 get ( id ) {
25- if ( window . localStorage . getItem ( id ) === null ) return null ;
26- return JSON . parse ( window . atob ( window . localStorage . getItem ( id ) ) ) ;
54+ const raw = lsGet ( id ) ;
55+ if ( raw === null ) return null ;
56+ return JSON . parse ( window . atob ( raw ) ) ;
2757 } ,
2858 save ( id , graphContent ) {
2959 this . addGraph ( id ) ;
3060 const serializedJson = JSON . stringify ( graphContent ) ;
31- window . localStorage . setItem ( id , window . btoa ( serializedJson ) ) ;
61+ lsSet ( id , window . btoa ( serializedJson ) ) ;
3262 } ,
3363 remove ( id ) {
3464 if ( this . allgs . delete ( id ) ) this . saveAllgs ( ) ;
35- localStorage . removeItem ( id ) ;
65+ lsRemove ( id ) ;
3666 } ,
3767 addGraph ( id ) {
3868 if ( this . allgs . has ( id ) ) return ;
3969 this . allgs . add ( id ) ;
4070 this . saveAllgs ( ) ;
4171 } ,
4272 getAllGraphs ( ) {
43- return JSON . parse ( window . atob ( window . localStorage . getItem ( this . ALL_GRAPHS ) ) ) ;
73+ const raw = lsGet ( this . ALL_GRAPHS ) ;
74+ if ( ! raw ) return [ ] ;
75+ return JSON . parse ( window . atob ( raw ) ) ;
4476 } ,
4577 addToFront ( id ) {
4678 if ( this . allgs . has ( id ) ) return ;
4779 this . allgs . add ( id ) ;
48- const Garr = JSON . parse ( window . atob ( window . localStorage . getItem ( this . ALL_GRAPHS ) ) ) ;
80+ const raw = lsGet ( this . ALL_GRAPHS ) ;
81+ if ( ! raw ) return ;
82+ const Garr = JSON . parse ( window . atob ( raw ) ) ;
4983 Garr . unshift ( id ) ;
50- window . localStorage . setItem ( this . ALL_GRAPHS , window . btoa ( JSON . stringify ( Garr ) ) ) ;
84+ lsSet ( this . ALL_GRAPHS , window . btoa ( JSON . stringify ( Garr ) ) ) ;
5185 } ,
5286 getAuthorName ( ) {
53- return localStorage . getItem ( this . AUTHOR_NAME ) || '' ;
87+ return lsGet ( this . AUTHOR_NAME ) || '' ;
5488 } ,
5589 setAuthorName ( authorName ) {
56- localStorage . setItem ( this . AUTHOR_NAME , authorName ) ;
90+ lsSet ( this . AUTHOR_NAME , authorName ) ;
5791 } ,
5892 clearGraph ( id ) {
59- window . localStorage . removeItem ( id ) ;
93+ lsRemove ( id ) ;
6094 } ,
6195 getFileList ( ) {
62- return localStorage . getItem ( 'fileList' ) || '' ;
96+ return lsGet ( 'fileList' ) || '' ;
6397 } ,
6498} ;
6599export default localStorageManager ;
0 commit comments