Hi all, I'm trying to play around with my first extension on Flarum, which adds a button to the sidebar of a discussion page and when clicked, toggles a modal that writes every single mention in that discussion. The text in the modal might look like:
Post with ID 1 was mentioned by post with ID 2
Post with ID 2 was mentioned by post with ID 4
Post with ID 3 has no mentions
Post with ID 4 has no mentions
Right now, I'm extending the sidebarItems
method of DiscussionPage
to add the button, and I've added a new class MentionsModal.js
that will display the desired info. Of course, I need the info on the discussion so currently I've also extended the controls
method of DiscussionControls
to just retrieve the discussion
object, which contains all the data I need. Specifically, the js/src/index.js
file looks like
var currentDiscussion;
const mentionExtension = (items) => {
items.add('MentionInfo', Button.component({
// Various attributes...
onclick: () => {
app.modal.show(new MentionsModal({
discussion: currentDiscussion
}));
},
}
));
}
const getMentionData = (items, discussion, context) => {
currentDiscussion = discussion;
}
app.initializers.add('my/extension', () => {
extend(DiscussionControls, 'controls', getMentionData);
extend(DiscussionPage.prototype, 'sidebarItems', mentionExtension);
});
I have a problem and a question:
Problem: The user needs to refresh the page to retrieve the most recent data. This is applicable whenever I add a new post and then click on my button without refreshing. If I don't refresh, the the discussion
object that I'm currently getting the data from becomes undefined, and the modal displays nothing because of this. What attributes/objects/etc. should I tinker with to fix this? I tried looking into PostStreamScrubber
because it is able to account for new posts without a refresh, but was unable to figure out exactly which piece of code in it allows for that.
Question: This feels like the wrong way to do what I want, even if I didn't care about the problem above. The extension to DiscussionControls
is just to retrieve a piece of data, not really to add any sort of functionality.
Thanks in advance for all the help!