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;