@@ -13,15 +13,65 @@ const GraphComp = (props) => {
1313 const { dispatcher, superState } = props ;
1414
1515 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 } ) ) } ) ;
16+ let mounted = true ;
17+ const restoreWorkspace = async ( ) => {
18+ await localStorageManager . initialize ( ) ;
19+ if ( ! mounted ) return ;
20+
21+ const session = localStorageManager . getSession ( ) ;
22+ const allIDs = Array . isArray ( session ?. openGraphIDs ) && session . openGraphIDs . length
23+ ? session . openGraphIDs
24+ : localStorageManager . getAllGraphs ( ) ;
25+ const validIDs = allIDs . filter (
26+ ( id ) => typeof id === 'string' && id && localStorageManager . get ( id ) !== null ,
27+ ) ;
28+ if ( validIDs . length !== allIDs . length ) {
29+ allIDs . filter ( ( id ) => ! validIDs . includes ( id ) ) . forEach ( ( id ) => localStorageManager . remove ( id ) ) ;
30+ }
31+
32+ if ( Array . isArray ( session ?. fileState ) ) {
33+ dispatcher ( { type : T . SET_FILE_STATE , payload : session . fileState } ) ;
34+ }
35+ if ( typeof session ?. uploadedDirName === 'string' ) {
36+ dispatcher ( { type : T . SET_DIR_NAME , payload : session . uploadedDirName } ) ;
37+ }
38+
39+ if ( validIDs . length === 0 ) return ;
40+ dispatcher ( {
41+ type : T . ADD_GRAPH_BULK ,
42+ payload : {
43+ graphs : validIDs . map ( ( graphID ) => ( { graphID } ) ) ,
44+ activeGraphID : session ?. activeGraphID || null ,
45+ } ,
46+ } ) ;
47+ } ;
48+ restoreWorkspace ( ) ;
49+
50+ return ( ) => {
51+ mounted = false ;
52+ } ;
2353 } , [ ] ) ;
2454
55+ React . useEffect ( ( ) => {
56+ const openGraphIDs = superState . graphs
57+ . map ( ( graph ) => graph . graphID )
58+ . filter ( ( graphID ) => typeof graphID === 'string' && graphID ) ;
59+ const activeGraphID = superState . curGraphIndex >= 0 && superState . curGraphIndex < superState . graphs . length
60+ ? superState . graphs [ superState . curGraphIndex ] . graphID
61+ : null ;
62+ localStorageManager . saveSession ( {
63+ openGraphIDs,
64+ activeGraphID,
65+ fileState : superState . fileState ,
66+ uploadedDirName : superState . uploadedDirName ,
67+ } ) ;
68+ } , [
69+ superState . graphs ,
70+ superState . curGraphIndex ,
71+ superState . fileState ,
72+ superState . uploadedDirName ,
73+ ] ) ;
74+
2575 // Remote server implementation - Not being used.
2676 // useEffect(() => {
2777 // if (!loadedFromStorage) return;
0 commit comments