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

62 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Component } from 'inferno';
import moment from 'moment';
import { getMomentLanguage, setupTippy, 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;
};
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={`${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-04 19:24:45 +00:00
{moment.utc(this.props.data.updated).fromNow(true)}
</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-04 19:24:45 +00:00
{moment.utc(str).fromNow(true)}
</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
}