misuzu/assets/misuzu.js/comments/section.jsx

84 lines
2.4 KiB
React
Raw Normal View History

2025-02-16 00:19:42 +00:00
#include msgbox.jsx
#include comments/api.js
#include comments/form.jsx
#include comments/listing.jsx
2025-02-19 02:45:28 +00:00
#include comments/options.jsx
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
const MszCommentsSection = function(args) {
let { category: catName } = args ?? {};
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
const options = new MszCommentsOptions;
2025-02-16 00:19:42 +00:00
const listing = new MszCommentsListing({ root: true });
const element = <div class="comments">
2025-02-19 02:45:28 +00:00
{options}
2025-02-16 00:19:42 +00:00
{listing}
</div>;
let form;
2025-02-19 02:45:28 +00:00
let retryAct;
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
const clearForm = () => {
if(form) {
element.removeChild(form.element);
form = undefined;
}
};
const setForm = elem => {
clearForm();
form = elem;
$insertBefore(element.firstChild, form.element);
};
2025-02-20 01:55:49 +00:00
const initForm = (userInfo, catInfo) => {
if(!userInfo)
setForm(new MszCommentsFormNotice({ body: 'You must be logged in to post comments.' }));
else if(!userInfo.can_create)
setForm(new MszCommentsFormNotice({ body: 'You are not allowed to comment.' }));
else if(catInfo.locked)
setForm(new MszCommentsFormNotice({ body: 'This comment section is closed.' }));
2025-02-19 02:45:28 +00:00
else
2025-02-20 01:55:49 +00:00
setForm(new MszCommentsForm({ userInfo, catInfo, listing }));
2025-02-19 02:45:28 +00:00
};
const pub = {
get element() { return element; },
async reload() {
clearForm();
listing.reset();
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
try {
const { user, category, posts } = await MszCommentsApi.getCategory(catName);
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
retryAct = undefined;
options.reset();
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
initForm(user, category);
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
if(user?.can_lock)
options.appendAction(new MszCommentsOptionsLockAction(
category,
() => {
initForm(user, category);
2025-02-20 01:55:49 +00:00
listing.updateLocked();
2025-02-19 02:45:28 +00:00
}
));
2025-02-16 00:19:42 +00:00
2025-02-19 02:45:28 +00:00
listing.addPosts(category, user, posts);
} catch(ex) {
console.error(ex);
listing.removeLoading();
2025-02-20 01:55:49 +00:00
form = new MszCommentsFormNotice({ body: 'Failed to load comments.' });
2025-02-19 02:45:28 +00:00
$insertBefore(element.firstChild, form.element);
if(!retryAct)
options.appendAction(retryAct = new MszCommentsOptionsRetryAction(pub));
}
2025-02-16 00:19:42 +00:00
},
};
2025-02-19 02:45:28 +00:00
pub.reload();
return pub;
2025-02-16 00:19:42 +00:00
};