Skip to content

Commit f863883

Browse files
author
zurgl
committed
fix localstorage bug
1 parent 099a60c commit f863883

File tree

5 files changed

+84
-37
lines changed

5 files changed

+84
-37
lines changed

components/shared/Layout/index.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import styled from 'styled-components';
33
import {Row, Col} from 'antd';
44

55
import {GlobalContext, globalStateReducer, initGlobalState} from 'context';
6-
import {ChainType, MarkdownForChainIdT, GlobalStateT} from 'types';
6+
import {ChainType, MarkdownForChainIdT, LocalStorageStateT} from 'types';
77
import {FOOTER_HEIGHT, GRID_LAYOUT, HEADER_HEIGHT} from 'lib/constants';
8-
import {isOneColumnStep, getChainId, mergeState} from 'utils/context';
8+
import {
9+
isOneColumnStep,
10+
getChainId,
11+
prepareGlobalState,
12+
prepareGlobalStateForStorage,
13+
} from 'utils/context';
914
import {useLocalStorage} from 'hooks';
1015

1116
import Sidebar from './Sidebar';
@@ -20,12 +25,17 @@ type LayoutPropT = {
2025
};
2126

2227
const Layout = ({children, chain, markdown}: LayoutPropT) => {
23-
const [storage, setStorage] = useLocalStorage<GlobalStateT>('figment');
24-
const newState = mergeState(chain.id, storage, initGlobalState);
25-
const [state, dispatch] = useReducer(globalStateReducer, newState);
28+
const [storageState, setStorageState] =
29+
useLocalStorage<LocalStorageStateT>('figment');
30+
const newGlobalState = prepareGlobalState(
31+
storageState,
32+
initGlobalState,
33+
chain.id,
34+
);
35+
const [state, dispatch] = useReducer(globalStateReducer, newGlobalState);
2636

2737
useEffect(() => {
28-
setStorage(state);
38+
setStorageState(prepareGlobalStateForStorage(state));
2939
}, [state, dispatch]);
3040

3141
const isStepOneColumn = isOneColumnStep(state);

context/index.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,7 @@ export const buildInitialState = (): ProtocolsStateT => {
8484
{} as ProtocolsStateT,
8585
);
8686
};
87-
/*
88-
export const initGlobalState = (currentChainId: CHAINS) => {
89-
return {
90-
currentChainId,
91-
protocols: buildInitialState(),
92-
};
93-
};
94-
*/
87+
9588
export const initGlobalState = {
9689
currentChainId: undefined,
9790
protocols: buildInitialState(),

hooks/useLocalStorage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const useLocalStorage = <StateT>(key: string, initialValue?: StateT) => {
66
const item = window.localStorage.getItem(key);
77
return item ? JSON.parse(item) : initialValue;
88
} catch (error) {
9-
return initialValue;
9+
return {};
1010
}
1111
});
1212

types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ export type LocalStorageStateT = {
256256

257257
export type LocalStorageProtocolStateT = {
258258
currentStepId: PROTOCOL_STEPS_ID;
259+
steps: {
260+
[Key in PROTOCOL_STEPS_ID]: {
261+
isCompleted: boolean;
262+
};
263+
};
259264
innerState?: InnerStateT;
260265
};
261266

utils/context.ts

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {defaultsDeep, difference, keys, omit} from 'lodash';
2-
31
import {
42
GlobalStateT,
53
CHAINS,
@@ -8,8 +6,69 @@ import {
86
NETWORKS,
97
ProtocolStepsT,
108
ProtocolStepT,
9+
LocalStorageStateT,
1110
} from 'types';
1211

12+
export const prepareGlobalStateForStorage = (
13+
globalState: GlobalStateT,
14+
): LocalStorageStateT => {
15+
const chains = Object.keys(globalState.protocols) as CHAINS[];
16+
17+
return chains.reduce((acc: LocalStorageStateT, el: CHAINS) => {
18+
const stepsId = Object.keys(
19+
globalState.protocols[el].steps,
20+
) as PROTOCOL_STEPS_ID[];
21+
const newSteps = stepsId.reduce((acc0, stepId) => {
22+
// @ts-ignore
23+
acc0[stepId] = {
24+
isCompleted: globalState.protocols[el].steps[stepId].isCompleted,
25+
};
26+
return acc0;
27+
}, {});
28+
acc[el] = {
29+
currentStepId: globalState.protocols[el].currentStepId,
30+
// @ts-ignore
31+
steps: newSteps,
32+
innerState: globalState.protocols[el].innerState,
33+
};
34+
return acc;
35+
}, {} as LocalStorageStateT);
36+
};
37+
38+
export const prepareGlobalState = (
39+
localStorage: LocalStorageStateT,
40+
initialGlobalState: GlobalStateT,
41+
chainId: CHAINS,
42+
): GlobalStateT => {
43+
const chains = Object.keys(initialGlobalState.protocols) as CHAINS[];
44+
const newProtocols = chains.reduce(
45+
(protocols: GlobalStateT['protocols'], chain: CHAINS) => {
46+
const stepsId = Object.keys(
47+
protocols[chain].steps,
48+
) as PROTOCOL_STEPS_ID[];
49+
const newSteps = stepsId.reduce((steps, stepId) => {
50+
steps[stepId] = {
51+
...steps[stepId],
52+
...(localStorage ? localStorage[chain].steps[stepId] : {}),
53+
};
54+
return steps;
55+
}, protocols[chain].steps);
56+
protocols[chain] = {
57+
...initialGlobalState.protocols[chain],
58+
...(localStorage ? localStorage[chain] : {}),
59+
steps: newSteps,
60+
};
61+
return protocols;
62+
},
63+
initialGlobalState.protocols,
64+
);
65+
66+
return {
67+
currentChainId: chainId,
68+
protocols: newProtocols,
69+
};
70+
};
71+
1372
// Global State function, upmost level
1473
export const getChainId = (state: GlobalStateT): CHAINS => {
1574
return state.currentChainId as CHAINS;
@@ -230,23 +289,3 @@ export const getInnerState = (state: GlobalStateT) => {
230289

231290
return {network, ...innerState};
232291
};
233-
234-
export const mergeState = (
235-
chainId: CHAINS,
236-
storedState: GlobalStateT,
237-
initialState: GlobalStateT,
238-
): GlobalStateT => {
239-
const storageProtocols = storedState ? {...storedState.protocols} : {};
240-
const initProtocols = {...initialState.protocols};
241-
const removedKeys = difference(keys(storageProtocols), keys(initProtocols));
242-
243-
return storedState
244-
? ({
245-
currentChainId: chainId,
246-
protocols: omit<any>(
247-
defaultsDeep(storageProtocols, initProtocols),
248-
removedKeys,
249-
),
250-
} as GlobalStateT)
251-
: initialState;
252-
};

0 commit comments

Comments
 (0)