From b5f41dfa10c8b2ef532704e8553f4ce433c51e14 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Mon, 22 Apr 2024 05:59:30 -0700 Subject: [PATCH] Unregister device Summary: Add a new reducer to allow device unregistration. It works as follow: If the device is disconnected, it will remove the device from the devices list. If there are more devices, the first device will become selected. If there are running apps, the first app will be selected. Reviewed By: antonk52 Differential Revision: D55795279 fbshipit-source-id: de90cddf386a9ad3fe0c955b3696c91036decc79 --- .../flipper-ui/src/reducers/connections.tsx | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/desktop/flipper-ui/src/reducers/connections.tsx b/desktop/flipper-ui/src/reducers/connections.tsx index b3d4816e1f5..23b0904b5b3 100644 --- a/desktop/flipper-ui/src/reducers/connections.tsx +++ b/desktop/flipper-ui/src/reducers/connections.tsx @@ -91,6 +91,10 @@ export type Action = type: 'REGISTER_DEVICE'; payload: BaseDevice; } + | { + type: 'UNREGISTER_DEVICE'; + payload: BaseDevice; + } | { type: 'SELECT_DEVICE'; payload: BaseDevice; @@ -259,6 +263,40 @@ export default (state: State = INITAL_STATE, action: Actions): State => { }; } + case 'UNREGISTER_DEVICE': { + const {payload} = action; + + const newDevices = state.devices.slice(); + const existing = state.devices.findIndex( + (device) => device.serial === payload.serial, + ); + if (existing !== -1) { + const d = newDevices[existing]; + if (d.connected.get()) { + throw new Error( + `Cannot unregister, '${d.serial}' as is still connected`, + ); + } + newDevices.splice(existing, 1); + } + + let selectedNewDevice: BaseDevice | null = null; + let selectedNewAppId: null | string = null; + if (newDevices.length > 0) { + selectedNewDevice = newDevices[0]; + selectedNewAppId = + getAllClients(state).find((c) => c.device === selectedNewDevice) + ?.id ?? null; + } + + return { + ...state, + devices: newDevices, + selectedDevice: selectedNewDevice, + selectedAppId: selectedNewAppId, + }; + } + case 'SELECT_PLUGIN': { const {selectedPlugin, selectedAppId, deepLinkPayload} = action.payload;