Summary: The redux store keeps a list of devices. For the active device, it stored the index in that list. This diff now stores a reference to the active device instead of the index in that array. This changes makes it easier to get the reference to the active device in a component. Reviewed By: jknoxville Differential Revision: D8767514 fbshipit-source-id: c740cf98d6039223ce8d5a47bcd277989fe70bc3
101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
/**
|
|
* 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 'sonar';
|
|
import {connect} from 'react-redux';
|
|
import {exec} from 'child_process';
|
|
import {selectDevice} 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,
|
|
};
|
|
|
|
class DevicesButton extends Component<Props> {
|
|
launchEmulator = (name: string) => {
|
|
exec(`$ANDROID_HOME/tools/emulator @${name}`, error => {
|
|
if (error) {
|
|
console.error(error);
|
|
}
|
|
});
|
|
};
|
|
|
|
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},
|
|
)(DevicesButton);
|