import { useBattery } from './useBattery' import { useBeforeUnload } from './useBeforeUnload' export function App() { const { supported, loading, level, charging, chargingTime, dischargingTime, error } = useBattery() // Ask for confirmation when leaving the page (custom message may be ignored by some browsers) useBeforeUnload(true, 'Are you sure you want to leave this page? Unsaved battery info will be lost.') if (!supported) { return (

Battery Status

Battery Status API is not supported by this browser.

Tip: On some desktop browsers, this API may be disabled for privacy reasons.

) } return (

Battery Status

{loading ? (

Loading…

) : error ? (

{error}

) : (
Charging: {charging ? 'Yes' : 'No'}
Level: {Math.round(level * 100)}%
{typeof chargingTime === 'number' && charging && (
Time to full: {formatTime(chargingTime)}
)} {typeof dischargingTime === 'number' && !charging && (
Time to empty: {formatTime(dischargingTime)}
)}
)}
) } function formatTime(seconds: number) { if (!isFinite(seconds) || seconds < 0) return 'N/A' const h = Math.floor(seconds / 3600) const m = Math.floor((seconds % 3600) / 60) if (h === 0) return `${m}m` return `${h}h ${m}m` }