diff --git a/src/actions/devices.js b/src/actions/devices.js index 79173a60..c5af4ad5 100644 --- a/src/actions/devices.js +++ b/src/actions/devices.js @@ -2,10 +2,6 @@ // eslint-disable import/prefer-default-export -import type { Dispatch } from 'redux' - -import { getDevices, getCurrentDevice } from 'reducers/devices' - import type { Device, Devices } from 'types/common' export type deviceChooseType = (Device | null) => { type: string, payload: Device | null } @@ -14,47 +10,20 @@ export const deviceChoose: deviceChooseType = payload => ({ payload, }) -type deviceChooseFirstType = () => (Dispatch, () => { devices: Devices }) => void -export const deviceChooseFirst: deviceChooseFirstType = () => (dispatch, getState) => { - const devices = getDevices(getState()) - - // If we detect only 1 device, we choose it - if (devices.length === 1) { - dispatch(deviceChoose(devices[0])) - } -} - -type devicesAddType = Device => (Dispatch) => void -export const deviceAdd: devicesAddType = payload => dispatch => { - dispatch({ - type: 'DEVICE_ADD', - payload, - }) - - dispatch(deviceChooseFirst()) -} - -type devicesRemoveType = Device => (Dispatch, () => { devices: Devices }) => void -export const deviceRemove: devicesRemoveType = payload => (dispatch, getState) => { - dispatch({ - type: 'DEVICE_REMOVE', - payload, - }) - - const currentDevice = getCurrentDevice(getState()) - - // If we disconnect the currentDevice we reset it - if (currentDevice.path === payload.path) { - dispatch(deviceChoose(null)) - } -} +type devicesAddType = Device => { type: string, payload: Device } +export const deviceAdd: devicesAddType = payload => ({ + type: 'DEVICE_ADD', + payload, +}) -type devicesUpdateType = Devices => (Dispatch) => void -export const devicesUpdate: devicesUpdateType = payload => dispatch => { - dispatch({ - type: 'DEVICES_UPDATE', - payload, - }) +type devicesRemoveType = Device => { type: string, payload: Device } +export const deviceRemove: devicesRemoveType = payload => ({ + type: 'DEVICE_REMOVE', + payload, +}) - dispatch(deviceChooseFirst()) -} +type devicesUpdateType = Devices => { type: string, payload: Devices } +export const devicesUpdate: devicesUpdateType = payload => ({ + type: 'DEVICES_UPDATE', + payload, +}) diff --git a/src/main/app.js b/src/main/app.js index 59226b37..3a8e84e8 100644 --- a/src/main/app.js +++ b/src/main/app.js @@ -35,7 +35,7 @@ function createMainWindow() { return window } -// dsq + // Quit application when all windows are closed app.on('window-all-closed', () => { // On macOS it is common for applications to stay open diff --git a/src/reducers/devices.js b/src/reducers/devices.js index bb3097be..d74c0c17 100644 --- a/src/reducers/devices.js +++ b/src/reducers/devices.js @@ -5,7 +5,7 @@ import { handleActions } from 'redux-actions' import type { Device, Devices } from 'types/common' type stateType = { - currentDevice: ?Device, + currentDevice: Device | null, devices: Devices, } const state = { @@ -13,19 +13,32 @@ const state = { devices: [], } -const handlers: Object = { - DEVICES_UPDATE: (state: stateType, { payload: devices }: { payload: Devices }) => ({ - ...state, - devices, - }), - DEVICE_ADD: (state: stateType, { payload: device }: { payload: Device }) => ({ +function setCurrentDevice(state) { + return { ...state, - devices: [...state.devices, device].filter( - (v, i, s) => s.findIndex(t => t.path === v.path) === i, - ), - }), + currentDevice: state.devices.length === 1 ? state.devices[0] : state.currentDevice, + } +} + +const handlers: Object = { + DEVICES_UPDATE: (state: stateType, { payload: devices }: { payload: Devices }) => + setCurrentDevice({ + ...state, + devices, + }), + DEVICE_ADD: (state: stateType, { payload: device }: { payload: Device }) => + setCurrentDevice({ + ...state, + devices: [...state.devices, device].filter( + (v, i, s) => s.findIndex(t => t.path === v.path) === i, + ), + }), DEVICE_REMOVE: (state: stateType, { payload: device }: { payload: Device }) => ({ ...state, + currentDevice: + state.currentDevice !== null && state.currentDevice.path === device.path + ? null + : state.currentDevice, devices: state.devices.filter(d => d.path !== device.path), }), DEVICE_CHOOSE: (state: stateType, { payload: currentDevice }: { payload: Device }) => ({