]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/sort-select.tsx
Merge branch 'update-issue-template' of https://github.com/alectrocute/lemmy-ui into...
[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={"TopHour"}>{I18NextService.i18n.t("top_hour")}</option>
71           <option value={"TopSixHour"}>
72             {I18NextService.i18n.t("top_six_hours")}
73           </option>
74           <option value={"TopTwelveHour"}>
75             {I18NextService.i18n.t("top_twelve_hours")}
76           </option>
77           <option value={"TopDay"}>{I18NextService.i18n.t("top_day")}</option>
78           <option value={"TopWeek"}>{I18NextService.i18n.t("top_week")}</option>
79           <option value={"TopMonth"}>
80             {I18NextService.i18n.t("top_month")}
81           </option>
82           <option value={"TopYear"}>{I18NextService.i18n.t("top_year")}</option>
83           <option value={"TopAll"}>{I18NextService.i18n.t("top_all")}</option>
84         </select>
85         <a
86           className="sort-select-icon text-muted"
87           href={sortingHelpUrl}
88           rel={relTags}
89           title={I18NextService.i18n.t("sorting_help")}
90         >
91           <Icon icon="help-circle" classes="icon-inline" />
92         </a>
93       </>
94     );
95   }
96
97   handleSortChange(i: SortSelect, event: any) {
98     i.props.onChange(event.target.value);
99   }
100 }