1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-01-13 01:20:59 +00:00
trezor-wallet/src/reducers/ModalReducer.js

120 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2017-12-13 11:01:37 +00:00
import { UI, DEVICE } from 'trezor-connect';
2018-08-14 13:11:52 +00:00
import * as RECEIVE from 'actions/constants/receive';
import * as MODAL from 'actions/constants/modal';
import * as CONNECT from 'actions/constants/TrezorConnect';
2017-12-13 11:01:37 +00:00
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
2018-04-23 10:20:15 +00:00
2019-03-04 12:33:02 +00:00
export type State =
| {
context: typeof MODAL.CONTEXT_NONE,
}
| {
context: typeof MODAL.CONTEXT_DEVICE,
device: TrezorDevice,
instances?: Array<TrezorDevice>,
windowType?: string,
}
| {
context: typeof MODAL.CONTEXT_EXTERNAL_WALLET,
windowType?: string,
}
| {
context: typeof MODAL.CONTEXT_SCAN_QR,
}
| {
context: typeof MODAL.CONTEXT_CONFIRMATION,
windowType: string,
};
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
const initialState: State = {
context: MODAL.CONTEXT_NONE,
2017-12-13 11:01:37 +00:00
};
2018-04-23 10:20:15 +00:00
export default function modal(state: State = initialState, action: Action): State {
2017-12-13 11:01:37 +00:00
switch (action.type) {
2018-07-30 10:52:13 +00:00
case RECEIVE.REQUEST_UNVERIFIED:
case CONNECT.FORGET_REQUEST:
case CONNECT.TRY_TO_DUPLICATE:
2018-10-05 13:47:51 +00:00
case CONNECT.REQUEST_WALLET_TYPE:
return {
context: MODAL.CONTEXT_DEVICE,
2018-10-05 13:47:51 +00:00
device: action.device,
windowType: action.type,
};
2018-07-30 10:52:13 +00:00
case CONNECT.REMEMBER_REQUEST:
2018-04-23 10:20:15 +00:00
return {
context: MODAL.CONTEXT_DEVICE,
2018-04-23 10:20:15 +00:00
device: action.device,
instances: action.instances,
2018-07-30 10:52:13 +00:00
windowType: action.type,
2018-04-23 10:20:15 +00:00
};
2017-12-13 11:01:37 +00:00
2018-10-11 11:07:58 +00:00
// device connected
// close modal if modal context is not 'device'
case DEVICE.CONNECT:
case DEVICE.CONNECT_UNACQUIRED:
if (state.context !== MODAL.CONTEXT_DEVICE) {
return initialState;
}
return state;
// device with context assigned to modal was disconnected
// close modal
2018-07-30 10:52:13 +00:00
case DEVICE.DISCONNECT:
2019-03-04 12:33:02 +00:00
if (
state.context === MODAL.CONTEXT_DEVICE &&
action.device.path === state.device.path
) {
2018-07-30 10:52:13 +00:00
return initialState;
2017-12-13 11:01:37 +00:00
}
2018-02-20 09:30:36 +00:00
return state;
2017-12-13 11:01:37 +00:00
2018-07-30 10:52:13 +00:00
case UI.REQUEST_PIN:
case UI.INVALID_PIN:
case UI.REQUEST_PASSPHRASE:
2017-12-13 11:01:37 +00:00
return {
context: MODAL.CONTEXT_DEVICE,
2018-05-02 09:01:08 +00:00
device: action.payload.device,
2018-07-30 10:52:13 +00:00
windowType: action.type,
2018-02-20 09:30:36 +00:00
};
2018-07-30 10:52:13 +00:00
case UI.REQUEST_BUTTON:
2017-12-13 11:01:37 +00:00
return {
context: MODAL.CONTEXT_DEVICE,
2018-05-02 09:01:08 +00:00
device: action.payload.device,
2018-07-30 10:52:13 +00:00
windowType: action.payload.code,
};
case UI.CLOSE_UI_WINDOW:
case MODAL.CLOSE:
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.REMEMBER:
2018-05-02 09:01:08 +00:00
return initialState;
2018-02-20 09:30:36 +00:00
case MODAL.OPEN_EXTERNAL_WALLET:
return {
context: MODAL.CONTEXT_EXTERNAL_WALLET,
windowType: action.id,
};
2019-01-17 16:12:12 +00:00
case MODAL.OPEN_SCAN_QR:
return {
context: MODAL.CONTEXT_SCAN_QR,
};
case UI.REQUEST_CONFIRMATION:
return {
context: MODAL.CONTEXT_CONFIRMATION,
windowType: action.payload.view,
};
2017-12-13 11:01:37 +00:00
default:
return state;
}
2019-03-04 12:33:02 +00:00
}