lemmy/ui/src/components/moment-time.tsx

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Component } from 'inferno';
import moment from 'moment';
2020-03-05 03:35:55 +00:00
import { getMomentLanguage, capitalizeFirstLetter } from '../utils';
import { i18n } from '../i18next';
2019-03-27 19:54:55 +00:00
interface MomentTimeProps {
data: {
published?: string;
when_?: string;
2019-03-27 19:54:55 +00:00
updated?: string;
};
2020-03-08 18:29:17 +00:00
showAgo?: boolean;
2019-03-27 19:54:55 +00:00
}
export class MomentTime extends Component<MomentTimeProps, any> {
constructor(props: any, context: any) {
2019-03-27 19:54:55 +00:00
super(props, context);
2019-08-17 17:22:38 +00:00
let lang = getMomentLanguage();
moment.locale(lang);
2019-03-27 19:54:55 +00:00
}
render() {
if (this.props.data.updated) {
return (
<span
data-tippy-content={`${capitalizeFirstLetter(
i18n.t('modified')
)} ${this.format(this.props.data.updated)}`}
className="font-italics pointer unselectable"
>
<svg class="icon icon-inline mr-1">
<use xlinkHref="#icon-edit-2"></use>
</svg>
2020-03-08 18:29:17 +00:00
{moment.utc(this.props.data.updated).fromNow(!this.props.showAgo)}
</span>
);
2019-03-27 19:54:55 +00:00
} else {
let str = this.props.data.published || this.props.data.when_;
return (
<span
className="pointer unselectable"
data-tippy-content={this.format(str)}
>
2020-03-08 18:29:17 +00:00
{moment.utc(str).fromNow(!this.props.showAgo)}
</span>
);
2019-03-27 19:54:55 +00:00
}
}
format(input: string): string {
return moment
.utc(input)
.local()
.format('LLLL');
}
2019-03-27 19:54:55 +00:00
}