Component to add a custom message to the Composer based on user and categories ID

Today I was facing this issue:

but for now, until the team works on it (hopefully), I decided to ask Claude for some help building a Component that allows me to have a custom message in the Composer to remind me to add a tag. I can add one or more users, by ID, and I can limit it to certain categories, also by ID.

If the ID (user and categories) is NOT on the list, I see this:

If the ID is on the list, I see the message:

If this is helpful to others, here it is (just create a component and add the script to the JS tab):

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer("0.8.31", (api) => {
  // Configuration: Add your user IDs and category IDs here
  const TARGET_USER_IDS = [2]; // Replace with actual user IDs
  const TARGET_CATEGORY_IDS = [4,49]; // Replace with actual category IDs
  const REMINDER_MESSAGE = "🛑 Add the appropriate tag to this post!";
  
  let previousCategoryId = null;
  let checkInterval = null;
  
  function checkAndUpdateMessage() {
    const composer = api.container.lookup("controller:composer");
    if (!composer || !composer.model) {
      // Composer is gone, stop checking
      if (checkInterval) {
        clearInterval(checkInterval);
        checkInterval = null;
      }
      return;
    }
    
    const currentUser = api.getCurrentUser();
    if (!currentUser || !TARGET_USER_IDS.includes(currentUser.id)) return;
    
    const model = composer.model;
    const categoryId = model.categoryId;
    
    // Only act if category has changed
    if (categoryId === previousCategoryId) return;
    previousCategoryId = categoryId;
    
    const currentReply = model.reply || "";
    
    // Remove message if it was added
    if (currentReply.startsWith(REMINDER_MESSAGE)) {
      model.set("reply", currentReply.replace(REMINDER_MESSAGE, "").trim());
    }
    
    // Add message if in target category and composer is empty
    if (categoryId && TARGET_CATEGORY_IDS.includes(categoryId)) {
      const cleanReply = model.reply || "";
      if (cleanReply.trim().length === 0) {
        model.set("reply", REMINDER_MESSAGE);
      }
    }
  }
  
  // Check on composer open
  api.onAppEvent("composer:opened", () => {
    previousCategoryId = null;
    checkAndUpdateMessage();
    
    // Start polling for category changes
    if (checkInterval) clearInterval(checkInterval);
    checkInterval = setInterval(checkAndUpdateMessage, 300);
  });
  
  // Stop checking when composer closes
  api.onAppEvent("composer:closed", () => {
    if (checkInterval) {
      clearInterval(checkInterval);
      checkInterval = null;
    }
    previousCategoryId = null;
  });
});
1 Like