]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/sort-select.tsx
Fix I18 next circular reference
[lemmy-ui.git] / src / shared / components / common / sort-select.tsx
1 import { randomStr } from "@utils/helpers";
2 import { Component, linkEvent } from "inferno";
3 import { SortType } from "lemmy-js-client";
4 import { relTags, sortingHelpUrl } from "../../config";
5 import { I18NextService } from "../../services";
6 import { Icon } from "./icon";
7
8 interface SortSelectProps {
9   sort: SortType;
10   onChange(val: SortType): void;
11   hideHot?: boolean;
12   hideMostComments?: boolean;
13 }
14
15 interface SortSelectState {
16   sort: SortType;
17 }
18
19 export class SortSelect extends Component<SortSelectProps, SortSelectState> {
20   private id = `sort-select-${randomStr()}`;
21   state: SortSelectState = {
22     sort: this.props.sort,
23   };
24
25   constructor(props: any, context: any) {
26     super(props, context);
27   }
28
29   static getDerivedStateFromProps(props: SortSelectProps): SortSelectState {
30     return {
31       sort: props.sort,
32     };
33   }
34
35   render() {
36     return (
37       <>
38         <select
39           id={this.id}
40           name={this.id}
41           value={this.state.sort}
42           onChange={linkEvent(this, this.handleSortChange)}
43           className="sort-select form-select d-inline-block w-auto me-2"
44           aria-label={I18NextService.i18n.t("sort_type")}
45         >
46           <option disabled aria-hidden="true">
47             {I18NextService.i18n.t("sort_type")}
48           </option>
49           {!this.props.hideHot && [
50             <option key={"Hot"} value={"Hot"}>
51               {I18NextService.i18n.t("hot")}
52             </option>,
53             <option key={"Active"} value={"Active"}>
54               {I18NextService.i18n.t("active")}
55             </option>,
56           ]}
57           <option value={"New"}>{I18NextService.i18n.t("new")}</option>
58           <option value={"Old"}>{I18NextService.i18n.t("old")}</option>
59           {!this.props.hideMostComments && [
60             <option key={"MostComments"} value={"MostComments"}>
61               {I18NextService.i18n.t("most_comments")}
62             </option>,
63             <option key={"NewComments"} value={"NewComments"}>
64               {I18NextService.i18n.t("new_comments")}
65             </option>,
66           ]}
67           <option disabled aria-hidden="true">
68             ─────
69           </option>
70           <option value={"TopDay"}>{I18NextService.i18n.t("top_day")}</option>
71           <option value={"TopWeek"}>{I18NextService.i18n.t("top_week")}</option>
72           <option value={"TopMonth"}>
73             {I18NextService.i18n.t("top_month")}
74           </option>
75           <option value={"TopYear"}>{I18NextService.i18n.t("top_year")}</option>
76           <option value={"TopAll"}>{I18NextService.i18n.t("top_all")}</option>
77         </select>
78         <a
79           className="sort-select-icon text-muted"
80           href={sortingHelpUrl}
81           rel={relTags}
82           title={I18NextService.i18n.t("sorting_help")}
83         >
84           <Icon icon="help-circle" classes="icon-inline" />
85         </a>
86       </>
87     );
88   }
89
90   handleSortChange(i: SortSelect, event: any) {
91     i.props.onChange(event.target.value);
92   }
93 }