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>