From 03e2950e0e094cfb7485d70a60e57ed4b7e510dd Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Fri, 29 Nov 2024 18:35:41 +0100 Subject: [PATCH 1/4] refactor: improve extensibility --- js/src/forum/components/SolvedFilter.tsx | 44 ++++++++++++++++++++++ js/src/forum/components/index.ts | 2 + js/src/forum/extenders/extendIndexPage.tsx | 37 +----------------- 3 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 js/src/forum/components/SolvedFilter.tsx diff --git a/js/src/forum/components/SolvedFilter.tsx b/js/src/forum/components/SolvedFilter.tsx new file mode 100644 index 0000000..013f485 --- /dev/null +++ b/js/src/forum/components/SolvedFilter.tsx @@ -0,0 +1,44 @@ +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'; + +export interface SolvedFilterAttrs extends ComponentAttrs { + selected: number; +} + +export default class SolvedFilter extends Component { + view() { + const { selected } = this.attrs; + 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`) + ); + }) + ); + } +} diff --git a/js/src/forum/components/index.ts b/js/src/forum/components/index.ts index 7970eae..f092908 100644 --- a/js/src/forum/components/index.ts +++ b/js/src/forum/components/index.ts @@ -6,6 +6,7 @@ import SelectBestAnswerItem from './SelectBestAnswerItem'; import SelectBestAnswerNotification from './SelectBestAnswerNotification'; import SolutionSearchItem from './SolutionSearchItem'; import SolutionSearchSource from './SolutionSearchSource'; +import SolvedFilter from './SolvedFilter'; export const components = { SelectBestAnswerItem, @@ -16,4 +17,5 @@ export const components = { BestAnswerInDiscussionNotification, SelectBestAnswerNotification, SolutionSearchItem, + SolvedFilter, }; diff --git a/js/src/forum/extenders/extendIndexPage.tsx b/js/src/forum/extenders/extendIndexPage.tsx index d331f5a..93d87ce 100644 --- a/js/src/forum/extenders/extendIndexPage.tsx +++ b/js/src/forum/extenders/extendIndexPage.tsx @@ -1,8 +1,7 @@ import app from 'flarum/forum/app'; import { extend } from 'flarum/common/extend'; import IndexPage from 'flarum/forum/components/IndexPage'; -import Dropdown from 'flarum/common/components/Dropdown'; -import Button from 'flarum/common/components/Button'; +import SolvedFilter from '../components/SolvedFilter'; export default function extendIndexPage() { extend(IndexPage.prototype, 'sidebarItems', function (items) { @@ -37,40 +36,8 @@ export default function extendIndexPage() { return; } - const options = ['all', 'solved', 'unsolved']; - const selected = app.discussions.bestAnswer as unknown as number; - items.add( - 'solved-filter', - 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`) - ); - }) - ) - ); + items.add('solved-filter', ); }); } From 72ea68145827c64f6204743005fa295c41d65213 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Fri, 29 Nov 2024 18:51:33 +0100 Subject: [PATCH 2/4] chore --- js/src/forum/components/SolvedFilter.tsx | 6 ++---- js/src/forum/extenders/extendIndexPage.tsx | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/js/src/forum/components/SolvedFilter.tsx b/js/src/forum/components/SolvedFilter.tsx index 013f485..ec60528 100644 --- a/js/src/forum/components/SolvedFilter.tsx +++ b/js/src/forum/components/SolvedFilter.tsx @@ -3,13 +3,11 @@ 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 { - selected: number; -} +export interface SolvedFilterAttrs extends ComponentAttrs {} export default class SolvedFilter extends Component { view() { - const { selected } = this.attrs; + const selected = app.discussions.bestAnswer as unknown as number; const options = ['all', 'solved', 'unsolved']; return Dropdown.component( diff --git a/js/src/forum/extenders/extendIndexPage.tsx b/js/src/forum/extenders/extendIndexPage.tsx index 93d87ce..aa12ca9 100644 --- a/js/src/forum/extenders/extendIndexPage.tsx +++ b/js/src/forum/extenders/extendIndexPage.tsx @@ -36,8 +36,6 @@ export default function extendIndexPage() { return; } - const selected = app.discussions.bestAnswer as unknown as number; - - items.add('solved-filter', ); + items.add('solved-filter', ); }); } From 92772f050b614391a39a608ff1518ca92a91573a Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Fri, 29 Nov 2024 19:03:39 +0100 Subject: [PATCH 3/4] chore: further improve extensibility --- js/src/forum/components/SolvedFilter.tsx | 28 +++++++++++++++++++++- js/src/forum/extenders/extendIndexPage.tsx | 15 ------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/js/src/forum/components/SolvedFilter.tsx b/js/src/forum/components/SolvedFilter.tsx index ec60528..f3060d0 100644 --- a/js/src/forum/components/SolvedFilter.tsx +++ b/js/src/forum/components/SolvedFilter.tsx @@ -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 { view() { + if (!this.shouldShowFilter()) return null; + const selected = app.discussions.bestAnswer as unknown as number; const options = ['all', 'solved', 'unsolved']; @@ -39,4 +45,24 @@ export default class SolvedFilter extends Component { }) ); } + + 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; + } } diff --git a/js/src/forum/extenders/extendIndexPage.tsx b/js/src/forum/extenders/extendIndexPage.tsx index aa12ca9..f769d08 100644 --- a/js/src/forum/extenders/extendIndexPage.tsx +++ b/js/src/forum/extenders/extendIndexPage.tsx @@ -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', ); }); } From 8884c3a5233a21b39eac53b47f683617df6ae332 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Fri, 29 Nov 2024 19:09:04 +0100 Subject: [PATCH 4/4] chore --- js/src/forum/components/SolvedFilter.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/js/src/forum/components/SolvedFilter.tsx b/js/src/forum/components/SolvedFilter.tsx index f3060d0..370b828 100644 --- a/js/src/forum/components/SolvedFilter.tsx +++ b/js/src/forum/components/SolvedFilter.tsx @@ -2,9 +2,8 @@ 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 Mithril from 'mithril'; import type Tag from 'flarum/tags/common/models/Tag'; + export interface SolvedFilterAttrs extends ComponentAttrs { alwaysShow?: boolean; }