forked from akai/readest
38552a0c2e
* added current time to desktop bar * added time prototype to footer, needs code cleanup and settings toggle * fixed settings toggle, added translations and code cleanup * added battery support and moved Statusbar to own Component * #3306 added 24 hour clock support * refactored code styling and getting rid of any type in battery hook * Add battery info for Tauri Apps --------- Co-authored-by: Huang Xin <chrox.huang@gmail.com>
26 lines
650 B
TypeScript
26 lines
650 B
TypeScript
import { useEffect, useState, useMemo } from 'react';
|
|
|
|
export function useCurrentTime(enabled: boolean, use24Hour = true, intervalMs = 10000) {
|
|
const [currentTime, setCurrentTime] = useState(() => new Date());
|
|
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
|
|
const timer = setInterval(() => {
|
|
setCurrentTime(new Date());
|
|
}, intervalMs);
|
|
|
|
return () => clearInterval(timer);
|
|
}, [enabled, intervalMs]);
|
|
|
|
return useMemo(() => {
|
|
if (!enabled) return '';
|
|
|
|
return currentTime.toLocaleTimeString([], {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: !use24Hour,
|
|
});
|
|
}, [currentTime, enabled, use24Hour]);
|
|
}
|