テーマコンポーネントの作り方、これで合ってますか?

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>

この方法は技術的には可能ですが、理想的なアプローチではありません。

script タグや jQuery の $(document).ready を使用するのではなく、Ember のレンダリングシステムを正しく活用することをお勧めします。

まずは、テーマコンポーネント用のリポジトリを作成し、適切なフォルダ構造を整えることから始めましょう。discourse_theme CLI を使用すると、この構造が自動的に生成され、コンポーネントの開発が容易になります。(あるいは、CLI のその他の機能は不要で、構造だけが必要な場合は、テーマのスケルトン もあります。)

そこから、Discourse が拡張性のために用意している apiInitializerspluginAPI、プラグインアウトレットなどのツールを使用して、目的の機能を実現することをお勧めします。

これらを学ぶ最も良い方法は、Meta 上の開発者ガイド(特にテーマ/テーマコンポーネントのセクション)を参照することです。また、Customization > Theme component カテゴリーの GitHub リポジトリを調べてみるのも良いでしょう。それらのコードや実装方法を参照することで、理解が深まるはずです。

お役に立てれば幸いです!

これが目標であれば、簡単なCSSで実現できます。

body.category-ask-a-question .discourse-reactions-picker.frog {
  display: none;
}