-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💄 (smpl): Rm transport selection, allow to select devices with differ…
…ent ones
- Loading branch information
1 parent
974e35c
commit 95dacb9
Showing
9 changed files
with
198 additions
and
145 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
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
91 changes: 91 additions & 0 deletions
91
apps/sample/src/components/MainView/ConnectDeviceActions.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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Button, Flex } from "@ledgerhq/react-ui"; | ||
import { BuiltinTransports, SdkError } from "@ledgerhq/device-management-kit"; | ||
import React, { useCallback } from "react"; | ||
import { useSdkConfigContext } from "@/providers/SdkConfig"; | ||
import { useSdk } from "@/providers/DeviceSdkProvider"; | ||
import { useDeviceSessionsContext } from "@/providers/DeviceSessionsProvider"; | ||
|
||
type ConnectDeviceActionsProps = { | ||
onError: (error: SdkError | null) => void; | ||
}; | ||
|
||
export const ConnectDeviceActions = ({ | ||
onError, | ||
}: ConnectDeviceActionsProps) => { | ||
const { | ||
dispatch: dispatchSdkConfig, | ||
state: { transport }, | ||
} = useSdkConfigContext(); | ||
const { dispatch: dispatchDeviceSession } = useDeviceSessionsContext(); | ||
const sdk = useSdk(); | ||
|
||
const onSelectDeviceClicked = useCallback( | ||
(selectedTransport: BuiltinTransports) => { | ||
onError(null); | ||
dispatchSdkConfig({ | ||
type: "set_transport", | ||
payload: { transport: selectedTransport }, | ||
}); | ||
sdk.startDiscovering({ transport: selectedTransport }).subscribe({ | ||
next: (device) => { | ||
sdk | ||
.connect({ device }) | ||
.then((sessionId) => { | ||
console.log( | ||
`🦖 Response from connect: ${JSON.stringify(sessionId)} 🎉`, | ||
); | ||
dispatchDeviceSession({ | ||
type: "add_session", | ||
payload: { | ||
sessionId, | ||
connectedDevice: sdk.getConnectedDevice({ sessionId }), | ||
}, | ||
}); | ||
}) | ||
.catch((error) => { | ||
onError(error); | ||
console.error(`Error from connection or get-version`, error); | ||
}); | ||
}, | ||
error: (error) => { | ||
console.error(error); | ||
}, | ||
}); | ||
}, | ||
[sdk, transport], | ||
); | ||
|
||
return transport === BuiltinTransports.MOCK_SERVER ? ( | ||
<Button | ||
mx={3} | ||
onClick={() => onSelectDeviceClicked(BuiltinTransports.MOCK_SERVER)} | ||
variant="main" | ||
backgroundColor="main" | ||
size="large" | ||
data-testid="CTA_select-device" | ||
> | ||
Select a device | ||
</Button> | ||
) : ( | ||
<Flex> | ||
<Button | ||
mx={3} | ||
onClick={() => onSelectDeviceClicked(BuiltinTransports.USB)} | ||
variant="main" | ||
backgroundColor="main" | ||
size="large" | ||
> | ||
Select a USB device | ||
</Button> | ||
<Button | ||
mx={3} | ||
onClick={() => onSelectDeviceClicked(BuiltinTransports.BLE)} | ||
variant="main" | ||
backgroundColor="main" | ||
size="large" | ||
> | ||
Select a BLE device | ||
</Button> | ||
</Flex> | ||
); | ||
}; |
Oops, something went wrong.