You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/ModalReducer.js

121 lines
3.5 KiB

7 years ago
/* @flow */
7 years ago
import { UI, DEVICE } from 'trezor-connect';
import * as RECEIVE from 'actions/constants/receive';
import * as MODAL from 'actions/constants/modal';
import * as CONNECT from 'actions/constants/TrezorConnect';
7 years ago
import type { Action, TrezorDevice } from 'flowtype';
6 years ago
5 years ago
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,
};
7 years ago
const initialState: State = {
context: MODAL.CONTEXT_NONE,
7 years ago
};
6 years ago
export default function modal(state: State = initialState, action: Action): State {
7 years ago
switch (action.type) {
case RECEIVE.REQUEST_UNVERIFIED:
case CONNECT.FORGET_REQUEST:
case CONNECT.TRY_TO_DUPLICATE:
case CONNECT.REQUEST_WALLET_TYPE:
return {
context: MODAL.CONTEXT_DEVICE,
device: action.device,
windowType: action.type,
};
case CONNECT.REMEMBER_REQUEST:
6 years ago
return {
context: MODAL.CONTEXT_DEVICE,
6 years ago
device: action.device,
instances: action.instances,
windowType: action.type,
6 years ago
};
7 years ago
// 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
case DEVICE.DISCONNECT:
5 years ago
if (
state.context === MODAL.CONTEXT_DEVICE &&
action.payload.path === state.device.path
5 years ago
) {
return initialState;
7 years ago
}
6 years ago
return state;
7 years ago
case UI.REQUEST_PIN:
case UI.INVALID_PIN:
case UI.REQUEST_PASSPHRASE:
case UI.REQUEST_PASSPHRASE_ON_DEVICE:
7 years ago
return {
context: MODAL.CONTEXT_DEVICE,
device: (action.payload.device: any), // should be TrezorDevice, but ath this point it doesn't matter, Device is enough
windowType: action.type,
6 years ago
};
case UI.REQUEST_BUTTON:
7 years ago
return {
context: MODAL.CONTEXT_DEVICE,
device: (action.payload.device: any), // should be TrezorDevice, but ath this point it doesn't matter, Device is enough
windowType: action.payload.code,
};
case UI.CLOSE_UI_WINDOW:
case MODAL.CLOSE:
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.REMEMBER:
6 years ago
return initialState;
6 years ago
case MODAL.OPEN_EXTERNAL_WALLET:
return {
context: MODAL.CONTEXT_EXTERNAL_WALLET,
windowType: action.id,
};
case MODAL.OPEN_SCAN_QR:
return {
context: MODAL.CONTEXT_SCAN_QR,
};
case UI.REQUEST_CONFIRMATION:
return {
context: MODAL.CONTEXT_CONFIRMATION,
windowType: action.payload.view,
};
7 years ago
default:
return state;
}
5 years ago
}