@chatcops/react: a <ChatCops> component and useChatCops() hook

オープン
#573 コメント 2 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Saifjafri212213 がすでに取り組んでいます。

2026年11月2日 から。

  • #583 @Saifjafri212213 による — オープン

評価

この issue はまだ評価されていません。

説明

enhancement widget

F-12 tasks 2 to 4. A React or Next.js site can use the widget today with useEffect and ChatCops.init(), but everyone writes the same glue, and gets StrictMode or unmounting wrong. This is a small @chatcops/react package that does it once.

'use client';
import { ChatCops, useChatCops } from '@chatcops/react';

export default function Layout({ children }) {
  return (
    <>
      {children}
      <ChatCops apiUrl="/api/chat" branding={{ name: 'Acme' }} onLeadCaptured={(lead) => track('lead', lead)} />
    </>
  );
}

function AskButton() {
  const chat = useChatCops();
  return <button onClick={() => chat.send('Is this in stock?')}>Ask about this</button>;
}

What to do

  • A new packages/react. Follow "A new package needs" in CONTRIBUTING.md:244 ("private": true, "type": "module", an exports entry, the build, typecheck and test scripts). @chatcops/widget (workspace:^) and react (>=18) are peer dependencies.
  • <ChatCops {...props} /> renders nothing. It takes WidgetConfig (exported from @chatcops/widget) plus event props: onOpen, onClose, onMessage, onLeadCaptured, onError (the events are WidgetEvents in packages/widget/src/widget.ts, around line 139).
  • Load the widget with a dynamic import('@chatcops/widget') inside useEffect, so server rendering outputs nothing and touches no window.
  • It wraps the default ChatCops object from packages/widget/src/index.ts:36: init() on mount and destroy() on unmount. A prop change that update() accepts (WidgetUpdate: branding, welcome message, suggestions, placeholder, strings, theme, privacyUrl, disclosure) goes through ChatCops.update(). Any other change, such as apiUrl, mode or container, destroys and inits again.
  • Event props: subscribe once with ChatCops.on(), which returns an unsubscribe function (index.ts:124), and keep the latest callback in a ref, so a new arrow function on each render doesn't resubscribe.
  • useChatCops() returns { open, close, toggle, send, update }, calling the same object (send is sendMessage). Before the widget has loaded, those calls do nothing.
  • StrictMode runs effects twice in development. Mount, unmount and mount must leave exactly one widget and one set of listeners.
  • One <ChatCops> per page. Say so in the docs. A second one replaces the first, as ChatCops.init() does.

Acceptance criteria

  • Tests with React Testing Library: it mounts one widget, StrictMode leaves one, unmounting destroys it and removes the listeners, a branding change calls update() and not init(), an apiUrl change inits again, onLeadCaptured fires and survives re-renders, and useChatCops().send() reaches sendMessage
  • A server-render test: renderToString(<ChatCops ... />) returns '' and doesn't throw
  • A .size-limit.json entry: the package's own code under 1 kB gzipped, with the widget left out
  • A "React" page under Widget in the docs, in website/astro.config.mjs (pnpm docs:check passes)
  • A changeset; pnpm test:coverage (85/80 for the new package), pnpm lint, pnpm publint and pnpm api (commit the new api/react.api.md) pass

The tools prop from the F-12 design waits for page tools (F-08), so leave it out.

Comment on this issue to be assigned.

主要言語
TypeScript
スター
13
フォーク
7
平均マージ
3時間 14分
マージ済み PR(30日)
329

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

codercops/chatcops のほかの issue

codercops/chatcops の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。