mirror of https://github.com/trezor/trezor-wallet
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.
161 lines
5.3 KiB
161 lines
5.3 KiB
6 years ago
|
/* @flow */
|
||
6 years ago
|
import React, { Component } from 'react';
|
||
6 years ago
|
import styled from 'styled-components';
|
||
6 years ago
|
import TrezorConnect from 'trezor-connect';
|
||
6 years ago
|
import type { TrezorDevice } from 'flowtype';
|
||
6 years ago
|
import DeviceHeader from 'components/DeviceHeader';
|
||
|
import Button from 'components/Button';
|
||
|
import { isWebUSB } from 'utils/device';
|
||
6 years ago
|
import MenuItems from './components/MenuItems';
|
||
6 years ago
|
import DeviceList from './components/DeviceList';
|
||
6 years ago
|
|
||
6 years ago
|
import type { Props } from '../common';
|
||
6 years ago
|
|
||
6 years ago
|
import AsideDivider from '../Divider';
|
||
6 years ago
|
|
||
6 years ago
|
const Wrapper = styled.div``;
|
||
6 years ago
|
const ButtonWrapper = styled.div``;
|
||
6 years ago
|
|
||
6 years ago
|
export const DeviceSelect = (props: Props) => {
|
||
6 years ago
|
const handleOpen = () => {
|
||
|
props.toggleDeviceDropdown(!props.deviceDropdownOpened);
|
||
|
};
|
||
|
|
||
6 years ago
|
return (
|
||
6 years ago
|
<DeviceHeader
|
||
6 years ago
|
onClickWrapper={handleOpen}
|
||
6 years ago
|
device={props.wallet.selectedDevice}
|
||
|
transport={props.connect.transport}
|
||
6 years ago
|
devices={props.devices}
|
||
6 years ago
|
isOpen={props.deviceDropdownOpened}
|
||
|
/>
|
||
6 years ago
|
);
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
6 years ago
|
type DeviceMenuItem = {
|
||
|
type: string;
|
||
|
label: string;
|
||
|
}
|
||
|
|
||
|
export class DeviceDropdown extends Component<Props> {
|
||
|
mouseDownHandler: (event: MouseEvent) => void;
|
||
6 years ago
|
|
||
6 years ago
|
blurHandler: (event: FocusEvent) => void;
|
||
6 years ago
|
|
||
6 years ago
|
constructor(props: Props) {
|
||
6 years ago
|
super(props);
|
||
|
this.mouseDownHandler = this.mouseDownHandler.bind(this);
|
||
|
this.blurHandler = this.blurHandler.bind(this);
|
||
|
}
|
||
|
|
||
|
componentDidUpdate() {
|
||
6 years ago
|
const { transport } = this.props.connect;
|
||
6 years ago
|
if (isWebUSB(transport)) TrezorConnect.renderWebUSBButton();
|
||
6 years ago
|
}
|
||
|
|
||
|
mouseDownHandler(event: MouseEvent): void {
|
||
6 years ago
|
let elem: any = (event.target: any);
|
||
6 years ago
|
let block: boolean = false;
|
||
|
while (elem.parentElement) {
|
||
6 years ago
|
if (elem.tagName.toLowerCase() === 'aside' || (elem.className && elem.className.indexOf && elem.className.indexOf('modal-container') >= 0)) {
|
||
6 years ago
|
block = true;
|
||
|
break;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
elem = elem.parentElement;
|
||
|
}
|
||
|
|
||
|
if (!block) {
|
||
|
this.props.toggleDeviceDropdown(false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
blurHandler(event: FocusEvent): void {
|
||
|
this.props.toggleDeviceDropdown(false);
|
||
|
}
|
||
|
|
||
|
componentDidMount(): void {
|
||
|
window.addEventListener('mousedown', this.mouseDownHandler, false);
|
||
|
// window.addEventListener('blur', this.blurHandler, false);
|
||
6 years ago
|
const { transport } = this.props.connect;
|
||
6 years ago
|
if (transport && transport.version.indexOf('webusb') >= 0) TrezorConnect.renderWebUSBButton();
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
onDeviceMenuClick(item: DeviceMenuItem, device: TrezorDevice): void {
|
||
|
if (item.type === 'reload') {
|
||
|
this.props.acquireDevice();
|
||
|
} else if (item.type === 'forget') {
|
||
|
this.props.forgetDevice(device);
|
||
|
} else if (item.type === 'clone') {
|
||
|
this.props.duplicateDevice(device);
|
||
|
} else if (item.type === 'settings') {
|
||
|
this.props.toggleDeviceDropdown(false);
|
||
|
this.props.gotoDeviceSettings(device);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
componentWillUnmount(): void {
|
||
|
window.removeEventListener('mousedown', this.mouseDownHandler, false);
|
||
|
// window.removeEventListener('blur', this.blurHandler, false);
|
||
|
}
|
||
|
|
||
6 years ago
|
showDivider() {
|
||
|
return this.props.devices.length > 1;
|
||
|
}
|
||
|
|
||
6 years ago
|
render() {
|
||
6 years ago
|
const { devices, onSelectDevice } = this.props;
|
||
6 years ago
|
const { transport } = this.props.connect;
|
||
6 years ago
|
const { selectedDevice } = this.props.wallet;
|
||
6 years ago
|
|
||
6 years ago
|
let currentDeviceMenu = null;
|
||
6 years ago
|
if (selectedDevice.features) {
|
||
6 years ago
|
const deviceMenuItems: Array<DeviceMenuItem> = [];
|
||
6 years ago
|
|
||
6 years ago
|
if (selectedDevice.status !== 'available') {
|
||
6 years ago
|
deviceMenuItems.push({ type: 'reload', label: 'Renew session' });
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
deviceMenuItems.push({ type: 'settings', label: 'Device settings' });
|
||
6 years ago
|
if (selectedDevice.features.passphrase_protection && selectedDevice.connected && selectedDevice.available) {
|
||
6 years ago
|
deviceMenuItems.push({ type: 'clone', label: 'Create hidden wallet' });
|
||
6 years ago
|
}
|
||
6 years ago
|
//if (selected.remember) {
|
||
6 years ago
|
deviceMenuItems.push({ type: 'forget', label: 'Forget device' });
|
||
6 years ago
|
//}
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
const deviceMenuButtons = deviceMenuItems.map((item, index) => (
|
||
6 years ago
|
<div key={item.type} className={item.type} onClick={event => this.onDeviceMenuClick(item, selectedDevice)}>{item.label}</div>
|
||
6 years ago
|
));
|
||
6 years ago
|
currentDeviceMenu = deviceMenuButtons.length < 1 ? null : (
|
||
|
<div className="device-menu">
|
||
6 years ago
|
{deviceMenuButtons}
|
||
6 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
return (
|
||
6 years ago
|
<Wrapper>
|
||
6 years ago
|
{currentDeviceMenu}
|
||
|
{/* {selectedDevice && selectedDevice.features && <MenuItems {...this.props} />} */}
|
||
6 years ago
|
{this.showDivider() && <AsideDivider textLeft="Other devices" />}
|
||
|
<DeviceList
|
||
|
devices={devices}
|
||
|
selectedDevice={selectedDevice}
|
||
6 years ago
|
onSelectDevice={onSelectDevice}
|
||
6 years ago
|
/>
|
||
6 years ago
|
<ButtonWrapper>
|
||
|
{isWebUSB(transport) && (
|
||
|
<Button
|
||
|
className="trezor-webusb-button"
|
||
|
text="Check for devices"
|
||
|
isWebUsb
|
||
|
/>
|
||
|
)}
|
||
|
</ButtonWrapper>
|
||
6 years ago
|
</Wrapper>
|
||
6 years ago
|
);
|
||
|
}
|
||
6 years ago
|
}
|