-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.ts
301 lines (274 loc) · 8.71 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import {
GlobalStateT,
CHAINS,
PROTOCOL_STEPS_ID,
PROTOCOL_INNER_STATES_ID,
NETWORKS,
ProtocolStepsT,
ProtocolStepT,
LocalStorageStateT,
} from 'types';
export const prepareGlobalStateForStorage = (
globalState: GlobalStateT,
): LocalStorageStateT => {
const chains = Object.keys(globalState.protocols) as CHAINS[];
return chains.reduce((acc: LocalStorageStateT, el: CHAINS) => {
const stepsId = Object.keys(
globalState.protocols[el].steps,
) as PROTOCOL_STEPS_ID[];
const newSteps = stepsId.reduce((acc0, stepId) => {
// @ts-ignore
acc0[stepId] = {
isCompleted: globalState.protocols[el].steps[stepId].isCompleted,
};
return acc0;
}, {});
acc[el] = {
currentStepId: globalState.protocols[el].currentStepId,
// @ts-ignore
steps: newSteps,
innerState: globalState.protocols[el].innerState,
};
return acc;
}, {} as LocalStorageStateT);
};
export const prepareGlobalState = (
localStorage: LocalStorageStateT,
initialGlobalState: GlobalStateT,
chainId: CHAINS,
): GlobalStateT => {
const initialLocalStorageState =
prepareGlobalStateForStorage(initialGlobalState);
const chains = Object.keys(initialLocalStorageState) as CHAINS[];
const newProtocols = chains.reduce(
(protocols: GlobalStateT['protocols'], chain: CHAINS) => {
const stepsId = Object.keys(
protocols[chain].steps,
) as PROTOCOL_STEPS_ID[];
const newSteps = stepsId.reduce((steps, stepId) => {
let newStep;
try {
newStep = localStorage[chain]?.steps[stepId];
} catch (error) {
newStep = initialLocalStorageState[chain]?.steps[stepId];
}
steps[stepId] = {
...steps[stepId],
...newStep,
};
return steps;
}, protocols[chain].steps);
protocols[chain] = {
...initialGlobalState.protocols[chain],
...(localStorage
? localStorage[chain]
: initialLocalStorageState[chain]),
steps: newSteps,
};
return protocols;
},
initialGlobalState.protocols,
);
return {
currentChainId: chainId,
protocols: newProtocols,
};
};
// Global State function, upmost level
export const getChainId = (state: GlobalStateT): CHAINS => {
return state.currentChainId as CHAINS;
};
export const getChainLabel = (state: GlobalStateT): string => {
const chainId = getChainId(state);
return state.protocols[chainId].label;
};
export const getNetwork = (state: GlobalStateT): NETWORKS => {
const chainId = getChainId(state);
return state.protocols[chainId].network;
};
export const getSteps = (state: GlobalStateT): ProtocolStepsT => {
const chainId = getChainId(state);
return state.protocols[chainId].steps;
};
export const getStepId = (state: GlobalStateT): PROTOCOL_STEPS_ID => {
const chainId = getChainId(state);
return state.protocols[chainId].currentStepId;
};
// Current Step Id function
export const getFirstStepId = (state: GlobalStateT): PROTOCOL_STEPS_ID => {
const chainId = getChainId(state);
return state.protocols[chainId].firstStepId;
};
export const getLastStepId = (state: GlobalStateT): PROTOCOL_STEPS_ID => {
const chainId = getChainId(state);
return state.protocols[chainId].lastStepId;
};
export const getPreviousStepId = (state: GlobalStateT): PROTOCOL_STEPS_ID => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
const previousStepId =
state.protocols[chainId].steps[currentStepId].previousStepId;
return previousStepId ? previousStepId : getFirstStepId(state);
};
export const getNextStepId = (state: GlobalStateT): PROTOCOL_STEPS_ID => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
const nextStepId = state.protocols[chainId].steps[currentStepId].nextStepId;
return nextStepId ? nextStepId : getLastStepId(state);
};
export const getPreviousStep = (state: GlobalStateT): ProtocolStepT | null => {
const chainId = getChainId(state);
const previousStep = getPreviousStepId(state);
return previousStep === null
? null
: state.protocols[chainId].steps[previousStep];
};
export const getNextStep = (state: GlobalStateT): ProtocolStepT | null => {
const chainId = getChainId(state);
const nextStepId = getNextStepId(state);
return nextStepId === null
? null
: state.protocols[chainId].steps[nextStepId];
};
export const getStepIndex = (state: GlobalStateT): number => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
return state.protocols[chainId].steps[currentStepId].position;
};
export const getStepTitle = (state: GlobalStateT): string => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
return state.protocols[chainId].steps[currentStepId].title;
};
// Predicat's functions
export const isConnectionStep = (state: GlobalStateT): boolean => {
return getStepId(state) === PROTOCOL_STEPS_ID.CHAIN_CONNECTION;
};
export const isOneColumnStep = (state: GlobalStateT): boolean => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
return state.protocols[chainId].steps[currentStepId].isOneColumn;
};
export const isFirstStep = (state: GlobalStateT) => {
return getStepId(state) === getFirstStepId(state);
};
export const isLastStep = (state: GlobalStateT): boolean => {
return getStepId(state) === getLastStepId(state);
};
export const isCompletedStep = (state: GlobalStateT): boolean => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
return (
isOneColumnStep(state) ||
isSkippableStep(state) ||
state.protocols[chainId].steps[currentStepId].isCompleted
);
};
export const isSkippableStep = (state: GlobalStateT): boolean => {
const chainId = getChainId(state);
const currentStepId = getStepId(state);
return state.protocols[chainId].steps[currentStepId].isSkippable;
};
// Inner state functions
export const getChainInnerState = (
state: GlobalStateT,
stateId: PROTOCOL_INNER_STATES_ID,
): string | null => {
const chainId = getChainId(state);
return state.protocols[chainId].innerState?.[stateId] as string | null;
};
// This function is temporary
// When all the pathways will be migrate it will rewrite and
// it will take 5 line of code and it will be replace by something like this
// The issue here is link to the case of the enum ...
/*
export const getChainInnerState = (
state: GlobalStateT,
stateId: PROTOCOL_INNER_STATES_ID,
) => {
const chainId = getChainId(state);
return state.protocols[chainId].innerState?.[stateId] as string | null;
};
*/
export const getInnerState = (state: GlobalStateT) => {
const innerState: any = {};
const network = getNetwork(state);
const secret = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.SECRET);
if (secret) {
innerState.secret = secret;
}
const privateKey = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.PRIVATE_KEY,
);
if (privateKey) {
innerState.privateKey = privateKey;
}
const publicKey = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.PUBLIC_KEY,
);
if (publicKey) {
innerState.publicKey = publicKey;
}
const address = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.ADDRESS);
if (address) {
innerState.address = address;
}
const contractId = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.CONTRACT_ID,
);
if (contractId) {
innerState.contractId = contractId;
}
const mnemonic = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.MNEMONIC);
if (mnemonic) {
innerState.mnemonic = mnemonic;
}
const accountId = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.ACCOUNT_ID,
);
if (accountId) {
innerState.accountId = accountId;
}
const password = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.PASSWORD);
if (password) {
innerState.password = password;
}
const email = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.EMAIL);
if (email) {
innerState.email = email;
}
const programId = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.PROGRAM_ID,
);
if (programId) {
innerState.programId = programId;
}
const greeter = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.GREETER);
if (greeter) {
innerState.greeter = greeter;
}
const metamaskNetworkName = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.METAMASK_NETWORK_NAME,
);
if (metamaskNetworkName) {
innerState.metamaskNetworkName = metamaskNetworkName;
}
const did = getChainInnerState(state, PROTOCOL_INNER_STATES_ID.DID);
if (did) {
innerState.did = did;
}
const userName = getChainInnerState(
state,
PROTOCOL_INNER_STATES_ID.USER_NAME,
);
if (userName) {
innerState.userName = userName;
}
return {network, ...innerState};
};