Communitygithub.com

YepAPI/技能:評論與討論

支援串接式留言、@提及、表情反應、審核佇列、樂觀更新 UI 以及游標分頁。

技能:評論與討論 是什麼?

此技能為 AI 代理(如 Claude Code、Cursor)提供在應用程式中實現評論與討論系統的完整實作模式。它涵蓋了從資料模型、API 端點到前端互動的所有關鍵面向,包括串接式留言(透過 parent_id)、@提及解析與通知、表情反應(確保唯一性約束)、內容審核(含自動標記與審核佇列)、軟刪除、編輯歷史記錄,以及使用游標分頁來高效載入大量評論。技能特別強調樂觀更新 UI,讓使用者能即時看到新增的留言,再與伺服器確認,並在錯誤時自動回滾。支援的平台包括任何使用 React Query 等資料擷取函式庫的前端框架,以及使用關聯式資料庫(如 PostgreSQL、SQLite)的後端。此技能透過提供可直接套用的程式碼片段與明確的規則,幫助開發者快速建構一個正確、可擴展且使用者體驗流暢的評論功能。

相容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/comments-discussions

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

評論與討論

規則

  • 串接式留言:parent_id 外鍵欄位位於 comments 表中 — null 表示頂層留言,引用 parent 則表示回覆
  • 樂觀更新 UI:立即將留言加入 UI,等待伺服器回應確認,若發生錯誤則回滾
  • @提及:在留言內容中解析 @username — 輸入 @ 時觸發使用者查詢下拉選單,並通知被提及的使用者
  • 表情反應:獨立的 reactions 表(commentId, userId, emoji)— 在 (commentId, userId, emoji) 上設定唯一約束
  • 審核機制:status 欄位(published, flagged, removed)— 為被標記的內容設立審核佇列,並可透過關鍵字清單自動標記
  • 軟刪除:設定 deletedAt 時間戳記 — 對於擁有回覆的留言顯示「已刪除」佔位符,完全隱藏沒有回覆的已刪除留言
  • 游標分頁:cursor(commentId)+ limit — 依 createdAt 排序,滾動時載入更早的評論
  • 編輯歷史:將編輯記錄儲存在獨立的資料表或 JSON 陣列中 — 顯示「已編輯」標記以及最後編輯時間戳

模式

// 留言資料模型
// comments: id, postId, authorId, parentId (nullable), body, status, deletedAt, createdAt, updatedAt
// reactions: id, commentId, userId, emoji, createdAt (unique: [commentId, userId, emoji])
// mentions: id, commentId, mentionedUserId, createdAt

// 串接查詢 — 取得頂層留言及其巢狀回覆
const comments = await db.query.comments.findMany({
  where: and(eq(comments.postId, postId), isNull(comments.parentId)),
  with: {
    author: { columns: { id: true, name: true, avatar: true } },
    replies: {
      with: { author: { columns: { id: true, name: true, avatar: true } } },
      orderBy: [asc(comments.createdAt)],
    },
    reactions: true,
  },
  orderBy: [desc(comments.createdAt)],
  limit: 20,
});

// 樂觀新增留言 (React Query)
const addComment = useMutation({
  mutationFn: (body: string) => api.post(`/posts/${postId}/comments`, { body }),
  onMutate: async (body) => {
    await queryClient.cancelQueries({ queryKey: ["comments", postId] });
    const previous = queryClient.getQueryData(["comments", postId]);
    queryClient.setQueryData(["comments", postId], (old: Comment[]) => [
      { id: `temp-${Date.now()}`, body, author: currentUser, createdAt: new Date() },
      ...old,
    ]);
    return { previous };
  },
  onError: (_err, _body, context) => {
    queryClient.setQueryData(["comments", postId], context?.previous);
  },
  onSettled: () => queryClient.invalidateQueries({ queryKey: ["comments", postId] }),
});

應避免

  • 巢狀深度無限制 — 最多 2-3 層,將更深層的回覆扁平化處理
  • 一次載入所有留言 — 使用游標分頁,特別是在活躍的討論串中
  • 僅在客戶端解析提及 — 應在伺服器端驗證並擷取提及內容,以發送通知
  • 硬刪除擁有回覆的留言 — 這會導致討論串孤立;應使用軟刪除並顯示佔位符
  • 反應沒有唯一約束 — 允許同一使用者對同一則留言重複按讚

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Tenant isolation (row-level vs schema), subdomain routing, per-tenant config, and data partitioning.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相關技能