Skip to content

Commit

Permalink
🚚 (smpl): Rename Session to DeviceSession
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed May 15, 2024
1 parent a83a609 commit 2f05046
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 63 deletions.
6 changes: 3 additions & 3 deletions apps/sample/src/app/client-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import styled, { DefaultTheme } from "styled-components";
import { Header } from "@/components/Header";
import { Sidebar } from "@/components/Sidebar";
import { SdkProvider } from "@/providers/DeviceSdkProvider";
import { SessionProvider } from "@/providers/SessionsProvider";
import { DeviceSessionsProvider } from "@/providers/DeviceSessionsProvider";
import { GlobalStyle } from "@/styles/globalstyles";

type ClientRootLayoutProps = {
Expand Down Expand Up @@ -45,13 +45,13 @@ const ClientRootLayout: React.FC<ClientRootLayoutProps> = ({ children }) => {
<GlobalStyle />
<body>
<Root>
<SessionProvider>
<DeviceSessionsProvider>
<Sidebar />
<PageContainer>
<Header />
{children}
</PageContainer>
</SessionProvider>
</DeviceSessionsProvider>
</Root>
</body>
</StyleProvider>
Expand Down
4 changes: 2 additions & 2 deletions apps/sample/src/components/ApduView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import styled, { DefaultTheme } from "styled-components";

import { useApduForm } from "@/hooks/useApduForm";
import { useSdk } from "@/providers/DeviceSdkProvider";
import { useSessionContext } from "@/providers/SessionsProvider";
import { useDeviceSessionsContext } from "@/providers/DeviceSessionsProvider";

const Root = styled(Flex).attrs({ mx: 15, mt: 10, mb: 5 })`
flex-direction: column;
Expand Down Expand Up @@ -70,7 +70,7 @@ export const ApduView: React.FC = () => {
const sdk = useSdk();
const {
state: { selectedId: selectedSessionId },
} = useSessionContext();
} = useDeviceSessionsContext();
const onSubmit = useCallback(
async (values: typeof apduFormValues) => {
setLoading(true);
Expand Down
8 changes: 4 additions & 4 deletions apps/sample/src/components/Device/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from "react";
import {
ConnectionType,
DeviceModelId,
SessionId,
DeviceSessionId,
} from "@ledgerhq/device-sdk-core";
import { Box, DropdownGeneric, Flex, Icons, Text } from "@ledgerhq/react-ui";
import styled, { DefaultTheme } from "styled-components";

import { useSessionState } from "@/hooks/useSessionState";
import { useDeviceSessionState } from "@/hooks/useDeviceSessionState";

import { StatusText } from "./StatusText";

Expand Down Expand Up @@ -38,7 +38,7 @@ const ActionRow = styled(Flex).attrs({ py: 4, px: 2 })`
type DeviceProps = {
name: string;
type: ConnectionType;
sessionId: SessionId;
sessionId: DeviceSessionId;
model: DeviceModelId;
onDisconnect: () => Promise<void>;
};
Expand All @@ -50,7 +50,7 @@ export const Device: React.FC<DeviceProps> = ({
onDisconnect,
sessionId,
}) => {
const sessionState = useSessionState(sessionId);
const sessionState = useDeviceSessionState(sessionId);
return (
<Root>
<IconContainer>
Expand Down
4 changes: 2 additions & 2 deletions apps/sample/src/components/MainView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Image from "next/image";
import styled, { DefaultTheme } from "styled-components";

import { useSdk } from "@/providers/DeviceSdkProvider";
import { useSessionContext } from "@/providers/SessionsProvider";
import { useDeviceSessionsContext } from "@/providers/DeviceSessionsProvider";

const Root = styled(Flex)`
flex: 1;
Expand All @@ -23,7 +23,7 @@ const NanoLogo = styled(Image).attrs({ mb: 8 })`

export const MainView: React.FC = () => {
const sdk = useSdk();
const { dispatch } = useSessionContext();
const { dispatch } = useDeviceSessionsContext();

// Example starting the discovery on a user action
const onSelectDeviceClicked = useCallback(() => {
Expand Down
4 changes: 2 additions & 2 deletions apps/sample/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import styled, { DefaultTheme } from "styled-components";
import { Device } from "@/components/Device";
import { Menu } from "@/components/Menu";
import { useSdk } from "@/providers/DeviceSdkProvider";
import { useSessionContext } from "@/providers/SessionsProvider";
import { useDeviceSessionsContext } from "@/providers/DeviceSessionsProvider";

const Root = styled(Flex).attrs({ py: 8, px: 6 })`
flex-direction: column;
Expand Down Expand Up @@ -46,7 +46,7 @@ export const Sidebar: React.FC = () => {
const {
state: { deviceById },
dispatch,
} = useSessionContext();
} = useDeviceSessionsContext();

useEffect(() => {
sdk
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { useEffect, useState } from "react";
import { SessionDeviceState, SessionId } from "@ledgerhq/device-sdk-core";
import { DeviceSessionId, DeviceSessionState } from "@ledgerhq/device-sdk-core";

import { useSdk } from "@/providers/DeviceSdkProvider";

export function useSessionState(sessionId: SessionId) {
export function useDeviceSessionState(sessionId: DeviceSessionId) {
const sdk = useSdk();
const [sessionState, setSessionState] = useState<SessionDeviceState>();
const [deviceSessionState, setDeviceSessionState] =
useState<DeviceSessionState>();

useEffect(() => {
if (sessionId) {
const subscription = sdk
.getSessionDeviceState({
.getDeviceSessionState({
sessionId,
})
.subscribe((state) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
setSessionState(state);
setDeviceSessionState(state);
});

return () => {
Expand All @@ -24,5 +25,5 @@ export function useSessionState(sessionId: SessionId) {
}
}, [sessionId, sdk]);

return sessionState;
return deviceSessionState;
}
38 changes: 38 additions & 0 deletions apps/sample/src/providers/DeviceSessionsProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { Context, createContext, useContext, useReducer } from "react";

import {
AddSessionAction,
DeviceSessionsInitialState,
deviceSessionsReducer,
DeviceSessionsState,
RemoveSessionAction,
} from "@/reducers/deviceSessions";

type DeviceSessionsContextType = {
state: DeviceSessionsState;
dispatch: (value: AddSessionAction | RemoveSessionAction) => void;
};

const DeviceSessionsContext: Context<DeviceSessionsContextType> =
createContext<DeviceSessionsContextType>({
state: DeviceSessionsInitialState,
dispatch: () => null,
});

export const DeviceSessionsProvider: React.FC<React.PropsWithChildren> = ({
children,
}) => {
const [state, dispatch] = useReducer(
deviceSessionsReducer,
DeviceSessionsInitialState,
);

return (
<DeviceSessionsContext.Provider value={{ state, dispatch }}>
{children}
</DeviceSessionsContext.Provider>
);
};

export const useDeviceSessionsContext = () =>
useContext<DeviceSessionsContextType>(DeviceSessionsContext);
35 changes: 0 additions & 35 deletions apps/sample/src/providers/SessionsProvider/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { Reducer } from "react";
import { ConnectedDevice, SessionId } from "@ledgerhq/device-sdk-core";
import { ConnectedDevice, DeviceSessionId } from "@ledgerhq/device-sdk-core";

export type SessionsState = {
selectedId: SessionId | null;
deviceById: Record<SessionId, ConnectedDevice>;
export type DeviceSessionsState = {
selectedId: DeviceSessionId | null;
deviceById: Record<DeviceSessionId, ConnectedDevice>;
};

export type AddSessionAction = {
type: "add_session";
payload: { sessionId: SessionId; connectedDevice: ConnectedDevice };
payload: { sessionId: DeviceSessionId; connectedDevice: ConnectedDevice };
};

export type RemoveSessionAction = {
type: "remove_session";
payload: { sessionId: SessionId };
payload: { sessionId: DeviceSessionId };
};

export const SessionsInitialState: SessionsState = {
export const DeviceSessionsInitialState: DeviceSessionsState = {
selectedId: null,
deviceById: {},
};

export const sessionsReducer: Reducer<
SessionsState,
export const deviceSessionsReducer: Reducer<
DeviceSessionsState,
AddSessionAction | RemoveSessionAction
> = (state, action) => {
switch (action.type) {
Expand Down

0 comments on commit 2f05046

Please sign in to comment.