]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/moment-time.tsx
reset, merge issues
[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, getLanguages } from "../../utils";
5 import { Icon } from "./icon";
6
7 interface MomentTimeProps {
8   published: string;
9   updated?: string;
10   showAgo?: boolean;
11   ignoreUpdated?: boolean;
12 }
13
14 export class MomentTime extends Component<MomentTimeProps, any> {
15   constructor(props: any, context: any) {
16     super(props, context);
17
18     const lang = getLanguages();
19
20     moment.locale(lang);
21   }
22
23   createdAndModifiedTimes() {
24     const updated = this.props.updated;
25     let line = `${capitalizeFirstLetter(i18n.t("created"))}: ${this.format(
26       this.props.published
27     )}`;
28     if (updated) {
29       line += `\n\n\n${capitalizeFirstLetter(i18n.t("modified"))} ${this.format(
30         updated
31       )}`;
32     }
33     return line;
34   }
35
36   render() {
37     if (!this.props.ignoreUpdated && this.props.updated) {
38       return (
39         <span
40           data-tippy-content={this.createdAndModifiedTimes()}
41           className="font-italics pointer unselectable"
42         >
43           <Icon icon="edit-2" classes="icon-inline mr-1" />
44           {moment.utc(this.props.updated).fromNow(!this.props.showAgo)}
45         </span>
46       );
47     } else {
48       const published = this.props.published;
49       return (
50         <span
51           className="pointer unselectable"
52           data-tippy-content={this.format(published)}
53         >
54           {moment.utc(published).fromNow(!this.props.showAgo)}
55         </span>
56       );
57     }
58   }
59
60   format(input: string): string {
61     return moment.utc(input).local().format("LLLL");
62   }
63 }