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

57 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Component } from 'inferno';
import moment from 'moment';
import { getMomentLanguage, setupTippy } 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;
};
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
}
componentDidMount() {
setupTippy();
}
2019-03-27 19:54:55 +00:00
render() {
if (this.props.data.updated) {
return (
<span
data-tippy-content={this.format(this.props.data.updated)}
className="font-italics pointer unselectable"
>
{i18n.t('modified')} {moment.utc(this.props.data.updated).fromNow()}
</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)}
>
{moment.utc(str).fromNow()}
</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
}