@chatcops/react: a <ChatCops> component and useChatCops() hook
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
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" inCONTRIBUTING.md:244("private": true,"type": "module", anexportsentry, the build, typecheck and test scripts).@chatcops/widget(workspace:^) andreact(>=18) are peer dependencies. <ChatCops {...props} />renders nothing. It takesWidgetConfig(exported from@chatcops/widget) plus event props:onOpen,onClose,onMessage,onLeadCaptured,onError(the events areWidgetEventsinpackages/widget/src/widget.ts, around line 139).- Load the widget with a dynamic
import('@chatcops/widget')insideuseEffect, so server rendering outputs nothing and touches nowindow. - It wraps the default
ChatCopsobject frompackages/widget/src/index.ts:36:init()on mount anddestroy()on unmount. A prop change thatupdate()accepts (WidgetUpdate: branding, welcome message, suggestions, placeholder, strings, theme,privacyUrl, disclosure) goes throughChatCops.update(). Any other change, such asapiUrl,modeorcontainer, 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 (sendissendMessage). 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, asChatCops.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 notinit(), anapiUrlchange inits again,onLeadCapturedfires and survives re-renders, anduseChatCops().send()reachessendMessage - A server-render test:
renderToString(<ChatCops ... />)returns''and doesn't throw - A
.size-limit.jsonentry: 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:checkpasses) - A changeset;
pnpm test:coverage(85/80 for the new package),pnpm lint,pnpm publintandpnpm api(commit the newapi/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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di codercops/chatcops
-
Widget: a code block tagged `c++`, `c#` or `objective-c` shows part of the tag as codeForse già presa @prakashNalla l’ha presa oggi. Apertabug widget
codercops/chatcops#578 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Docs: Astro, Nuxt and SvelteKit guides for the serverForse già presa @anurag2787 l’ha presa oggi. Apertadocumentation
codercops/chatcops#575 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Server: chatcopsNext(), one catch-all route for Next.jsForse già presa @Saifjafri212213 l’ha presa oggi. Apertaenhancement server
codercops/chatcops#574 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 25/100
codercops/chatcops#546 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 64/100
codercops/chatcops#542 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di codercops/chatcops
Issue simili
-
area: cli bug flake ready-for-agent
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
gazetta-studio/gazetta-studio#1772 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
microsoft/playwright#43875 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
documentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
calagopus/panel#285 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno