@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
39 lines • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useManualRefresh = useManualRefresh;
const react_1 = require("react");
/**
* State machine for manual refresh lifecycle:
* idle → (click) → waitingForSync → (syncStarted) → syncing → (syncDone) → idle
*/
function reducer(phase, event) {
switch (event) {
case "click":
return "waitingForSync";
case "syncStarted":
return phase === "waitingForSync" ? "syncing" : phase;
case "syncDone":
return phase === "syncing" || phase === "waitingForSync" ? "idle" : phase;
default:
return phase;
}
}
/**
* Tracks whether a user-triggered manual refresh is in progress.
* Latches on when a click is detected and releases once the sync completes.
*/
function useManualRefresh(stableSyncPending, lastUserSyncClickTimestamp) {
const [phase, dispatch] = (0, react_1.useReducer)(reducer, "idle");
const prevTimestampRef = (0, react_1.useRef)(lastUserSyncClickTimestamp);
(0, react_1.useEffect)(() => {
if (lastUserSyncClickTimestamp !== prevTimestampRef.current) {
prevTimestampRef.current = lastUserSyncClickTimestamp;
dispatch("click");
}
}, [lastUserSyncClickTimestamp]);
(0, react_1.useEffect)(() => {
dispatch(stableSyncPending ? "syncStarted" : "syncDone");
}, [stableSyncPending]);
return phase !== "idle";
}
//# sourceMappingURL=useManualRefresh.js.map