@chatcops/react: a <ChatCops> component and useChatCops() hook
维护者通常 1 天内回复
@Saifjafri212213 已经在做这个了。
开始于 2026年11月2日。
评估
这个 Issue 还没有评估数据。
描述
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.
- 主要语言
- TypeScript
- 星标
- 13
- 派生
- 7
- 平均合并
- 3 小时 15 分钟
- 30 天内合并 PR
- 330
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
codercops/chatcops 的其他 Issue
-
Widget: a code block tagged `c++`, `c#` or `objective-c` shows part of the tag as code可能已有人在做 @prakashNalla 今天认领。 未关闭bug widget
codercops/chatcops#578 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Docs: Astro, Nuxt and SvelteKit guides for the server可能已有人在做 @anurag2787 今天认领。 未关闭documentation
codercops/chatcops#575 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Server: chatcopsNext(), one catch-all route for Next.js可能已有人在做 @Saifjafri212213 今天认领。 未关闭enhancement server
codercops/chatcops#574 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 25/100
codercops/chatcops#546 · 5 条评论 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 64/100
codercops/chatcops#542 · 3 条评论 ·
维护者通常 1 天内回复
查看 codercops/chatcops 的全部 Issue
相似的 Issue
-
area: cli bug flake ready-for-agent
难度 2/5 1-3 小时 新手友好度 62/100
gazetta-studio/gazetta-studio#1772 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
-
needs-triage
难度 2/5 1-3 小时 新手友好度 66/100
microsoft/playwright#43875 · 1 条评论 ·
维护者通常 1 天内回复
-
documentation good first issue
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
feature
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复