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

Aperta
#573 2 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Saifjafri212213 ci sta già lavorando.

Dal 2/11/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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.

Lingua principale
TypeScript
Stelle
13
Fork
7
Merge medio
3h 15m
PR unite (30g)
330

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di codercops/chatcops

Tutte le issue di codercops/chatcops

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.