Skip to content

Commit

Permalink
chore: refactor extenders
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 28, 2024
1 parent b970103 commit addacc0
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 145 deletions.
31 changes: 31 additions & 0 deletions js/src/forum/extenders/extendDiscussionComposer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { extend } from 'flarum/common/extend';
import app from 'flarum/forum/app';
import DiscussionComposer from 'flarum/forum/components/DiscussionComposer';

export default function extendDiscussionComposer() {
extend(DiscussionComposer.prototype, 'headerItems', function (items) {
const tags = this.composer.fields.tags;
if (tags === undefined) return;

const qna = tags.some((t) => t.isQnA());

if (!qna) return;

this.attrs.titlePlaceholder = app.translator.trans('fof-best-answer.forum.composer.titlePlaceholder');

if (items.has('discussionTitle')) {
items.setContent(
'discussionTitle',
<h3>
<input
className="FormControl"
bidi={this.title}
placeholder={this.attrs.titlePlaceholder}
disabled={!!this.attrs.disabled}
onkeydown={this.onkeydown.bind(this)}
/>
</h3>
);
}
});
}
18 changes: 18 additions & 0 deletions js/src/forum/extenders/extendDiscussionListState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import DiscussionListState from 'flarum/forum/states/DiscussionListState';

export default function extendDiscussionListState() {
extend(DiscussionListState.prototype, 'requestParams', function (params) {
if (app.discussions.bestAnswer) {
const negate = app.discussions.bestAnswer === '2';
const prepend = negate ? '-' : '';

params.filter[`${prepend}solved-discussions`] = true;

if (params.filter.q) {
params.filter.q += ` ${prepend}is:solved`;
}
}
});
}
11 changes: 11 additions & 0 deletions js/src/forum/extenders/extendDiscussionSearchSource.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import DiscussionsSearchSource from 'flarum/forum/components/DiscussionsSearchSource';

export default function extendDiscussionsSearchSource() {
extend(DiscussionsSearchSource.prototype, 'queryMutators', function (mutators: string[]) {
if (app.forum.attribute<boolean>('solutionSearchEnabled')) {
mutators.push('-is:solved');
}
});
}
78 changes: 78 additions & 0 deletions js/src/forum/extenders/extendIndexPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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';

export default function extendIndexPage() {
extend(IndexPage.prototype, 'sidebarItems', function (items) {
/** @ts-ignore */
const tag = this.currentTag();

if (!tag?.isQnA?.()) return;

const canStartDiscussion = app.forum.attribute('canStartDiscussion') || !app.session.user;
const cta = items.get('newDiscussion');
cta.children = app.translator.trans(
canStartDiscussion ? 'fof-best-answer.forum.index.ask_question' : 'fof-best-answer.forum.index.cannot_ask_question'
);

if (items.has('startDiscussion')) {
items.setContent('startDiscussion', cta);
}
});

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

/** @ts-ignore */
const tag = this.currentTag();

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

return;
}

const options = ['all', 'solved', 'unsolved'];

const selected = app.discussions.bestAnswer;

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[key])[0]}_label`
),
accessibleToggleLabel: app.translator.trans('fof-best-answer.forum.filter.accessible_label'),
},
Object.keys(options).map((value) => {
const label = options[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`)
);
})
)
);
});
}
145 changes: 0 additions & 145 deletions js/src/forum/index.js

This file was deleted.

38 changes: 38 additions & 0 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import app from 'flarum/forum/app';
import SelectBestAnswerNotification from './components/SelectBestAnswerNotification';
import addBestAnswerAction from './addBestAnswerAction';
import addBestAnswerView from './addBestAnswerView';
import addAnsweredBadge from './addAnsweredBadge';
import AwardedBestAnswerNotification from './components/AwardedBestAnswerNotification';
import BestAnswerInDiscussionNotification from './components/BestAnswerInDiscussionNotification';
import extendNotifications from './extenders/extendNotifications';
import addBestAnswerCountToUsers from './addBestAnswerCountToUsers';
import addBestAnswerCountSort from '../common/addBestAnswerCountSort';
import extendSearch from './extenders/extendSearch';
import extendDiscussionsSearchSource from './extenders/extendDiscussionSearchSource';
import extendIndexPage from './extenders/extendIndexPage';
import extendDiscussionListState from './extenders/extendDiscussionListState';
import extendDiscussionComposer from './extenders/extendDiscussionComposer';

export * from './components';

export { default as extend } from './extend';

app.initializers.add('fof/best-answer', () => {
app.notificationComponents.selectBestAnswer = SelectBestAnswerNotification;
app.notificationComponents.awardedBestAnswer = AwardedBestAnswerNotification;
app.notificationComponents.bestAnswerInDiscussion = BestAnswerInDiscussionNotification;

addAnsweredBadge();
addBestAnswerAction();
addBestAnswerView();
addBestAnswerCountToUsers();
addBestAnswerCountSort();

extendNotifications();
extendSearch();
extendDiscussionsSearchSource();
extendIndexPage();
extendDiscussionListState();
extendDiscussionComposer();
});

0 comments on commit addacc0

Please sign in to comment.