@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
29 lines (23 loc) • 795 B
text/typescript
import { useEffect, useState } from "react";
import {
getDeviceInfoAction as defaultGetDeviceInfoAction,
GetDeviceInfoActionState,
initialState,
} from "../actions/getDeviceInfo";
export type UseGetDeviceInfoArgs = {
getDeviceInfoAction?: typeof defaultGetDeviceInfoAction;
deviceId: string;
deviceName: string | null;
};
export const useGetDeviceInfo = ({
getDeviceInfoAction = defaultGetDeviceInfoAction,
deviceId,
deviceName,
}: UseGetDeviceInfoArgs): GetDeviceInfoActionState => {
const [state, setState] = useState<GetDeviceInfoActionState>(initialState);
useEffect(() => {
const sub = getDeviceInfoAction({ deviceId, deviceName }).subscribe(setState);
return () => sub.unsubscribe();
}, [deviceId, getDeviceInfoAction, deviceName]);
return state;
};