|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import type { Nullable } from "@/lib/types/nullable.js"; |
| 4 | + |
| 5 | +interface BatteryState { |
| 6 | + level: Nullable<number>; |
| 7 | + charging: Nullable<boolean>; |
| 8 | + chargingTime: Nullable<number>; |
| 9 | + dischargingTime: Nullable<number>; |
| 10 | +} |
| 11 | + |
| 12 | +interface BatteryManager extends Readonly<BatteryState>, EventTarget { |
| 13 | + onchargingchange: () => void; |
| 14 | + onchargingtimechange: () => void; |
| 15 | + ondischargingtimechange: () => void; |
| 16 | + onlevelchange: () => void; |
| 17 | +} |
| 18 | + |
| 19 | +interface NavigatorWithPossibleBattery extends Navigator { |
| 20 | + getBattery?: () => Promise<BatteryManager>; |
| 21 | +} |
| 22 | + |
| 23 | +type UseBatteryState = |
| 24 | + | { isSupported: false } // Battery API is not supported |
| 25 | + | { isSupported: true; loading: true } // battery API supported but not fetched yet |
| 26 | + | (BatteryState & { isSupported: true; loading: false }); // battery API supported and fetched |
| 27 | + |
| 28 | +const nav: NavigatorWithPossibleBattery | undefined = |
| 29 | + typeof navigator !== "undefined" ? navigator : undefined; |
| 30 | + |
| 31 | +const isBatteryApiSupported = nav && typeof nav.getBattery === "function"; |
| 32 | + |
| 33 | +/** |
| 34 | + * Returns the device's current battery state if available via the Navigator API. |
| 35 | + */ |
| 36 | +function useBattery(): UseBatteryState { |
| 37 | + const [state, setState] = useState<UseBatteryState>({ |
| 38 | + isSupported: true, |
| 39 | + loading: true, |
| 40 | + }); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (!nav?.getBattery) { |
| 44 | + setState((s) => ({ ...s, isSupported: false, loading: false })); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + let battery: BatteryManager | null = null; |
| 49 | + |
| 50 | + const handleChange = () => { |
| 51 | + if (!battery) return; |
| 52 | + |
| 53 | + const { level, charging, chargingTime, dischargingTime } = battery; |
| 54 | + |
| 55 | + setState({ |
| 56 | + isSupported: true, |
| 57 | + loading: false, |
| 58 | + level, |
| 59 | + charging, |
| 60 | + chargingTime, |
| 61 | + dischargingTime, |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + nav.getBattery().then((b) => { |
| 66 | + battery = b; |
| 67 | + handleChange(); |
| 68 | + |
| 69 | + b.addEventListener("levelchange", handleChange); |
| 70 | + b.addEventListener("chargingchange", handleChange); |
| 71 | + b.addEventListener("chargingtimechange", handleChange); |
| 72 | + b.addEventListener("dischargingtimechange", handleChange); |
| 73 | + }); |
| 74 | + |
| 75 | + return () => { |
| 76 | + if (battery) { |
| 77 | + battery.removeEventListener("levelchange", handleChange); |
| 78 | + battery.removeEventListener("chargingchange", handleChange); |
| 79 | + battery.removeEventListener("chargingtimechange", handleChange); |
| 80 | + battery.removeEventListener("dischargingtimechange", handleChange); |
| 81 | + } |
| 82 | + }; |
| 83 | + }, []); |
| 84 | + |
| 85 | + return state; |
| 86 | +} |
| 87 | + |
| 88 | +const useBattery_ = isBatteryApiSupported |
| 89 | + ? useBattery |
| 90 | + : (): UseBatteryState => ({ isSupported: false }); |
| 91 | + |
| 92 | +export { useBattery_ as useBattery }; |
0 commit comments