لدي خبرة في بناء حلول مخصصة باستخدام منصات/أطر عمل أخرى، وأرغب في فهم ما إذا كانت هذه هي الطريقة الصحيحة لإنشاء مكون سمة في Discourse.
يبدو أن الأمر يعمل، لكن هذا لا يعني بالضرورة أنه الطريقة الصحيحة.
باختصار، يجب أن يؤدي هذا إلى إخفاء ردود الفعل اعتمادًا على الفئة التي يوجد بها الموضوع. هل هذه هي الطريقة الصحيحة للقيام بذلك؟
<script type="text/discourse-plugin" version="0.1">
$(document).ready(function() {
try {
const isTopicPage = /^\/t\//.test(window.location.pathname);
if (!isTopicPage) return;
const allowedCategories = ['ask-a-question'];
const topic = Discourse.__container__.lookup("controller:topic");
const categorySlug = topic && topic.get("model.category.slug");
const isAllowedCategory = categorySlug && allowedCategories.includes(categorySlug);
const toggleReactionEmoji = () => {
const emoji = document.querySelector("[data-reaction='frog']");
if (emoji) {
emoji.style.display = isAllowedCategory ? '' : 'none';
console.log(`Emoji with data-reaction='frog' ${isAllowedCategory ? 'shown' : 'hidden'}.`);
}
};
toggleReactionEmoji();
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1) {
const emoji = node.querySelector("[data-reaction='frog']");
if (emoji) {
emoji.style.display = isAllowedCategory ? '' : 'none';
console.log(`Emoji with data-reaction='frog' found in mutation and ${isAllowedCategory ? 'shown' : 'hidden'}.`);
}
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
api.cleanupStream(() => observer.disconnect());
} catch (error) {
console.error("An error occurred in the emoji toggle script:", error);
}
});
</script>