43 lines
1.3 KiB
React
Raw Normal View History

2022-09-16 10:53:12 +03:00
import { useState, useEffect } from "react";
2022-09-25 19:43:00 +03:00
import { useTranslation } from "next-i18next";
import classNames from "classnames";
2022-09-16 10:53:12 +03:00
const textSizes = {
"4xl": "text-4xl",
"3xl": "text-3xl",
"2xl": "text-2xl",
xl: "text-xl",
lg: "text-lg",
md: "text-md",
sm: "text-sm",
xs: "text-xs",
};
export default function DateTime({ options }) {
const { text_size: textSize, locale, format } = options;
2022-09-16 10:53:12 +03:00
const { i18n } = useTranslation();
2022-11-28 10:42:21 -08:00
const [date, setDate] = useState("");
const dateLocale = locale ?? i18n.language;
2022-09-16 10:53:12 +03:00
useEffect(() => {
const dateFormat = new Intl.DateTimeFormat(dateLocale, { ...format });
2022-09-16 10:53:12 +03:00
const interval = setInterval(() => {
2022-11-28 10:42:21 -08:00
setDate(dateFormat.format(new Date()));
2022-09-16 10:53:12 +03:00
}, 1000);
return () => clearInterval(interval);
}, [date, setDate, dateLocale, format]);
2022-09-16 10:53:12 +03:00
return (
<div className={classNames(
"flex flex-col justify-center first:ml-0 ml-4",
options?.styleBoxed === true && " mt-2 m:mb-0 rounded-md shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-100/20 dark:bg-white/5 p-3",
)}>
<div className="flex flex-row items-center grow justify-end">
<span className={`text-theme-800 dark:text-theme-200 tabular-nums ${textSizes[textSize || "lg"]}`}>
2022-11-28 10:42:21 -08:00
{date}
</span>
</div>
2022-09-16 10:53:12 +03:00
</div>
);
}