Skip to content

Commit dc2254c

Browse files
committed
refactor: introduce a common flag for Bluetooth OS unpairing
1 parent 4ecccf5 commit dc2254c

28 files changed

+94
-86
lines changed

packages/suite/src/actions/bluetooth/__tests__/desktopBluetoothReducer.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const bluetoothReducer = bluetoothSlice.prepareReducer(extraDependenciesMock);
2323

2424
const initialState: DesktopBluetoothState = {
2525
...prepareInitialState(),
26-
unpairedDeviceNeedsManualOsRemoval: false,
2726
connectingDeviceIds: [],
2827
isUnpairingDevice: false,
2928
};

packages/suite/src/actions/bluetooth/bluetoothConnectDeviceThunk.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { desktopApi } from '@trezor/suite-desktop-api';
66
import { bluetoothIpc } from '@trezor/transport-bluetooth';
77

88
import {
9-
setBluetoothDeviceNeedsManualOsRemoval,
109
startConnectingBluetoothDevice,
1110
stopConnectingBluetoothDevice,
1211
} from './desktopBluetoothReducer';
@@ -40,7 +39,7 @@ export const bluetoothConnectDeviceThunk = createThunk<
4039
result.error.includes('Operation already in progress') ||
4140
result.error.includes('Peer removed pairing information');
4241
if (isUnpaired) {
43-
dispatch(setBluetoothDeviceNeedsManualOsRemoval({ needsManualRemoval: true }));
42+
dispatch(bluetoothActions.setIsDeviceOsUnpairingRequired(true));
4443
dispatch(bluetoothActions.removeKnownDeviceAction({ id: deviceId }));
4544
} else {
4645
dispatch(

packages/suite/src/actions/bluetooth/bluetoothEraseBondsThunk.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { BLUETOOTH_PREFIX, bluetoothActions } from '@suite-common/bluetooth';
1+
import {
2+
BLUETOOTH_PREFIX,
3+
ForgetBluetoothDeviceThunkParams,
4+
bluetoothActions,
5+
} from '@suite-common/bluetooth';
26
import { createThunk } from '@suite-common/redux-utils';
37
import { notificationsActions } from '@suite-common/toast-notifications';
48
import {
@@ -10,16 +14,7 @@ import TrezorConnect, { BluetoothDeviceId } from '@trezor/connect';
1014
import { bluetoothIpc } from '@trezor/transport-bluetooth';
1115

1216
import { bluetoothDisconnectDeviceThunk } from './bluetoothDisconnectDeviceThunk';
13-
import {
14-
setBluetoothDeviceNeedsManualOsRemoval,
15-
setIsUnpairingDevice,
16-
} from './desktopBluetoothReducer';
17-
18-
type ForgetBluetoothDeviceThunkParams = {
19-
// This thunk must rely on `bluetoothId` directly. When this thunk is called,
20-
// the device may already be disconnected, and therefore, it cannot be selected from the state.
21-
bluetoothId: BluetoothDeviceId;
22-
};
17+
import { setIsUnpairingDevice } from './desktopBluetoothReducer';
2318

2419
export const forgetBluetoothDeviceThunk = createThunk<void, ForgetBluetoothDeviceThunkParams, void>(
2520
`${BLUETOOTH_PREFIX}/forgetBluetoothDevice`,
@@ -29,7 +24,7 @@ export const forgetBluetoothDeviceThunk = createThunk<void, ForgetBluetoothDevic
2924
const resultForget = await bluetoothIpc.forgetDevice(bluetoothId);
3025
dispatch(setIsUnpairingDevice({ isUnpairing: false }));
3126
if (!resultForget.success) {
32-
dispatch(setBluetoothDeviceNeedsManualOsRemoval({ needsManualRemoval: true }));
27+
dispatch(bluetoothActions.setIsDeviceOsUnpairingRequired(true));
3328
}
3429
dispatch(bluetoothActions.removeKnownDeviceAction({ id: bluetoothId }));
3530
},

packages/suite/src/actions/bluetooth/desktopBluetoothReducer.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import { deviceActions } from '@suite-common/wallet-core';
99
import { DesktopBluetoothDevice } from './DesktopBluetoothDevice';
1010

1111
export type DesktopBluetoothState = BluetoothState<DesktopBluetoothDevice> & {
12-
// Flag to display some extra info (Modal) to instruct the user to remove
13-
// the device from the OS settings manually
14-
unpairedDeviceNeedsManualOsRemoval: boolean;
15-
1612
// When we get an update that KnownDevice appeared, we start auto-connecting to it.
1713
// But there may be other updates before the connection is done, and we want to skip them
1814
// during the connection process.
@@ -36,14 +32,10 @@ export const bluetoothSlice = createSliceWithExtraDeps({
3632
name: 'bluetooth',
3733
initialState: {
3834
...prepareInitialState<DesktopBluetoothDevice>(),
39-
unpairedDeviceNeedsManualOsRemoval: false,
4035
connectingDeviceIds: [] as string[],
4136
isUnpairingDevice: false,
4237
} satisfies DesktopBluetoothState,
4338
reducers: {
44-
setBluetoothDeviceNeedsManualOsRemoval: (state, { payload: { needsManualRemoval } }) => {
45-
state.unpairedDeviceNeedsManualOsRemoval = needsManualRemoval;
46-
},
4739
startConnectingBluetoothDevice: (state, { payload: { deviceId } }) => {
4840
state.connectingDeviceIds.push(deviceId);
4941
},
@@ -79,7 +71,6 @@ export const bluetoothSlice = createSliceWithExtraDeps({
7971
});
8072

8173
export const {
82-
setBluetoothDeviceNeedsManualOsRemoval,
8374
startConnectingBluetoothDevice,
8475
stopConnectingBluetoothDevice,
8576
setIsUnpairingDevice,

packages/suite/src/actions/bluetooth/desktopBluetoothSelectors.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ import { WithBluetoothRootState } from './desktopBluetoothReducer';
33
export const selectConnectingDevices = (state: WithBluetoothRootState) =>
44
state.bluetooth.connectingDeviceIds;
55

6-
export const selectUnpairedDeviceNeedsManualOsRemoval = (state: WithBluetoothRootState) =>
7-
state.bluetooth.unpairedDeviceNeedsManualOsRemoval;
8-
96
export const selectIsUnpairingDevice = (state: WithBluetoothRootState) =>
107
state.bluetooth.isUnpairingDevice;

packages/suite/src/components/connection/ConnectDeviceGlobalModal.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { useTheme } from 'styled-components';
22

3-
import { selectAdapterStatus } from '@suite-common/bluetooth';
3+
import { selectAdapterStatus, selectIsDeviceOsUnpairingRequired } from '@suite-common/bluetooth';
44
import { Box, Button, Column, Modal, Row, Spinner, Text } from '@trezor/components';
55
import { borders } from '@trezor/theme';
66

7-
import {
8-
selectIsUnpairingDevice,
9-
selectUnpairedDeviceNeedsManualOsRemoval,
10-
} from 'src/actions/bluetooth/desktopBluetoothSelectors';
7+
import { selectIsUnpairingDevice } from 'src/actions/bluetooth/desktopBluetoothSelectors';
118
import { Translation } from 'src/components/suite/Translation';
129
import { useSelector } from 'src/hooks/suite';
1310
import { selectSuiteFlags } from 'src/selectors/suite/suiteSelectors';
@@ -79,7 +76,7 @@ export const ConnectDeviceGlobalModal = ({ onCancel }: { onCancel: () => void })
7976
selectedDevice,
8077
} = useConnectionGlobalModalContext();
8178

82-
const wasBluetoothDeviceWiped = useSelector(selectUnpairedDeviceNeedsManualOsRemoval);
79+
const wasBluetoothDeviceWiped = useSelector(selectIsDeviceOsUnpairingRequired);
8380
const isUnpairingDevice = useSelector(selectIsUnpairingDevice);
8481

8582
const { isBluetoothEnabled } = useSelector(selectSuiteFlags);

packages/suite/src/components/suite/bluetooth/UnpairBluetoothDeviceFromOsModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useState } from 'react';
22

3+
import { bluetoothActions } from '@suite-common/bluetooth';
34
import { Banner, Column, H3, Modal, Paragraph, Spinner } from '@trezor/components';
45
import { spacings } from '@trezor/theme';
56

67
import { openSystemSettingsThunk } from 'src/actions/bluetooth/openSystemSettingsThunk';
78
import { toggleConnectionModal } from 'src/actions/device/deviceSlice';
89
import { Translation } from 'src/components/suite/Translation';
910

10-
import { setBluetoothDeviceNeedsManualOsRemoval } from '../../../actions/bluetooth/desktopBluetoothReducer';
1111
import { selectIsUnpairingDevice } from '../../../actions/bluetooth/desktopBluetoothSelectors';
1212
import { useDispatch, useSelector } from '../../../hooks/suite';
1313

@@ -32,7 +32,7 @@ export const UnpairBluetoothDeviceFromOsModal = ({
3232
};
3333

3434
const onCancel = () => {
35-
dispatch(setBluetoothDeviceNeedsManualOsRemoval({ needsManualRemoval: false }));
35+
dispatch(bluetoothActions.setIsDeviceOsUnpairingRequired(false));
3636
dispatch(toggleConnectionModal());
3737
onFinish?.();
3838
};

packages/suite/src/components/suite/bluetooth/WipedBleDeviceNeedsManualOsRemovalModalManager.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { selectIsDeviceOsUnpairingRequired } from '@suite-common/bluetooth';
2+
13
import { UnpairBluetoothDeviceFromOsModal } from './UnpairBluetoothDeviceFromOsModal';
2-
import { selectUnpairedDeviceNeedsManualOsRemoval } from '../../../actions/bluetooth/desktopBluetoothSelectors';
34
import { useSelector } from '../../../hooks/suite';
45

56
export const WipedBleDeviceNeedsManualOsRemovalModalManager = () => {
6-
const wasBluetoothDeviceWiped = useSelector(selectUnpairedDeviceNeedsManualOsRemoval);
7+
const wasBluetoothDeviceWiped = useSelector(selectIsDeviceOsUnpairingRequired);
78

89
if (!wasBluetoothDeviceWiped) {
910
return null;

packages/suite/src/support/tests/__fixtures__/defaultAppState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const initialAppState: AppState = {
1818
suite: suiteInitialState,
1919
device: initialState,
2020
bluetooth: {
21-
unpairedDeviceNeedsManualOsRemoval: false,
2221
connectingDeviceIds: [],
2322
isUnpairingDevice: false,
2423
adapterStatus: 'unknown',
@@ -27,6 +26,7 @@ export const initialAppState: AppState = {
2726
knownDevices: [],
2827
ignoredDeviceIds: [],
2928
autoConnectPolicy: {},
29+
isDeviceOsUnpairingRequired: false,
3030
},
3131
thp: {
3232
step: null,

suite-common/bluetooth/src/bluetoothActions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ const setAutoConnectPolicyAction = createAction(
7878
}),
7979
);
8080

81+
const setIsDeviceOsUnpairingRequired = createAction(
82+
`${BLUETOOTH_PREFIX}/set-is-device-os-unpairing-required`,
83+
(isDeviceOsUnpairingRequired: boolean) => ({ payload: isDeviceOsUnpairingRequired }),
84+
);
85+
8186
export const bluetoothActions = {
8287
adapterEventAction,
8388
nearbyDevicesUpdateAction,
@@ -87,4 +92,5 @@ export const bluetoothActions = {
8792
removeKnownDeviceAction,
8893
updateDeviceConnectionStatus,
8994
setAutoConnectPolicyAction,
95+
setIsDeviceOsUnpairingRequired,
9096
};

0 commit comments

Comments
 (0)