-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve extensibility (#113)
* refactor: improve extensibility * chore * chore: further improve extensibility * chore
- Loading branch information
1 parent
0604ce1
commit 155d615
Showing
3 changed files
with
71 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import app from 'flarum/forum/app'; | ||
import Component, { ComponentAttrs } from 'flarum/common/Component'; | ||
import Dropdown from 'flarum/common/components/Dropdown'; | ||
import Button from 'flarum/common/components/Button'; | ||
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']; | ||
|
||
return Dropdown.component( | ||
{ | ||
buttonClassName: 'Button', | ||
label: app.translator.trans( | ||
`fof-best-answer.forum.filter.${options[selected] || Object.keys(options).map((key) => options[Number(key)])[0]}_label` | ||
), | ||
accessibleToggleLabel: app.translator.trans('fof-best-answer.forum.filter.accessible_label'), | ||
}, | ||
Object.keys(options).map((value) => { | ||
const label = options[Number(value)]; | ||
const active = (selected || Object.keys(options)[0]) === value; | ||
|
||
return Button.component( | ||
{ | ||
icon: active ? 'fas fa-check' : true, | ||
active: active, | ||
onclick: () => { | ||
app.discussions.bestAnswer = value; | ||
if (value === '0') { | ||
delete app.discussions.bestAnswer; | ||
} | ||
app.discussions.refresh(); | ||
}, | ||
}, | ||
app.translator.trans(`fof-best-answer.forum.filter.${label}_label`) | ||
); | ||
}) | ||
); | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters