Skip to content

Commit

Permalink
chore: further improve extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideIadeluca committed Nov 29, 2024
1 parent 72ea681 commit 92772f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
28 changes: 27 additions & 1 deletion js/src/forum/components/SolvedFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import Component, { ComponentAttrs } from 'flarum/common/Component';
import Dropdown from 'flarum/common/components/Dropdown';
import Button from 'flarum/common/components/Button';

export interface SolvedFilterAttrs extends ComponentAttrs {}
import type Mithril from 'mithril';
import type Tag from 'flarum/tags/common/models/Tag';
export interface SolvedFilterAttrs extends ComponentAttrs {
alwaysShow?: boolean;
}

export default class SolvedFilter extends Component<SolvedFilterAttrs> {
view() {
if (!this.shouldShowFilter()) return null;

const selected = app.discussions.bestAnswer as unknown as number;
const options = ['all', 'solved', 'unsolved'];

Expand Down Expand Up @@ -39,4 +45,24 @@ export default class SolvedFilter extends Component<SolvedFilterAttrs> {
})
);
}

shouldShowFilter() {
const { alwaysShow } = this.attrs;

if (alwaysShow) return true;

if (!app.forum.attribute('showBestAnswerFilterUi')) return false;

const tag: Tag = app.current.get('tag');

if (!tag?.isQnA?.()) {
if (app.discussions.bestAnswer) {
delete app.discussions.bestAnswer;
app.discussions.refresh();
}
return false;
}

return true;
}
}
15 changes: 0 additions & 15 deletions js/src/forum/extenders/extendIndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@ export default function extendIndexPage() {
});

extend(IndexPage.prototype, 'viewItems', function (items) {
if (!app.forum.attribute('showBestAnswerFilterUi')) {
return;
}

const tag = this.currentTag();

if (!tag?.isQnA?.()) {
if (app.discussions.bestAnswer) {
delete app.discussions.bestAnswer;
app.discussions.refresh();
}

return;
}

items.add('solved-filter', <SolvedFilter />);
});
}

0 comments on commit 92772f0

Please sign in to comment.