-
Notifications
You must be signed in to change notification settings - Fork 947
/
DevicesButton.js
104 lines (96 loc) · 2.61 KB
/
DevicesButton.js
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
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {Component, Button} from 'flipper';
import {connect} from 'react-redux';
import {spawn} from 'child_process';
import {selectDevice, preferDevice} from '../reducers/connections.js';
import type BaseDevice from '../devices/BaseDevice.js';
type Props = {
selectedDevice: ?BaseDevice,
androidEmulators: Array<string>,
devices: Array<BaseDevice>,
selectDevice: (device: BaseDevice) => void,
preferDevice: (device: string) => void,
};
class DevicesButton extends Component<Props> {
launchEmulator = (name: string) => {
const child = spawn('emulator', [`@${name}`], {
detached: true,
});
child.stderr.on('data', data => {
console.error(`Android emulator error: ${data}`);
});
child.on('error', console.error);
this.props.preferDevice(name);
};
render() {
const {
devices,
androidEmulators,
selectedDevice,
selectDevice,
} = this.props;
let text = 'No device selected';
let icon = 'minus-circle';
if (selectedDevice) {
text = selectedDevice.title;
icon = 'mobile';
}
const dropdown = [];
if (devices.length > 0) {
dropdown.push(
{
label: 'Running devices',
enabled: false,
},
...devices.map((device: BaseDevice) => ({
click: () => selectDevice(device),
checked: device === selectedDevice,
label: `${device.deviceType === 'physical' ? '📱 ' : ''}${
device.title
}`,
type: 'checkbox',
})),
);
}
if (androidEmulators.length > 0) {
const emulators = Array.from(androidEmulators)
.filter(
(name: string) =>
devices.findIndex((device: BaseDevice) => device.title === name) ===
-1,
)
.map((name: string) => ({
label: name,
click: () => this.launchEmulator(name),
}));
if (emulators.length > 0) {
dropdown.push(
{type: 'separator'},
{
label: 'Launch Android emulators',
enabled: false,
},
...emulators,
);
}
}
return (
<Button compact={true} icon={icon} dropdown={dropdown} disabled={false}>
{text}
</Button>
);
}
}
export default connect(
({connections: {devices, androidEmulators, selectedDevice}}) => ({
devices,
androidEmulators,
selectedDevice,
}),
{selectDevice, preferDevice},
)(DevicesButton);