2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { SortType } from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2022-02-24 15:31:44 +00:00
|
|
|
import { randomStr, relTags, sortingHelpUrl } from "../../utils";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Icon } from "./icon";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface SortSelectProps {
|
|
|
|
sort: SortType;
|
|
|
|
onChange?(val: SortType): any;
|
|
|
|
hideHot?: boolean;
|
2021-02-01 18:48:42 +00:00
|
|
|
hideMostComments?: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface SortSelectState {
|
|
|
|
sort: SortType;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SortSelect extends Component<SortSelectProps, SortSelectState> {
|
|
|
|
private id = `sort-select-${randomStr()}`;
|
|
|
|
private emptyState: SortSelectState = {
|
|
|
|
sort: this.props.sort,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(props: any): SortSelectState {
|
|
|
|
return {
|
|
|
|
sort: props.sort,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<select
|
|
|
|
id={this.id}
|
|
|
|
name={this.id}
|
|
|
|
value={this.state.sort}
|
|
|
|
onChange={linkEvent(this, this.handleSortChange)}
|
|
|
|
class="custom-select w-auto mr-2 mb-2"
|
2021-02-22 02:39:04 +00:00
|
|
|
aria-label={i18n.t("sort_type")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<option disabled aria-hidden="true">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("sort_type")}
|
2021-02-11 20:35:27 +00:00
|
|
|
</option>
|
2021-02-01 18:48:42 +00:00
|
|
|
{!this.props.hideHot && [
|
2021-02-22 02:39:04 +00:00
|
|
|
<option value={SortType.Hot}>{i18n.t("hot")}</option>,
|
|
|
|
<option value={SortType.Active}>{i18n.t("active")}</option>,
|
2021-02-01 18:48:42 +00:00
|
|
|
]}
|
2021-02-22 02:39:04 +00:00
|
|
|
<option value={SortType.New}>{i18n.t("new")}</option>
|
2021-02-18 15:57:00 +00:00
|
|
|
{!this.props.hideMostComments && [
|
2021-02-01 18:48:42 +00:00
|
|
|
<option value={SortType.MostComments}>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("most_comments")}
|
2021-02-18 15:57:00 +00:00
|
|
|
</option>,
|
|
|
|
<option value={SortType.NewComments}>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("new_comments")}
|
2021-02-18 15:57:00 +00:00
|
|
|
</option>,
|
|
|
|
]}
|
2021-02-11 20:35:27 +00:00
|
|
|
<option disabled aria-hidden="true">
|
|
|
|
─────
|
|
|
|
</option>
|
2021-02-22 02:39:04 +00:00
|
|
|
<option value={SortType.TopDay}>{i18n.t("top_day")}</option>
|
|
|
|
<option value={SortType.TopWeek}>{i18n.t("top_week")}</option>
|
|
|
|
<option value={SortType.TopMonth}>{i18n.t("top_month")}</option>
|
|
|
|
<option value={SortType.TopYear}>{i18n.t("top_year")}</option>
|
|
|
|
<option value={SortType.TopAll}>{i18n.t("top_all")}</option>
|
2020-09-06 16:15:25 +00:00
|
|
|
</select>
|
|
|
|
<a
|
|
|
|
className="text-muted"
|
|
|
|
href={sortingHelpUrl}
|
2022-02-24 15:31:44 +00:00
|
|
|
rel={relTags}
|
2021-02-22 02:39:04 +00:00
|
|
|
title={i18n.t("sorting_help")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="help-circle" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</a>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSortChange(i: SortSelect, event: any) {
|
|
|
|
i.props.onChange(event.target.value);
|
|
|
|
}
|
|
|
|
}
|