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

Abierto
#573 2 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@Saifjafri212213 ya está trabajando en esto.

Desde el 2/11/2026.

  • #583 de @Saifjafri212213 — abierto

Evaluación

Este issue todavía no se ha evaluado.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
13
Forks
7
Merge medio
3 h 14 min
PR fusionados (30 d)
329

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de codercops/chatcops

Todos los issues de codercops/chatcops

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.