mirror of
https://github.com/quantum5/qcal.git
synced 2025-08-02 03:58:12 -04:00
27 lines
758 B
TypeScript
27 lines
758 B
TypeScript
import React from 'react';
|
|
import {dateJDN} from './gregorian';
|
|
|
|
function dayMs(date: Date): number {
|
|
return date.getHours() * 3_600_000 + date.getMinutes() * 60_000 + date.getSeconds() * 1000 + date.getMilliseconds();
|
|
}
|
|
|
|
type DayChangerProps = {
|
|
onDateChange: (jdn: number) => void,
|
|
};
|
|
|
|
export function DayChanger({onDateChange}: DayChangerProps): null {
|
|
const [jdn, setJDN] = React.useState(dateJDN(new Date()));
|
|
|
|
React.useEffect(() => {
|
|
const now = new Date();
|
|
const timer = setTimeout(() => {
|
|
const jdn = dateJDN(new Date());
|
|
setJDN(jdn);
|
|
onDateChange(jdn);
|
|
}, 86_400_000 - dayMs(now));
|
|
return () => clearTimeout(timer);
|
|
}, [jdn, onDateChange]);
|
|
|
|
return null;
|
|
}
|