]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/moment-time.tsx
component classes v2
[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 } 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     moment.locale([...i18n.languages]);
19   }
20
21   createdAndModifiedTimes() {
22     const updated = this.props.updated;
23     let line = `${capitalizeFirstLetter(i18n.t("created"))}: ${this.format(
24       this.props.published
25     )}`;
26     if (updated) {
27       line += `\n\n\n${capitalizeFirstLetter(i18n.t("modified"))} ${this.format(
28         updated
29       )}`;
30     }
31     return line;
32   }
33
34   render() {
35     if (!this.props.ignoreUpdated && this.props.updated) {
36       return (
37         <span
38           data-tippy-content={this.createdAndModifiedTimes()}
39           className="moment-time font-italics pointer unselectable"
40         >
41           <Icon icon="edit-2" classes="icon-inline me-1" />
42           {moment.utc(this.props.updated).fromNow(!this.props.showAgo)}
43         </span>
44       );
45     } else {
46       const published = this.props.published;
47       return (
48         <span
49           className="moment-time pointer unselectable"
50           data-tippy-content={this.format(published)}
51         >
52           {moment.utc(published).fromNow(!this.props.showAgo)}
53         </span>
54       );
55     }
56   }
57
58   format(input: string): string {
59     return moment.utc(input).local().format("LLLL");
60   }
61 }