Skip to content

feat: Add Webchat component and script integration + hCMS - SFS-3084 - #3242

Merged
eduardoformiga merged 7 commits into
devfrom
feat/webchat-approach-3
Mar 24, 2026
Merged

eduardoformiga merged 7 commits into
devfrom
feat/webchat-approach-3

Conversation

@eduardoformiga

@eduardoformiga eduardoformiga commented Mar 17, 2026 •

Copy link
Copy Markdown
Member

What's the purpose of this pull request?

This feature enhances user interaction by allowing a webchat option that can be toggled via hCMS.

Screenshot 2026-03-23 at 17 55 23

How it works?

  • 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.

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

  • 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.

- 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.
@coderabbitai

coderabbitai Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@eduardoformiga has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 10 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: da9ad7e6-5314-4e09-8cf4-131347c9346f

📥 Commits

Reviewing files that changed from the base of the PR and between b7308a8 and 12b6377.

📒 Files selected for processing (2)
  • packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx
  • packages/core/src/components/sections/Webchat/section.module.scss

Walkthrough

Adds a new "Webchat" CMS section and a client-only Webchat component that injects the Weni webchat script via a new useWebchatScript hook; registers the component in the CMS components registry and exposes the hook from the third-party scripts index.

Changes

Cohort / File(s) Summary
Section configuration
packages/core/cms/faststore/sections.json
Added new Webchat section entry with requiredScopes: [] and a schema of type: "object", properties: {} (no configurable properties exposed).
Third-party scripts API
packages/core/src/components/ThirdPartyScripts/index.ts
Exported named useWebchatScript from the ThirdPartyScripts module.
Webchat script hook
packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx
New React hook that injects https://cdn.cloud.weni.ai/agentic-cx.js on client mount, respects DISABLE_3P_SCRIPTS, appends to an optional containerRef or document.head, and cleans up on unmount.
Webchat component & registry
packages/core/src/components/sections/Webchat/Webchat.tsx, packages/core/src/components/sections/Webchat/index.ts, packages/core/src/components/cms/global/Components.ts
Added Webchat section component (client-only container using the hook), re-export index, and registered dynamic Webchat component in the global COMPONENTS registry.
Styling stub
packages/core/src/components/sections/Webchat/section.module.scss
Added SCSS stub (currently commented placeholder).

Sequence Diagram

sequenceDiagram
    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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • ommeirelles
  • lemagnetic

Poem

A tiny script slips into place, unseen,
Hidden divs hum where agents convene,
Webchat wakes on client-side light,
CMS knows the section — ready for flight ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: adding a Webchat component with script integration and hCMS support, accurately reflecting the changeset's primary purpose.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/webchat-approach-3

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

codesandbox-ci Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

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.

@eduardoformiga eduardoformiga changed the title feat: Add Webchat component and script integration feat: Add Webchat component and script integration + hCMS Mar 18, 2026
…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.
@eduardoformiga
eduardoformiga marked this pull request as ready for review March 23, 2026 20:47
@eduardoformiga
eduardoformiga requested a review from a team as a code owner March 23, 2026 20:47
@eduardoformiga
eduardoformiga requested review from gabpaladino and renatomaurovtex and removed request for a team March 23, 2026 20:47
@eduardoformiga eduardoformiga changed the title feat: Add Webchat component and script integration + hCMS feat: Add Webchat component and script integration + hCMS - SFS-3084 Mar 23, 2026
@eduardoformiga
eduardoformiga requested review from lemagnetic and ommeirelles and removed request for gabpaladino March 23, 2026 20:49

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.tsx and vtex.tsx use JSX <script> elements with dangerouslySetInnerHTML and key props, benefiting from Next.js script optimizations and React's deduplication. The current hook uses imperative document.createElement instead.

Note: This differs architecturally since useWebchatScript is 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

📥 Commits

Reviewing files that changed from the base of the PR and between c6e0f65 and 8beba73.

📒 Files selected for processing (7)
  • packages/core/cms/faststore/sections.json
  • packages/core/src/components/ThirdPartyScripts/index.ts
  • packages/core/src/components/ThirdPartyScripts/useWebchatScript.tsx
  • packages/core/src/components/cms/global/Components.ts
  • packages/core/src/components/sections/Webchat/Webchat.tsx
  • packages/core/src/components/sections/Webchat/index.ts
  • packages/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.
@eduardoformiga
eduardoformiga merged commit 04c3fbb into dev Mar 24, 2026
2 checks passed
@eduardoformiga
eduardoformiga deleted the feat/webchat-approach-3 branch March 24, 2026 19:33
eduardoformiga added a commit that referenced this pull request Apr 14, 2026
…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 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants