Skip to content

Commit 20e6528

Browse files
chore: reimplement frontend
1 parent 83e7055 commit 20e6528

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

js/src/forum/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import app from 'flarum/forum/app';
2+
import replaceSortDropdown from './extenders/replaceSortDropdown';
23

34
app.initializers.add('glowingblue-save-sorting-preferences', () => {
4-
// nothing here yet
5+
replaceSortDropdown();
56
});

0 commit comments

Comments
 (0)