]> Untitled Git - lemmy.git/blob - ui/src/components/sort-select.tsx
Some more prettifying, cleaning up community and main.tsx
[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   static getDerivedStateFromProps(props: any): SortSelectState {
27     return {
28       sort: props.sort,
29     };
30   }
31
32   render() {
33     return (
34       <>
35         <select
36           value={this.state.sort}
37           onChange={linkEvent(this, this.handleSortChange)}
38           class="custom-select w-auto mr-2 mb-2"
39         >
40           <option disabled>{i18n.t('sort_type')}</option>
41           {!this.props.hideHot && (
42             <option value={SortType.Hot}>{i18n.t('hot')}</option>
43           )}
44           <option value={SortType.New}>{i18n.t('new')}</option>
45           <option disabled>─────</option>
46           <option value={SortType.TopDay}>{i18n.t('top_day')}</option>
47           <option value={SortType.TopWeek}>{i18n.t('week')}</option>
48           <option value={SortType.TopMonth}>{i18n.t('month')}</option>
49           <option value={SortType.TopYear}>{i18n.t('year')}</option>
50           <option value={SortType.TopAll}>{i18n.t('all')}</option>
51         </select>
52         <a
53           className="text-muted"
54           href={sortingHelpUrl}
55           target="_blank"
56           rel="noopener"
57           title={i18n.t('sorting_help')}
58         >
59           <svg class={`icon icon-inline`}>
60             <use xlinkHref="#icon-help-circle"></use>
61           </svg>
62         </a>
63       </>
64     );
65   }
66
67   handleSortChange(i: SortSelect, event: any) {
68     i.props.onChange(event.target.value);
69   }
70 }