-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: progress on the UI, implement new structure of the bluetooth re…
…dux state and implement connect/disconnect
- Loading branch information
1 parent
937dae2
commit 9a68d09
Showing
12 changed files
with
247 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/suite/src/actions/bluetooth/bluetoothDisconnectDeviceThunk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { BLUETOOTH_PREFIX } 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 BluetoothDisconnectDeviceThunkResult = { | ||
success: boolean; | ||
}; | ||
|
||
export const bluetoothDisconnectDeviceThunk = createThunk< | ||
BluetoothDisconnectDeviceThunkResult, | ||
{ id: string }, | ||
void | ||
>( | ||
`${BLUETOOTH_PREFIX}/bluetoothConnectDeviceThunk`, | ||
async ({ id }, { fulfillWithValue, dispatch }) => { | ||
const result = await bluetoothIpc.disconnectDevice(id); | ||
|
||
if (!result.success) { | ||
dispatch( | ||
notificationsActions.addToast({ | ||
type: 'error', | ||
error: result.error, | ||
}), | ||
); | ||
} | ||
|
||
return fulfillWithValue({ success: result.success }); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 80 additions & 17 deletions
97
packages/suite/src/components/suite/bluetooth/BluetoothDeviceItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,88 @@ | ||
import { useState } from 'react'; | ||
|
||
import { DeviceBluetoothConnectionStatusType } from '@suite-common/bluetooth'; | ||
import { Button, Row } from '@trezor/components'; | ||
import { spacings } from '@trezor/theme'; | ||
import { BluetoothDevice as BluetoothDeviceType } from '@trezor/transport-bluetooth'; | ||
import { BluetoothDevice } from '@trezor/transport-bluetooth'; | ||
|
||
import { BluetoothDeviceComponent } from './BluetoothDeviceComponent'; | ||
import { bluetoothConnectDeviceThunk } from '../../../actions/bluetooth/bluetoothConnectDeviceThunk'; | ||
import { bluetoothDisconnectDeviceThunk } from '../../../actions/bluetooth/bluetoothDisconnectDeviceThunk'; | ||
import { closeModalApp } from '../../../actions/suite/routerActions'; | ||
import { useDispatch } from '../../../hooks/suite'; | ||
|
||
const labelMap: Record<DeviceBluetoothConnectionStatusType, string> = { | ||
disconnected: 'Connect', | ||
connecting: 'Connecting', | ||
connected: 'Disconnect', | ||
'connection-error': 'Try again', // Out-of-range, offline, in the faraday cage, ... | ||
pairing: 'Pairing', | ||
paired: 'Paired', | ||
'pairing-error': '', // shall never be show to user | ||
}; | ||
|
||
const LOADING_STATUSES: DeviceBluetoothConnectionStatusType[] = ['pairing', 'connecting']; | ||
const DISABLED_STATUSES: DeviceBluetoothConnectionStatusType[] = ['pairing', 'connecting']; | ||
|
||
type BluetoothDeviceItemProps = { | ||
device: BluetoothDeviceType; | ||
onClick: () => void; | ||
isDisabled?: boolean; | ||
device: BluetoothDevice; | ||
onSelect: (id: string) => void; | ||
uiMode: 'spatial' | 'card'; | ||
}; | ||
|
||
export const BluetoothDeviceItem = ({ device, onClick, isDisabled }: BluetoothDeviceItemProps) => ( | ||
<Row onClick={onClick} gap={spacings.md} alignItems="stretch"> | ||
<BluetoothDeviceComponent device={device} flex="1" /> | ||
<Button | ||
variant="primary" | ||
size="small" | ||
margin={{ vertical: spacings.xxs }} | ||
isDisabled={isDisabled} | ||
> | ||
Connect | ||
</Button> | ||
</Row> | ||
); | ||
export const BluetoothDeviceItem = ({ device, onSelect, uiMode }: BluetoothDeviceItemProps) => { | ||
const dispatch = useDispatch(); | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const isDisabled = DISABLED_STATUSES.includes(device.connectionStatus.type); | ||
const isGlobalLoading = LOADING_STATUSES.includes(device.connectionStatus.type); | ||
|
||
const onConnect = async () => { | ||
onSelect(device.id); | ||
const result = await dispatch(bluetoothConnectDeviceThunk({ id: device.id })).unwrap(); | ||
|
||
if (uiMode === 'card' && result.success) { | ||
dispatch(closeModalApp()); | ||
} | ||
}; | ||
|
||
const onDisconnect = async () => { | ||
await dispatch(bluetoothDisconnectDeviceThunk({ id: device.id })).unwrap(); | ||
}; | ||
|
||
const onClickMap: Record< | ||
DeviceBluetoothConnectionStatusType, | ||
(() => Promise<void>) | undefined | ||
> = { | ||
'connection-error': onConnect, | ||
'pairing-error': undefined, | ||
connected: onDisconnect, | ||
connecting: undefined, | ||
disconnected: onConnect, | ||
paired: undefined, | ||
pairing: undefined, | ||
}; | ||
|
||
const handleOnClick = async () => { | ||
setIsLoading(true); | ||
await onClickMap[device.connectionStatus.type]?.(); | ||
setIsLoading(false); | ||
}; | ||
|
||
return ( | ||
<Row gap={spacings.md} alignItems="stretch"> | ||
<BluetoothDeviceComponent device={device} flex="1" /> | ||
<Button | ||
variant="primary" | ||
size="small" | ||
margin={{ vertical: spacings.xxs }} | ||
isDisabled={isDisabled} | ||
isLoading={isLoading || isGlobalLoading} | ||
onClick={handleOnClick} | ||
> | ||
{labelMap[device.connectionStatus.type]} | ||
</Button> | ||
</Row> | ||
); | ||
}; |
Oops, something went wrong.