]> Untitled Git - lemmy.git/blob - ui/src/components/sort-select.tsx
Merge branch 'master' into federation
[lemmy.git] / ui / src / components / sort-select.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { SortType } from '../interfaces';
3 import { sortingHelpUrl } from '../utils';
4 import { i18n } from '../i18next';
5
6 interface SortSelectProps {
7   sort: SortType;
8   onChange?(val: SortType): any;
9   hideHot?: boolean;
10 }
11
12 interface SortSelectState {
13   sort: SortType;
14 }
15
16 export class SortSelect extends Component<SortSelectProps, SortSelectState> {
17   private emptyState: SortSelectState = {
18     sort: this.props.sort,
19   };
20
21   constructor(props: any, context: any) {
22     super(props, context);
23     this.state = this.emptyState;
24   }
25
26   render() {
27     return (
28       <>
29         <select
30           value={this.state.sort}
31           onChange={linkEvent(this, this.handleSortChange)}
32           class="custom-select custom-select-sm w-auto mr-2"
33         >
34           <option disabled>{i18n.t('sort_type')}</option>
35           {!this.props.hideHot && (
36             <option value={SortType.Hot}>{i18n.t('hot')}</option>
37           )}
38           <option value={SortType.New}>{i18n.t('new')}</option>
39           <option disabled>─────</option>
40           <option value={SortType.TopDay}>{i18n.t('top_day')}</option>
41           <option value={SortType.TopWeek}>{i18n.t('week')}</option>
42           <option value={SortType.TopMonth}>{i18n.t('month')}</option>
43           <option value={SortType.TopYear}>{i18n.t('year')}</option>
44           <option value={SortType.TopAll}>{i18n.t('all')}</option>
45         </select>
46         <a
47           className="text-muted"
48           href={sortingHelpUrl}
49           target="_blank"
50           title={i18n.t('sorting_help')}
51         >
52           <svg class={`icon icon-inline`}>
53             <use xlinkHref="#icon-help-circle"></use>
54           </svg>
55         </a>
56       </>
57     );
58   }
59
60   handleSortChange(i: SortSelect, event: any) {
61     i.state.sort = Number(event.target.value);
62     i.setState(i.state);
63     i.props.onChange(i.state.sort);
64   }
65 }