Skip to content

Commit

Permalink
feat: WIP bluetooth
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Nov 21, 2024
1 parent ee6fdb9 commit 9837d4d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
TROUBLESHOOTING_TIP_DIFFERENT_COMPUTER,
TROUBLESHOOTING_TIP_UDEV,
} from 'src/components/suite/troubleshooting/tips';
import { Button } from '@trezor/components';
import { openModal } from '../../../actions/suite/modalActions';
import { useDispatch } from '../../../hooks/suite';

interface DeviceConnectProps {
isWebUsbTransport: boolean;
isBluetooth: boolean;
}

export const DeviceConnect = ({ isWebUsbTransport }: DeviceConnectProps) => {
export const DeviceConnect = ({ isWebUsbTransport, isBluetooth }: DeviceConnectProps) => {
const items = isWebUsbTransport
? [
TROUBLESHOOTING_TIP_UDEV,
Expand All @@ -28,11 +32,29 @@ export const DeviceConnect = ({ isWebUsbTransport }: DeviceConnectProps) => {
TROUBLESHOOTING_TIP_DIFFERENT_COMPUTER,
];

const dispatch = useDispatch();

return (
<TroubleshootingTips
label={<Translation id="TR_STILL_DONT_SEE_YOUR_TREZOR" />}
items={items}
cta={isWebUsbTransport ? <WebUsbButton data-testid="@webusb-button" /> : undefined}
cta={
// eslint-disable-next-line no-nested-ternary
isBluetooth ? (
<Button
variant="tertiary"
size="tiny"
onClick={e => {
e.stopPropagation();
dispatch(openModal({ type: 'select-bluetooth-device' }));
}}
>
Connect Safe 7 via bluetooth
</Button>
) : isWebUsbTransport ? (
<WebUsbButton data-testid="@webusb-button" />
) : undefined
}
data-testid="@connect-device-prompt/no-device-detected"
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Button, motionEasing } from '@trezor/components';
import { selectDevices, selectDevice } from '@suite-common/wallet-core';

import { ConnectDevicePrompt, Translation } from 'src/components/suite';
import { isWebUsb } from 'src/utils/suite/transport';
import { isBluetoothTransport, isWebUsb } from 'src/utils/suite/transport';
import { useDispatch, useSelector } from 'src/hooks/suite';
import { selectPrerequisite } from 'src/reducers/suite/suiteReducer';
import { goto } from 'src/actions/suite/routerActions';
Expand Down Expand Up @@ -56,6 +56,7 @@ export const PrerequisitesGuide = ({ allowSwitchDevice }: PrerequisitesGuideProp
const prerequisite = useSelector(selectPrerequisite);

const isWebUsbTransport = isWebUsb(transport);
const isBluetooth = isBluetoothTransport(transport);

const TipComponent = useMemo(
() => (): React.JSX.Element => {
Expand All @@ -65,7 +66,12 @@ export const PrerequisitesGuide = ({ allowSwitchDevice }: PrerequisitesGuideProp
case 'device-disconnect-required':
return <DeviceDisconnectRequired />;
case 'device-disconnected':
return <DeviceConnect isWebUsbTransport={isWebUsbTransport} />;
return (
<DeviceConnect
isWebUsbTransport={isWebUsbTransport}
isBluetooth={isBluetooth}
/>
);
case 'device-unacquired':
return <DeviceAcquire />;
case 'device-unreadable':
Expand Down
37 changes: 14 additions & 23 deletions packages/suite/src/components/suite/WebUsbButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type TrezorConnectWeb from '@trezor/connect-web';
import { ButtonProps, Button, IconButton, Tooltip } from '@trezor/components';

import { Translation, TranslationKey } from './Translation';
import { openModal } from 'src/actions/suite/modalActions';
import { useDispatch } from 'src/hooks/suite';

interface WebUsbButtonProps extends Omit<ButtonProps, 'children' | 'icon'> {
translationId?: TranslationKey;
Expand All @@ -17,31 +15,24 @@ const handleClick = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
};

export const WebUsbButton = ({
// translationId = 'TR_CHECK_FOR_DEVICES',
translationId = 'TR_CHECK_FOR_DEVICES',
icon = 'search',
size = 'tiny',
variant = 'primary',
...rest
}: WebUsbButtonProps) => {
const dispatch = useDispatch();

return (
<div data-testid="web-usb-button">
<Button
{...rest}
icon={icon === false ? undefined : icon}
size={size}
variant={variant}
onClick={e => {
e.stopPropagation();
dispatch(openModal({ type: 'select-bluetooth-device' }));
}}
>
Find bluetooth device
</Button>
</div>
);
};
}: WebUsbButtonProps) => (
<div data-testid="web-usb-button">
<Button
{...rest}
icon={icon === false ? undefined : icon}
size={size}
variant={variant}
onClick={handleClick}
>
<Translation id={translationId} />
</Button>
</div>
);

export const WebUsbIconButton = ({
translationId = 'TR_CHECK_FOR_DEVICES',
Expand Down
3 changes: 3 additions & 0 deletions packages/suite/src/utils/suite/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ import { AppState } from 'src/types/suite';

export const isWebUsb = (transport?: AppState['suite']['transport']) =>
!!(transport && transport.type && transport.type === 'WebUsbTransport');

export const isBluetoothTransport = (transport?: AppState['suite']['transport']) =>
!!(transport && transport.type && transport.type === 'BluetoothTransport');
2 changes: 1 addition & 1 deletion packages/transport-bluetooth/src/client/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BluetoothApi } from './bluetooth-api';

// Reflection of @trezor/transport/src/transports
export class BluetoothTransport extends AbstractApiTransport {
public name = 'WebUsbTransport' as const;
public name = 'BluetoothTransport' as const;
private wsApi: BluetoothApi;
// private session: SessionsClient;

Expand Down
3 changes: 2 additions & 1 deletion packages/transport/src/transports/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export abstract class AbstractTransport extends TransportEmitter {
| 'BridgeTransport'
| 'NodeUsbTransport'
| 'WebUsbTransport'
| 'UdpTransport';
| 'UdpTransport'
| 'BluetoothTransport';
/**
* transports with "external element" such as bridge can be outdated.
*/
Expand Down

0 comments on commit 9837d4d

Please sign in to comment.