Ho esperienza nella creazione di soluzioni personalizzate con altre piattaforme/framework e voglio capire se questo è il modo corretto per creare un componente tema in Discourse.
Sembra funzionare, ma non significa necessariamente che sia il modo corretto.
In breve, questo dovrebbe nascondere la reazione a seconda della categoria in cui si trova un argomento. È il modo giusto per farlo?
<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>