2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { withTranslation, WithTranslation } from "react-i18next";
|
2019-10-20 16:59:02 +02:00
|
|
|
import { formatDistance, format, parseISO, Locale } from "date-fns";
|
|
|
|
|
import { enUS, de, es } from "date-fns/locale";
|
|
|
|
|
import styled from "styled-components";
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type LocaleMap = {
|
2019-10-21 10:57:56 +02:00
|
|
|
[key: string]: Locale;
|
2019-10-20 16:59:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type DateInput = Date | string;
|
|
|
|
|
|
|
|
|
|
const supportedLocales: LocaleMap = {
|
2019-10-09 15:51:49 +02:00
|
|
|
enUS,
|
2019-10-30 15:26:43 +01:00
|
|
|
en: enUS,
|
2019-10-09 15:51:49 +02:00
|
|
|
de,
|
2019-10-20 16:59:02 +02:00
|
|
|
es
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-03-12 10:44:58 +01:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-20 16:59:02 +02:00
|
|
|
date?: DateInput;
|
2019-10-19 16:38:07 +02:00
|
|
|
timeZone?: string;
|
2019-10-12 14:03:49 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* baseDate is the date from which the distance is calculated,
|
|
|
|
|
* default is the current time (new Date()). This property
|
|
|
|
|
* is required to keep snapshots tests green over the time on
|
|
|
|
|
* ci server.
|
|
|
|
|
*/
|
2019-10-20 16:59:02 +02:00
|
|
|
baseDate?: DateInput;
|
|
|
|
|
};
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type Options = {
|
|
|
|
|
addSuffix: boolean;
|
|
|
|
|
locale: Locale;
|
|
|
|
|
timeZone?: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
const DateElement = styled.time`
|
2019-10-08 16:42:08 +02:00
|
|
|
border-bottom: 1px dotted rgba(219, 219, 219);
|
|
|
|
|
cursor: help;
|
|
|
|
|
`;
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
class DateFromNow extends React.Component<Props> {
|
2019-10-20 16:59:02 +02:00
|
|
|
getLocale = (): Locale => {
|
2019-10-09 15:51:49 +02:00
|
|
|
const { i18n } = this.props;
|
2019-10-30 15:26:43 +01:00
|
|
|
|
2019-10-30 15:29:49 +01:00
|
|
|
for (const lng of i18n.languages || []) {
|
2019-10-30 15:26:43 +01:00
|
|
|
const locale = supportedLocales[lng];
|
|
|
|
|
if (locale) {
|
|
|
|
|
return locale;
|
|
|
|
|
}
|
2019-10-09 15:51:49 +02:00
|
|
|
}
|
2019-10-30 15:26:43 +01:00
|
|
|
|
2019-10-30 15:29:49 +01:00
|
|
|
const locale = supportedLocales[i18n.language];
|
|
|
|
|
if (locale) {
|
|
|
|
|
return locale;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 15:26:43 +01:00
|
|
|
return enUS;
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
createOptions = () => {
|
2019-10-12 14:03:49 +02:00
|
|
|
const { timeZone } = this.props;
|
2019-10-20 16:59:02 +02:00
|
|
|
const options: Options = {
|
2019-10-12 14:03:49 +02:00
|
|
|
addSuffix: true,
|
2019-10-20 16:59:02 +02:00
|
|
|
locale: this.getLocale()
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-10-12 14:03:49 +02:00
|
|
|
if (timeZone) {
|
|
|
|
|
options.timeZone = timeZone;
|
|
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
toDate = (value: DateInput): Date => {
|
|
|
|
|
if (value instanceof Date) {
|
2019-10-30 15:26:43 +01:00
|
|
|
return value;
|
2019-10-20 16:59:02 +02:00
|
|
|
}
|
|
|
|
|
return parseISO(value);
|
2019-10-21 10:57:56 +02:00
|
|
|
};
|
2019-10-20 16:59:02 +02:00
|
|
|
|
2019-10-12 14:03:49 +02:00
|
|
|
getBaseDate = () => {
|
|
|
|
|
const { baseDate } = this.props;
|
|
|
|
|
if (baseDate) {
|
2019-10-20 16:59:02 +02:00
|
|
|
return this.toDate(baseDate);
|
2019-10-12 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
return new Date();
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
render() {
|
|
|
|
|
const { date } = this.props;
|
|
|
|
|
if (date) {
|
2019-10-20 16:59:02 +02:00
|
|
|
const isoDate = this.toDate(date);
|
2019-10-09 15:51:49 +02:00
|
|
|
const options = this.createOptions();
|
2019-10-12 14:03:49 +02:00
|
|
|
const distance = formatDistance(isoDate, this.getBaseDate(), options);
|
2019-10-20 16:59:02 +02:00
|
|
|
const formatted = format(isoDate, "yyyy-MM-dd HH:mm:ss", options);
|
2019-10-09 15:51:49 +02:00
|
|
|
return <DateElement title={formatted}>{distance}</DateElement>;
|
2019-02-07 09:28:43 +01:00
|
|
|
}
|
|
|
|
|
return null;
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
export default withTranslation()(DateFromNow);
|