]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/moment-time.tsx
Re-organized components folder. (#339)
[lemmy-ui.git] / src / shared / components / common / moment-time.tsx
1 import { Component } from "inferno";
2 import moment from "moment";
3 import { i18n } from "../../i18next";
4 import { capitalizeFirstLetter, getMomentLanguage } from "../../utils";
5 import { Icon } from "./icon";
6
7 interface MomentTimeProps {
8   data: {
9     published?: string;
10     when_?: string;
11     updated?: string;
12   };
13   showAgo?: boolean;
14   ignoreUpdated?: boolean;
15 }
16
17 export class MomentTime extends Component<MomentTimeProps, any> {
18   constructor(props: any, context: any) {
19     super(props, context);
20
21     let lang = getMomentLanguage();
22
23     moment.locale(lang);
24   }
25
26   render() {
27     if (!this.props.ignoreUpdated && this.props.data.updated) {
28       return (
29         <span
30           data-tippy-content={`${capitalizeFirstLetter(
31             i18n.t("modified")
32           )} ${this.format(this.props.data.updated)}`}
33           className="font-italics pointer unselectable"
34         >
35           <Icon icon="edit-2" classes="icon-inline mr-1" />
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.utc(input).local().format("LLLL");
54   }
55 }