Add loader while waiting for an emulator to start

Reviewed By: lblasa

Differential Revision: D50367884

fbshipit-source-id: cdc7c408bf2eb4ea3f7f4f03c27ca72e8bbe2947
This commit is contained in:
Andrey Goncharov
2023-10-17 09:27:30 -07:00
committed by Facebook GitHub Bot
parent cc76a21d80
commit 7040e56185

View File

@@ -32,6 +32,7 @@ import SettingsSheet from '../../chrome/SettingsSheet';
import {Link} from '../../ui'; import {Link} from '../../ui';
import {chain, uniq, without} from 'lodash'; import {chain, uniq, without} from 'lodash';
import {ReactNode} from 'react-markdown'; import {ReactNode} from 'react-markdown';
import {produce} from 'immer';
const COLD_BOOT = 'cold-boot'; const COLD_BOOT = 'cold-boot';
@@ -108,6 +109,8 @@ export const LaunchEmulatorDialog = withTrackingScope(
setFavoriteVirtualDevices(without(favoriteVirtualDevices, deviceName)); setFavoriteVirtualDevices(without(favoriteVirtualDevices, deviceName));
}; };
const [pendingEmulators, setPendingEmulators] = useState(new Set<string>());
useEffect(() => { useEffect(() => {
const getiOSSimulators = async () => { const getiOSSimulators = async () => {
if (!iosEnabled) { if (!iosEnabled) {
@@ -172,14 +175,29 @@ export const LaunchEmulatorDialog = withTrackingScope(
) )
.sortBy((item) => [!item.isFavorite, item.name]) .sortBy((item) => [!item.isFavorite, item.name])
.map(({name, isFavorite}) => { .map(({name, isFavorite}) => {
const launch = (coldBoot: boolean) => { const launch = async (coldBoot: boolean) => {
getRenderHostInstance() try {
.flipperServer.exec('android-launch-emulator', name, coldBoot) setPendingEmulators(
.then(onClose) produce((draft) => {
.catch((e) => { draft.add(name);
console.error('Failed to start emulator: ', e); }),
message.error('Failed to start emulator: ' + e); );
}); await getRenderHostInstance().flipperServer.exec(
'android-launch-emulator',
name,
coldBoot,
);
onClose();
} catch (e) {
console.error('Failed to start emulator: ', e);
message.error('Failed to start emulator: ' + e);
} finally {
setPendingEmulators(
produce((draft) => {
draft.delete(name);
}),
);
}
}; };
const menu = ( const menu = (
<Menu <Menu
@@ -206,6 +224,7 @@ export const LaunchEmulatorDialog = withTrackingScope(
<Dropdown.Button <Dropdown.Button
overlay={menu} overlay={menu}
icon={<MoreOutlined />} icon={<MoreOutlined />}
loading={pendingEmulators.has(name)}
onClick={() => launch(false)}> onClick={() => launch(false)}>
{name} {name}
</Dropdown.Button> </Dropdown.Button>
@@ -237,15 +256,30 @@ export const LaunchEmulatorDialog = withTrackingScope(
type="default" type="default"
key={device.udid} key={device.udid}
style={{width: '100%'}} style={{width: '100%'}}
onClick={() => loading={pendingEmulators.has(device.udid)}
getRenderHostInstance() onClick={async () => {
.flipperServer.exec('ios-launch-simulator', device.udid) try {
.catch((e) => { setPendingEmulators(
console.warn('Failed to start simulator: ', e); produce((draft) => {
message.error('Failed to start simulator: ' + e); draft.add(device.udid);
}) }),
.then(onClose) );
}> await getRenderHostInstance().flipperServer.exec(
'ios-launch-simulator',
device.udid,
);
onClose();
} catch (e) {
console.warn('Failed to start simulator: ', e);
message.error('Failed to start simulator: ' + e);
} finally {
setPendingEmulators(
produce((draft) => {
draft.delete(device.udid);
}),
);
}
}}>
{device.name} {device.name}
{device.osVersion ? ` (${device.osVersion})` : ''} {device.osVersion ? ` (${device.osVersion})` : ''}
</Button> </Button>