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

未关闭
#573 2 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@Saifjafri212213 已经在做这个了。

开始于 2026年11月2日。

评估

这个 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 小时 15 分钟
30 天内合并 PR
330

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

codercops/chatcops 的其他 Issue

查看 codercops/chatcops 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。