]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/sort-select.tsx
fix submodule error
[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={"Controversial"}>
58             {I18NextService.i18n.t("controversial")}
59           </option>
60           <option value={"New"}>{I18NextService.i18n.t("new")}</option>
61           <option value={"Old"}>{I18NextService.i18n.t("old")}</option>
62           {!this.props.hideMostComments && [
63             <option key={"MostComments"} value={"MostComments"}>
64               {I18NextService.i18n.t("most_comments")}
65             </option>,
66             <option key={"NewComments"} value={"NewComments"}>
67               {I18NextService.i18n.t("new_comments")}
68             </option>,
69           ]}
70           <option disabled aria-hidden="true">
71             ─────
72           </option>
73           <option value={"TopHour"}>{I18NextService.i18n.t("top_hour")}</option>
74           <option value={"TopSixHour"}>
75             {I18NextService.i18n.t("top_six_hours")}
76           </option>
77           <option value={"TopTwelveHour"}>
78             {I18NextService.i18n.t("top_twelve_hours")}
79           </option>
80           <option value={"TopDay"}>{I18NextService.i18n.t("top_day")}</option>
81           <option value={"TopWeek"}>{I18NextService.i18n.t("top_week")}</option>
82           <option value={"TopMonth"}>
83             {I18NextService.i18n.t("top_month")}
84           </option>
85           <option value={"TopThreeMonths"}>
86             {I18NextService.i18n.t("top_three_months")}
87           </option>
88           <option value={"TopSixMonths"}>
89             {I18NextService.i18n.t("top_six_months")}
90           </option>
91           <option value={"TopNineMonths"}>
92             {I18NextService.i18n.t("top_nine_months")}
93           </option>
94           <option value={"TopYear"}>{I18NextService.i18n.t("top_year")}</option>
95           <option value={"TopAll"}>{I18NextService.i18n.t("top_all")}</option>
96         </select>
97         <a
98           className="sort-select-icon text-muted"
99           href={sortingHelpUrl}
100           rel={relTags}
101           title={I18NextService.i18n.t("sorting_help")}
102         >
103           <Icon icon="help-circle" classes="icon-inline" />
104         </a>
105       </>
106     );
107   }
108
109   handleSortChange(i: SortSelect, event: any) {
110     i.props.onChange(event.target.value);
111   }
112 }