Skip to content

Commit

Permalink
feat(sample): display sdk version in sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Feb 1, 2024
1 parent 402eb4e commit bb7c4ad
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@ledgerhq/device-sdk-core": "workspace:*",
"@ledgerhq/react-ui": "^0.14.13",
"next": "14.0.4",
"react": "^18",
Expand Down
20 changes: 19 additions & 1 deletion apps/sample/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import React, { useEffect, useState } from "react";
import { Box, Flex, Icons, Text } from "@ledgerhq/react-ui";
import styled, { DefaultTheme } from "styled-components";
import { useSdk } from "@/providers/DeviceSdkProvider";
import { Menu } from "../Menu";
import { Device } from "../Device";

Expand Down Expand Up @@ -40,10 +41,27 @@ const VersionText = styled(Text)`
`;

export const Sidebar: React.FC = () => {
const [version, setVersion] = useState("");
const sdk = useSdk();

useEffect(() => {
sdk
.getVersion()
.then((v) => setVersion(v))
.catch((error: unknown) => {
console.error(error as Error);
setVersion("");
});
}, [sdk]);

return (
<Root>
<Title variant={"large"}>Ledger Device SDK</Title>

{version ? (
<Subtitle variant={"small"}>SDK Version: {version}</Subtitle>
) : null}

<Subtitle variant={"tiny"}>Device</Subtitle>
<Device />

Expand Down
18 changes: 18 additions & 0 deletions apps/sample/src/providers/DeviceSdkProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createContext, useContext } from "react";
import { DeviceSdkBuilder, DeviceSdk } from "@ledgerhq/device-sdk-core";

export const sdk = new DeviceSdkBuilder().build();

const SdkContext = createContext<DeviceSdk>(sdk);

type Props = {
children: React.ReactNode;
};

export const SdkProvider: React.FC<Props> = ({ children }) => {
return <SdkContext.Provider value={sdk}>{children}</SdkContext.Provider>;
};

export const useSdk = () => {
return useContext(SdkContext);
};
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src";
8 changes: 5 additions & 3 deletions packages/core/src/api/DeviceSdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Container } from "inversify";
import { GetSdkVersionUseCase } from "@internal/config/usecase/GetSdkVersionUseCase";
// import { GetSdkVersionUseCase } from "@internal/config/usecase/GetSdkVersionUseCase";
import { types as ConfigTypes } from "@internal/config/di/configTypes";
import { MakeContainerProps, makeContainer } from "../di";
import { GetSdkVersionUseCase } from "@internal/config/usecase/GetSdkVersionUseCase";

export class DeviceSdk {
container: Container;
Expand All @@ -20,9 +22,9 @@ export class DeviceSdk {
return;
}

getVersion() {
getVersion(): Promise<string> {
return this.container
.get<GetSdkVersionUseCase>("GetSdkVersionUseCase")
.get<GetSdkVersionUseCase>(ConfigTypes.GetSdkVersionUseCase)
.getSdkVersion();
}
}
4 changes: 1 addition & 3 deletions packages/core/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

export { LedgerDeviceSdkBuilder as DeviceSdkBuilder } from "./DeviceSdkBuilder";

// NOTE: not sure we want to export directly the DeviceSdk as we are using the builder pattern
// export { DeviceSdk } from "./DeviceSdk";
export { DeviceSdk } from "./DeviceSdk";
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"@internal/*": ["src/internal/*"]
}
},
"include": ["src", "jest.*.ts"]
"include": ["src", "index.ts", "jest.*.ts"]
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb7c4ad

Please sign in to comment.