사용자 및 카테고리 ID에 따라 Composer에 사용자 지정 메시지를 추가하는 컴포넌트

오늘 저는 이런 문제에 직면했습니다:

하지만 당분간, 팀이 이 문제를 해결하기까지(기대하기는 하지만) Composer에 커스텀 메시지를 표시하여 태그를 추가해야 한다는 것을 상기시켜 주는 Component를 만드는 데 Claude의 도움을 요청하기로 결정했습니다. ID를 기반으로 한 명 이상의 사용자를 추가할 수 있고, ID를 기반으로 특정 카테고리로 제한할 수도 있습니다.

ID(사용자 및 카테고리)가 목록에 없으면 다음을 확인합니다:

ID가 목록에 있으면 메시지를 확인합니다:

이것이 다른 분들에게 도움이 된다면, 여기 있습니다 (단, Component를 생성하고 스크립트를 JS 탭에 추가하기만 하면 됩니다):

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;
  });
});
3개의 좋아요