@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
51 lines • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetLatestAvailableFirmware = void 0;
const react_1 = require("react");
const logs_1 = require("@ledgerhq/logs");
const getLatestAvailableFirmware_1 = require("../actions/getLatestAvailableFirmware");
const filterIgnoredFirmwareUpdates_1 = require("./filterIgnoredFirmwareUpdates");
/**
* Hook to get the latest available firmware that the device could update to
*
* @param `deviceId` A device id, or an empty string if device is usb plugged
* @param isHookEnabled A boolean to enable (true, default value) or disable (false) the hook
* @returns an object containing the state of the process to get the latest available firmware.
* The state is as follow (+ shared state with other actions):
* - `firmwareUpdateContext`: the `FirmwareUpdateContext` when retrieved, null otherwise
* - `deviceInfo`: the `DeviceInfo` when retrieved, null otherwise
* - `status`: the current status of the action: "error" | "no-available-firmware" | "available-firmware" meaning the action has finished
* - `error`: an error coming from the business logic, the device or internal. Some error are retried:
* `{ type`: "SharedError", retrying: true, ... }`
* - `...sharedActionState`
*/
const useGetLatestAvailableFirmware = ({ getLatestAvailableFirmwareAction = getLatestAvailableFirmware_1.getLatestAvailableFirmwareAction, deviceId, deviceName, isHookEnabled = true, ignoredOSUpdates, }) => {
const [state, setState] = (0, react_1.useState)(getLatestAvailableFirmware_1.initialState);
(0, react_1.useEffect)(() => {
if (!isHookEnabled) {
return;
}
// Resets the resulting state on each new triggering
// (not on clean up, to keep the last state when `isHookEnabled === false`)
setState(getLatestAvailableFirmware_1.initialState);
const subscription = getLatestAvailableFirmwareAction({
deviceId,
deviceName,
}).subscribe({
next: newValue => {
const filteredValue = (0, filterIgnoredFirmwareUpdates_1.filterIgnoredFirmwareUpdates)(newValue, ignoredOSUpdates);
setState(filteredValue);
},
error: (error) => {
// Error from an action should be handled like an event and should not reach here
(0, logs_1.log)("useGetLatestAvailableFirmware", "Unknown error", error);
},
});
return () => {
subscription.unsubscribe();
};
}, [deviceId, getLatestAvailableFirmwareAction, ignoredOSUpdates, isHookEnabled]);
return { state };
};
exports.useGetLatestAvailableFirmware = useGetLatestAvailableFirmware;
//# sourceMappingURL=useGetLatestAvailableFirmware.js.map