Repository navigation
feat: Add Webchat component and script integration + hCMS - SFS-3084 - #3242
Conversation
- Introduced a new Webchat section with a corresponding component and styles. - Implemented `useWebchatScript` to manage the webchat script injection based on URL parameters and localStorage preferences. - Updated `Components.ts` to include the new Webchat component dynamically. - Added necessary JSON schema for the Webchat section in sections.json. This feature enhances user interaction by allowing a webchat option that can be toggled via URL parameters.
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughAdds a new "Webchat" CMS section and a client-only Webchat component that injects the Weni webchat script via a new Changes
Sequence DiagramsequenceDiagram
participant Browser as Browser
participant WebchatComp as Webchat Component
participant Hook as useWebchatScript Hook
participant DOM as Document (Head / Container)
participant CDN as Weni CDN
Browser->>WebchatComp: Mount Webchat Section
WebchatComp->>WebchatComp: create containerRef (useRef)
WebchatComp->>Hook: call useWebchatScript(containerRef)
Hook->>Hook: useEffect runs on mount
Hook->>Hook: check DISABLE_3P_SCRIPTS and window (skip SSR)
alt 3P scripts enabled & client
Hook->>DOM: create <script> element (src=https://cdn.cloud.weni.ai/agentic-cx.js)
Hook->>DOM: append to containerRef.current or document.head
DOM->>CDN: fetch agentic-cx.js
CDN-->>DOM: script loaded/executed
end
WebchatComp->>DOM: render hidden container div
DOM-->>Browser: Webchat initialized
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
…oves loading performance.
…ema description - Removed URL parameter handling and localStorage logic from `useWebchatScript`, focusing on direct script injection. - Updated the description in sections.json for the Webchat schema to clarify loading behavior.
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
packages/core/src/components/sections/Webchat/Webchat.tsx (1)
9-18: Consider adding explicit return type for TypeScript clarity.The component works correctly, but adding an explicit return type improves documentation and catches potential issues.
💡 Add return type annotation
-const Webchat = () => { +const Webchat = (): JSX.Element => { const scriptContainerRef = useRef<HTMLDivElement>(null) useWebchatScript(scriptContainerRef)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/components/sections/Webchat/Webchat.tsx` around lines 9 - 18, The Webchat component is missing an explicit TypeScript return type; update the Webchat function signature (function/component named Webchat) to include a JSX.Element (or React.FC) return type to improve documentation and type-checking so the component explicitly returns JSX.Element and helps catch future typing issues.packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx (1)
1-27: Consider converting to a JSX-based script component or documenting the imperative approach rationale.
GoogleTagManager.tsxandvtex.tsxuse JSX<script>elements withdangerouslySetInnerHTMLandkeyprops, benefiting from Next.js script optimizations and React's deduplication. The current hook uses imperativedocument.createElementinstead.Note: This differs architecturally since
useWebchatScriptis a hook that injects into a specific container (rather than always the head), which would require refactoring to a wrapper component to align with the established pattern. If keeping the imperative approach, document why container-specific injection is necessary for the webchat SDK.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx` around lines 1 - 27, Replace the imperative hook approach with a small JSX wrapper component (e.g. WebchatScript) instead of useWebchatScript: create a component that accepts a containerRef (or containerId) and returns a <script key="weni-webchat" src={WEBCHAT_SCRIPT_URL} /> inserted into the target container (use ReactDOM.createPortal to render into a non-head container) so React can dedupe and Next.js/React optimizations apply; if you choose to keep the hook, add a clear JSDoc on useWebchatScript explaining why imperative DOM insertion into a specific containerRef is required for the webchat SDK (referencing WEBCHAT_SCRIPT_URL and containerRef) and note the tradeoffs versus a JSX script component.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/core/src/components/sections/Webchat/section.module.scss`:
- Around line 1-4: The imported stylesheet exports no .section class so
styles.section is undefined; either restore the rule in section.module.scss by
uncommenting the .section selector (so styles.section exists) or remove the
unused import / className usage in Webchat.tsx (remove reference to
styles.section and the stylesheet import). Locate the import of
section.module.scss and the usage of styles.section in Webchat.tsx to apply one
of these two fixes.
In `@packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx`:
- Around line 14-26: The useEffect in useWebchatScript references
containerRef?.current and injects a script without cleanup or deduplication;
update the effect to include containerRef in the dependency array, remove the
redundant typeof window check (useEffect runs only on client), and add a cleanup
that removes the injected script (or avoids injecting if a script with
WEBCHAT_SCRIPT_URL already exists) when the component unmounts or containerRef
changes; ensure you still respect process.env.DISABLE_3P_SCRIPTS and operate on
the script element identified by WEBCHAT_SCRIPT_URL so duplicate injections are
prevented and the script is removed on teardown.
---
Nitpick comments:
In `@packages/core/src/components/sections/Webchat/Webchat.tsx`:
- Around line 9-18: The Webchat component is missing an explicit TypeScript
return type; update the Webchat function signature (function/component named
Webchat) to include a JSX.Element (or React.FC) return type to improve
documentation and type-checking so the component explicitly returns JSX.Element
and helps catch future typing issues.
In `@packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx`:
- Around line 1-27: Replace the imperative hook approach with a small JSX
wrapper component (e.g. WebchatScript) instead of useWebchatScript: create a
component that accepts a containerRef (or containerId) and returns a <script
key="weni-webchat" src={WEBCHAT_SCRIPT_URL} /> inserted into the target
container (use ReactDOM.createPortal to render into a non-head container) so
React can dedupe and Next.js/React optimizations apply; if you choose to keep
the hook, add a clear JSDoc on useWebchatScript explaining why imperative DOM
insertion into a specific containerRef is required for the webchat SDK
(referencing WEBCHAT_SCRIPT_URL and containerRef) and note the tradeoffs versus
a JSX script component.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: a2c4ae67-1172-4513-932b-8cd350f7dc29
📒 Files selected for processing (7)
packages/core/cms/faststore/sections.jsonpackages/core/src/components/ThirdPartyScripts/index.tspackages/core/src/components/ThirdPartyScripts/useWebchatScript.tsxpackages/core/src/components/cms/global/Components.tspackages/core/src/components/sections/Webchat/Webchat.tsxpackages/core/src/components/sections/Webchat/index.tspackages/core/src/components/sections/Webchat/section.module.scss
- Eliminated the check for `typeof window` in `useWebchatScript`, streamlining the script injection process for improved performance.
- Added a cleanup function to remove the webchat script when the component using `useWebchatScript` unmounts, preventing potential memory leaks and ensuring proper resource management.
- Modified the cleanup effect in `useWebchatScript` to depend on `containerRef`, ensuring that the script is properly removed when the reference changes, enhancing resource management.
- Updated the SCSS for the Webchat section to include a relative positioning style, enhancing layout control for the component.
…3242) This feature enhances user interaction by allowing a webchat option that can be toggled via hCMS. <img width="600" alt="Screenshot 2026-03-23 at 17 55 23" src="https://github.com/user-attachments/assets/31f05a33-5eb9-428b-85bb-590b1037f55f" /> - Introduced a new Webchat section with a corresponding component and styles. - Implemented `useWebchatScript` to manage the webchat script injection. - Updated `Components.ts` to include the new Webchat component dynamically. - Added necessary JSON schema for the Webchat section in sections.json. Run locally and see the webchat component added from hCMS to the code. TBD - https://docs.google.com/document/d/11nyKPMYjcb87xow1Igc_iy8uHyp9bK1H0CBN7u5FerM/edit?tab=t.0#heading=h.f90kuwq3iwh6 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> * **New Features** * Introduced a new Webchat widget section to the content management system, enabling integration of webchat functionality into pages. The widget is fully registered and ready to be configured and deployed alongside other page sections. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
What's the purpose of this pull request?
This feature enhances user interaction by allowing a webchat option that can be toggled via hCMS.
How it works?
useWebchatScriptto manage the webchat script injection.Components.tsto include the new Webchat component dynamically.How to test it?
Run locally and see the webchat component added from hCMS to the code.
Starters Deploy Preview
TBD
References
Summary by CodeRabbit