lbry-desktop/src/ui/component/dateTime/view.jsx

72 lines
1.6 KiB
React
Raw Normal View History

// @flow
import React from 'react';
import moment from 'moment';
type Props = {
2019-03-05 05:46:57 +01:00
date?: number | {},
timeAgo?: boolean,
formatOptions: {},
show?: string,
};
class DateTime extends React.PureComponent<Props> {
static SHOW_DATE = 'date';
static SHOW_TIME = 'time';
static SHOW_BOTH = 'both';
2017-10-08 22:43:56 +02:00
static defaultProps = {
formatOptions: {
month: 'long',
day: 'numeric',
year: 'numeric',
2017-10-08 22:43:56 +02:00
},
};
componentWillMount() {
2018-11-07 17:03:42 +01:00
// this.refreshDate(this.props);
}
2018-11-07 17:03:42 +01:00
componentWillReceiveProps() {
// this.refreshDate(props);
}
2018-11-07 17:03:42 +01:00
// Removing this for performance reasons. Can be un-commented once block_show is better with large numbers of calls
// Or the date is included in the claim
//
// refreshDate(props: Props) {
// const { block, date, fetchBlock } = props;
// if (block && date === undefined) {
// fetchBlock(block);
// }
// }
render() {
const { date, formatOptions, timeAgo } = this.props;
const show = this.props.show || DateTime.SHOW_BOTH;
2019-03-05 05:46:57 +01:00
const locale = i18n.getLocale();
2019-03-06 18:04:26 +01:00
const locales = ['en-US'];
if (locale) {
locales.push(locale);
}
if (timeAgo) {
return date ? <span>{moment(date).from(moment())}</span> : <span />;
}
return (
<span>
{date &&
(show === DateTime.SHOW_BOTH || show === DateTime.SHOW_DATE) &&
2019-03-06 18:04:26 +01:00
date.toLocaleDateString(locales, formatOptions)}
{show === DateTime.SHOW_BOTH && ' '}
{date &&
(show === DateTime.SHOW_BOTH || show === DateTime.SHOW_TIME) &&
date.toLocaleTimeString()}
{!date && '...'}
</span>
);
}
}
export default DateTime;