]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/moment-time.tsx
Change from using Link to NavLink. resolve #269
[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   createdAndModifiedTimes() {
27     let created = this.props.data.published || this.props.data.when_;
28     return `
29       <div>
30         <div>
31           ${capitalizeFirstLetter(i18n.t("created"))}: ${this.format(created)}
32         </div>
33         <div>
34           ${capitalizeFirstLetter(i18n.t("modified"))} ${this.format(
35       this.props.data.updated
36     )}
37         </div>
38         </div>`;
39   }
40
41   render() {
42     if (!this.props.ignoreUpdated && this.props.data.updated) {
43       return (
44         <span
45           data-tippy-content={this.createdAndModifiedTimes()}
46           data-tippy-allowHtml={true}
47           className="font-italics pointer unselectable"
48         >
49           <Icon icon="edit-2" classes="icon-inline mr-1" />
50           {moment.utc(this.props.data.updated).fromNow(!this.props.showAgo)}
51         </span>
52       );
53     } else {
54       let created = this.props.data.published || this.props.data.when_;
55       return (
56         <span
57           className="pointer unselectable"
58           data-tippy-content={this.format(created)}
59         >
60           {moment.utc(created).fromNow(!this.props.showAgo)}
61         </span>
62       );
63     }
64   }
65
66   format(input: string): string {
67     return moment.utc(input).local().format("LLLL");
68   }
69 }