]> Untitled Git - lemmy.git/blob - ui/src/components/moment-time.tsx
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / ui / src / components / moment-time.tsx
1 import { Component } from 'inferno';
2 import moment from 'moment';
3 import { getMomentLanguage, capitalizeFirstLetter } from '../utils';
4 import { i18n } from '../i18next';
5
6 interface MomentTimeProps {
7   data: {
8     published?: string;
9     when_?: string;
10     updated?: string;
11   };
12   showAgo?: boolean;
13 }
14
15 export class MomentTime extends Component<MomentTimeProps, any> {
16   constructor(props: any, context: any) {
17     super(props, context);
18
19     let lang = getMomentLanguage();
20
21     moment.locale(lang);
22   }
23
24   render() {
25     if (this.props.data.updated) {
26       return (
27         <span
28           data-tippy-content={`${capitalizeFirstLetter(
29             i18n.t('modified')
30           )} ${this.format(this.props.data.updated)}`}
31           className="font-italics pointer unselectable"
32         >
33           <svg class="icon icon-inline mr-1">
34             <use xlinkHref="#icon-edit-2"></use>
35           </svg>
36           {moment.utc(this.props.data.updated).fromNow(!this.props.showAgo)}
37         </span>
38       );
39     } else {
40       let str = this.props.data.published || this.props.data.when_;
41       return (
42         <span
43           className="pointer unselectable"
44           data-tippy-content={this.format(str)}
45         >
46           {moment.utc(str).fromNow(!this.props.showAgo)}
47         </span>
48       );
49     }
50   }
51
52   format(input: string): string {
53     return moment
54       .utc(input)
55       .local()
56       .format('LLLL');
57   }
58 }