@chatcops/react: a <ChatCops> component and useChatCops() hook
メンテナーはふだん 1 日以内に返信
評価
この 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時間 14分
- マージ済み PR(30日)
- 329
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
codercops/chatcops のほかの issue
-
fix(core): Cohere and Voyage rerankers bypass non-positive limit check on NaN topN and send invalid null payload対応中かも @bhushan4work が今日担当しました。 オープンbug
codercops/chatcops#582 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Widget: a code block tagged `c++`, `c#` or `objective-c` shows part of the tag as code対応中かも @prakashNalla が 1 日前に担当しました。 オープンbug widget
codercops/chatcops#578 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Docs: Astro, Nuxt and SvelteKit guides for the server対応中かも @anurag2787 が 1 日前に担当しました。 オープンdocumentation
codercops/chatcops#575 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Server: chatcopsNext(), one catch-all route for Next.js対応中かも @Saifjafri212213 が 1 日前に担当しました。 オープンenhancement server
codercops/chatcops#574 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 25/100
codercops/chatcops#546 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
codercops/chatcops の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
-
bug needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
DSpace/dspace-angular#6511 ·
メンテナーはふだん 1 日以内に返信
-
area: frontend area:byok bug severity:low
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
Phone app shows Unknown for a remote machine whose key changed対応中かも @janicduplessis が今日担当しました。 オープンarea: mobile priority: P3 type: bug
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
appandflow/stim#5006 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Area - Local to Cloud Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
microsoft/vscode-mssql#23209 ·
メンテナーはふだん 1 日以内に返信