2023-04-12 13:55:42 +00:00
|
|
|
import { Tooltip } from "@saleor/macaw-ui/next";
|
2019-08-09 11:14:35 +00:00
|
|
|
import moment from "moment-timezone";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ReactMoment from "react-moment";
|
|
|
|
|
|
|
|
import { LocaleConsumer } from "../Locale";
|
|
|
|
import { TimezoneConsumer } from "../Timezone";
|
|
|
|
import { Consumer } from "./DateContext";
|
|
|
|
|
|
|
|
interface DateTimeProps {
|
|
|
|
date: string;
|
|
|
|
plain?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const DateTime: React.FC<DateTimeProps> = ({ date, plain }) => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const getTitle = (value: string, locale?: string, tz?: string) => {
|
|
|
|
let date = moment(value).locale(locale);
|
|
|
|
if (tz !== undefined) {
|
|
|
|
date = date.tz(tz);
|
|
|
|
}
|
|
|
|
return date.format("lll");
|
|
|
|
};
|
2022-07-19 11:58:00 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<TimezoneConsumer>
|
|
|
|
{tz => (
|
|
|
|
<LocaleConsumer>
|
2019-10-16 15:18:29 +00:00
|
|
|
{({ locale }) => (
|
2019-06-19 14:40:52 +00:00
|
|
|
<Consumer>
|
|
|
|
{currentDate =>
|
|
|
|
plain ? (
|
|
|
|
getTitle(date, locale, tz)
|
|
|
|
) : (
|
2023-04-12 13:55:42 +00:00
|
|
|
<Tooltip>
|
|
|
|
<Tooltip.Trigger>
|
|
|
|
<div>
|
|
|
|
<ReactMoment from={currentDate} locale={locale} tz={tz}>
|
|
|
|
{date}
|
|
|
|
</ReactMoment>
|
|
|
|
</div>
|
|
|
|
</Tooltip.Trigger>
|
|
|
|
<Tooltip.Content side="bottom">
|
|
|
|
<Tooltip.Arrow />
|
|
|
|
{getTitle(date, locale, tz)}
|
|
|
|
</Tooltip.Content>
|
2019-06-19 14:40:52 +00:00
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Consumer>
|
|
|
|
)}
|
|
|
|
</LocaleConsumer>
|
|
|
|
)}
|
|
|
|
</TimezoneConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
DateTime.displayName = "DateTime";
|
|
|
|
export default DateTime;
|