Skip to content

Commit

Permalink
fixup! feat: implement suite-common bluetooth code into Suite
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Feb 24, 2025
1 parent ef80901 commit 1a79397
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import { BLUETOOTH_PREFIX } from '@suite-common/bluetooth';
import { BLUETOOTH_PREFIX, bluetoothActions } from '@suite-common/bluetooth';
import { createThunk } from '@suite-common/redux-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { bluetoothIpc } from '@trezor/transport-bluetooth';

type ThunkResponse = ReturnType<typeof bluetoothIpc.connectDevice>;
type BluetoothConnectDeviceThunkResult = {
success: boolean;
};

export const bluetoothConnectDeviceThunk = createThunk<ThunkResponse, { id: string }, void>(
export const bluetoothConnectDeviceThunk = createThunk<
BluetoothConnectDeviceThunkResult,
{ id: string },
void
>(
`${BLUETOOTH_PREFIX}/bluetoothConnectDeviceThunk`,
async ({ id }, { fulfillWithValue }) => {
async ({ id }, { fulfillWithValue, dispatch }) => {
const result = await bluetoothIpc.connectDevice(id);

return fulfillWithValue(result);
if (!result.success) {
dispatch(
bluetoothActions.connectDeviceEventAction({
id,
connectionStatus: { type: 'error', error: result.error },
}),
);
dispatch(
notificationsActions.addToast({
type: 'error',
error: result.error,
}),
);
} else {
dispatch(
bluetoothActions.connectDeviceEventAction({
id,
connectionStatus: { type: 'connected' },
}),
);
}

return fulfillWithValue({ success: result.success });
},
);
24 changes: 3 additions & 21 deletions packages/suite/src/components/suite/bluetooth/BluetoothConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
selectKnownDevices,
selectScanStatus,
} from '@suite-common/bluetooth';
import { notificationsActions } from '@suite-common/toast-notifications';
import { Card, Column, ElevationUp } from '@trezor/components';
import { spacings } from '@trezor/theme';
import { BluetoothDevice } from '@trezor/transport-bluetooth';
Expand All @@ -24,6 +23,7 @@ import { BluetoothVersionNotCompatible } from './errors/BluetoothVersionNotCompa
import { bluetoothConnectDeviceThunk } from '../../../actions/bluetooth/bluetoothConnectDeviceThunk';
import { bluetoothStartScanningThunk } from '../../../actions/bluetooth/bluetoothStartScanningThunk';
import { bluetoothStopScanningThunk } from '../../../actions/bluetooth/bluetoothStopScanningThunk';
import { closeModalApp } from '../../../actions/suite/routerActions';
import { useDispatch, useSelector } from '../../../hooks/suite';

const SCAN_TIMEOUT = 30_000;
Expand Down Expand Up @@ -100,26 +100,8 @@ export const BluetoothConnect = ({ onClose, uiMode }: BluetoothConnectProps) =>
setSelectedDeviceId(id);
const result = await dispatch(bluetoothConnectDeviceThunk({ id })).unwrap();

if (!result.success) {
dispatch(
bluetoothActions.connectDeviceEventAction({
id,
connectionStatus: { type: 'error', error: result.error },
}),
);
dispatch(
notificationsActions.addToast({
type: 'error',
error: result.error,
}),
);
} else {
dispatch(
bluetoothActions.connectDeviceEventAction({
id,
connectionStatus: { type: 'connected' },
}),
);
if (uiMode === 'card' && result.success) {
dispatch(closeModalApp());
}
};

Expand Down

0 comments on commit 1a79397

Please sign in to comment.