2022-12-11 00:00:11 +01:00
|
|
|
import { Center, Stack, Text, Title } from '@mantine/core';
|
2022-12-19 18:26:04 +01:00
|
|
|
import { IconClock } from '@tabler/icons';
|
2022-12-10 22:14:31 +01:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2022-12-16 21:01:06 +01:00
|
|
|
import { useSetSafeInterval } from '../../tools/hooks/useSetSafeInterval';
|
2022-12-18 21:21:23 +01:00
|
|
|
import { defineWidget } from '../helper';
|
|
|
|
|
import { IWidget } from '../widgets';
|
|
|
|
|
|
|
|
|
|
const definition = defineWidget({
|
2022-12-18 22:58:00 +01:00
|
|
|
id: 'date',
|
2022-12-18 21:21:23 +01:00
|
|
|
icon: IconClock,
|
|
|
|
|
options: {
|
|
|
|
|
display24HourFormat: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
defaultValue: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-12-18 21:50:08 +01:00
|
|
|
gridstack: {
|
|
|
|
|
minWidth: 4,
|
|
|
|
|
minHeight: 2,
|
|
|
|
|
maxWidth: 12,
|
|
|
|
|
maxHeight: 12,
|
|
|
|
|
},
|
2022-12-18 22:58:00 +01:00
|
|
|
component: DateTile,
|
2022-12-18 21:21:23 +01:00
|
|
|
});
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
export type IDateWidget = IWidget<typeof definition['id'], typeof definition>;
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-19 18:26:04 +01:00
|
|
|
interface DateTileProps {
|
|
|
|
|
widget: IDateWidget;
|
2022-12-10 22:14:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 18:26:04 +01:00
|
|
|
function DateTile({ widget }: DateTileProps) {
|
2022-12-10 22:14:31 +01:00
|
|
|
const date = useDateState();
|
2022-12-19 17:03:39 +01:00
|
|
|
const formatString = widget.properties.display24HourFormat ? 'HH:mm' : 'h:mm A';
|
2022-12-10 22:14:31 +01:00
|
|
|
|
|
|
|
|
return (
|
2022-12-19 18:26:04 +01:00
|
|
|
<Center style={{ height: '100%' }}>
|
|
|
|
|
<Stack spacing="xs">
|
|
|
|
|
<Title>{dayjs(date).format(formatString)}</Title>
|
|
|
|
|
<Text size="lg">{dayjs(date).format('dddd, MMMM D')}</Text>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Center>
|
2022-12-10 22:14:31 +01:00
|
|
|
);
|
2022-12-18 21:21:23 +01:00
|
|
|
}
|
2022-12-10 22:14:31 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* State which updates when the minute is changing
|
|
|
|
|
* @returns current date updated every new minute
|
|
|
|
|
*/
|
|
|
|
|
const useDateState = () => {
|
|
|
|
|
const [date, setDate] = useState(new Date());
|
|
|
|
|
const setSafeInterval = useSetSafeInterval();
|
|
|
|
|
const timeoutRef = useRef<NodeJS.Timeout>(); // reference for initial timeout until first minute change
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
|
setDate(new Date());
|
|
|
|
|
// Starts intervall which update the date every minute
|
|
|
|
|
setSafeInterval(() => {
|
|
|
|
|
setDate(new Date());
|
|
|
|
|
}, 1000 * 60);
|
|
|
|
|
}, getMsUntilNextMinute());
|
|
|
|
|
|
|
|
|
|
return () => timeoutRef.current && clearTimeout(timeoutRef.current);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return date;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// calculates the amount of milliseconds until next minute starts.
|
|
|
|
|
const getMsUntilNextMinute = () => {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const nextMinute = new Date(
|
|
|
|
|
now.getFullYear(),
|
|
|
|
|
now.getMonth(),
|
|
|
|
|
now.getDate(),
|
|
|
|
|
now.getHours(),
|
|
|
|
|
now.getMinutes() + 1
|
|
|
|
|
);
|
|
|
|
|
return nextMinute.getTime() - now.getTime();
|
|
|
|
|
};
|
2022-12-18 21:21:23 +01:00
|
|
|
|
|
|
|
|
export default definition;
|