2021-05-14 08:15:15 +00:00
|
|
|
import { Tooltip } from "@material-ui/core";
|
2020-05-14 09:30:32 +00:00
|
|
|
import useDateLocalize from "@saleor/hooks/useDateLocalize";
|
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 { LocaleConsumer } from "../Locale";
|
|
|
|
import { Consumer } from "./DateContext";
|
2022-07-19 11:58:00 +00:00
|
|
|
import { useStyles } from "./styles";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
interface DateProps {
|
|
|
|
date: string;
|
|
|
|
plain?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Date: React.FC<DateProps> = ({ date, plain }) => {
|
2022-07-19 11:58:00 +00:00
|
|
|
const classes = useStyles();
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const localizeDate = useDateLocalize();
|
2022-07-19 11:58:00 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const getHumanized = (value: string, locale: string, currentDate: number) =>
|
|
|
|
moment(value)
|
|
|
|
.locale(locale)
|
|
|
|
.from(currentDate);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<LocaleConsumer>
|
2019-10-16 15:18:29 +00:00
|
|
|
{({ locale }) => (
|
2019-06-19 14:40:52 +00:00
|
|
|
<Consumer>
|
|
|
|
{currentDate =>
|
|
|
|
plain ? (
|
|
|
|
localizeDate(date)
|
|
|
|
) : (
|
2022-07-19 11:58:00 +00:00
|
|
|
<Tooltip
|
|
|
|
title={localizeDate(date)}
|
|
|
|
PopperProps={{
|
|
|
|
className: classes.tooltip,
|
|
|
|
}}
|
|
|
|
>
|
2019-06-19 14:40:52 +00:00
|
|
|
<time dateTime={date}>
|
|
|
|
{getHumanized(date, locale, currentDate)}
|
|
|
|
</time>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Consumer>
|
|
|
|
)}
|
|
|
|
</LocaleConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
Date.displayName = "Date";
|
|
|
|
export default Date;
|