|
| 1 | +import app from 'flarum/forum/app'; |
| 2 | +import { extend } from 'flarum/common/extend'; |
| 3 | +import Dropdown from 'flarum/common/components/Dropdown'; |
| 4 | +import IndexPage from 'flarum/forum/components/IndexPage'; |
| 5 | +import Button from 'flarum/common/components/Button'; |
| 6 | + |
| 7 | +import type ItemList from 'flarum/common/utils/ItemList'; |
| 8 | +import type Mithril from 'mithril'; |
| 9 | + |
| 10 | +export default function replaceSortDropdown() { |
| 11 | + extend(IndexPage.prototype, 'viewItems', function (items: ItemList<Mithril.Children>) { |
| 12 | + if (!app.session.user) return; |
| 13 | + |
| 14 | + const sortMap = app.discussions.sortMap(); |
| 15 | + |
| 16 | + const sortOptions = Object.keys(sortMap).reduce((acc: any, sortId) => { |
| 17 | + acc[sortId] = app.translator.trans(`core.forum.index_sort.${sortId}_button`); |
| 18 | + return acc; |
| 19 | + }, {}); |
| 20 | + |
| 21 | + let userSort = app.session.user.preferences()?.['discussion_sort']; |
| 22 | + |
| 23 | + items.setContent( |
| 24 | + 'sort', |
| 25 | + <Dropdown |
| 26 | + buttonClassName="Button" |
| 27 | + label={sortOptions[userSort] || Object.keys(sortMap).map((key) => sortOptions[key])[0]} |
| 28 | + accessibleToggleLabel={app.translator.trans('core.forum.index_sort.toggle_dropdown_accessible_label')} |
| 29 | + > |
| 30 | + {Object.keys(sortOptions).map((value) => { |
| 31 | + const label = sortOptions[value]; |
| 32 | + const active = (userSort || Object.keys(sortMap)[0]) === value; |
| 33 | + |
| 34 | + function handleClick() { |
| 35 | + app.search.changeSort.bind(app.search, value)(); |
| 36 | + userSort = value; |
| 37 | + app.session.user?.savePreferences({ discussion_sort: value }); |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <Button icon={active ? 'fas fa-check' : true} onclick={handleClick} active={active}> |
| 42 | + {label} |
| 43 | + </Button> |
| 44 | + ); |
| 45 | + })} |
| 46 | + </Dropdown> |
| 47 | + ); |
| 48 | + }); |
| 49 | +} |
0 commit comments