]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/moment-time.tsx
Fix I18 next circular reference
[lemmy-ui.git] / src / shared / components / common / moment-time.tsx
1 import { capitalizeFirstLetter } from "@utils/helpers";
2 import { Component } from "inferno";
3 import moment from "moment";
4 import { I18NextService } from "../../services";
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([...I18NextService.i18n.languages]);
19   }
20
21   createdAndModifiedTimes() {
22     const updated = this.props.updated;
23     let line = `${capitalizeFirstLetter(
24       I18NextService.i18n.t("created")
25     )}: ${this.format(this.props.published)}`;
26     if (updated) {
27       line += `\n\n\n${capitalizeFirstLetter(
28         I18NextService.i18n.t("modified")
29       )} ${this.format(updated)}`;
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 }