-
Notifications
You must be signed in to change notification settings - Fork 47
/
SetupWalletProvider.tsx
92 lines (86 loc) · 3.16 KB
/
SetupWalletProvider.tsx
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
import * as React from 'react'
import {
SetupWalletActions,
SetupWalletActionType,
SetupWalletContext,
setupWalletDefaultState,
setupWalletInitialContext,
setupWalletReducer,
SetupWalletState,
} from '../state/state'
export const SetupWalletCtx = React.createContext<SetupWalletContext>(
setupWalletInitialContext,
)
export const SetupWalletProvider = ({
children,
initialState,
}: {
children: React.ReactNode
initialState?: Partial<SetupWalletState>
}) => {
const [state, dispatch] = React.useReducer(setupWalletReducer, {
...setupWalletDefaultState,
...initialState,
})
const actions = React.useRef<SetupWalletActions>({
mnemonicChanged: (mnemonic: SetupWalletState['mnemonic']) =>
dispatch({type: SetupWalletActionType.MnemonicChanged, mnemonic}),
walletNameChanged: (walletName: SetupWalletState['walletName']) =>
dispatch({type: SetupWalletActionType.WalletNameChanged, walletName}),
walletPasswordChanged: (
walletPassword: SetupWalletState['walletPassword'],
) =>
dispatch({
type: SetupWalletActionType.WalletPasswordChanged,
walletPassword,
}),
networkIdChanged: (networkId: SetupWalletState['networkId']) =>
dispatch({type: SetupWalletActionType.NetworkIdChanged, networkId}),
walletImplementationIdChanged: (
walletImplementationId: SetupWalletState['walletImplementationId'],
) =>
dispatch({
type: SetupWalletActionType.WalletImplementationIdChanged,
walletImplementationId,
}),
publicKeyHexChanged: (publicKeyHex: SetupWalletState['publicKeyHex']) =>
dispatch({type: SetupWalletActionType.PublicKeyHexChanged, publicKeyHex}),
pathChanged: (path: SetupWalletState['path']) =>
dispatch({type: SetupWalletActionType.PathChanged, path}),
hwDeviceInfoChanged: (hwDeviceInfo: SetupWalletState['hwDeviceInfo']) =>
dispatch({type: SetupWalletActionType.HwDeviceInfoChanged, hwDeviceInfo}),
setUpTypeChanged: (setUpType: SetupWalletState['setUpType']) =>
dispatch({type: SetupWalletActionType.SetUpTypeChanged, setUpType}),
mnemonicTypeChanged: (mnemonicType: SetupWalletState['mnemonicType']) =>
dispatch({type: SetupWalletActionType.MnemonicTypeChanged, mnemonicType}),
useUSBChanged: (useUSB: SetupWalletState['useUSB']) =>
dispatch({type: SetupWalletActionType.UseUSBChanged, useUSB}),
reset: () => dispatch({type: SetupWalletActionType.Reset}),
showRestoreWalletInfoModalChanged: (
showRestoreWalletInfoModal: SetupWalletState['showRestoreWalletInfoModal'],
) =>
dispatch({
type: SetupWalletActionType.ShowRestoreWalletInfoModalChanged,
showRestoreWalletInfoModal,
}),
showCreateWalletInfoModalChanged: (
showCreateWalletInfoModal: SetupWalletState['showCreateWalletInfoModal'],
) =>
dispatch({
type: SetupWalletActionType.ShowCreateWalletInfoModalChanged,
showCreateWalletInfoModal,
}),
}).current
const context = React.useMemo(
() => ({
...state,
...actions,
}),
[state, actions],
)
return (
<SetupWalletCtx.Provider value={context}>
{children}
</SetupWalletCtx.Provider>
)
}