Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(user-state): receive agent stateChange event #350

Merged
merged 4 commits into from
Dec 23, 2024

Conversation

mkesavan13
Copy link
Contributor

@mkesavan13 mkesavan13 commented Dec 20, 2024

COMPLETES SPARK-584812

This pull request addresses

Receiving emitted State Change Event for agent:stateChange

by making the following changes

  • Updating helper to listen to the newly emit event agent:stateChange and updating the current State accordingly

The following scenarios were tested

  • Tested by logging in as supervisor and changing to Available State and Meeting State
  • Tested by logging in as supervisor and changing to Custom State created from the Admin portal
agent.state.receive.react.sample.mov
agent.state.receive.wc.sample.mov

Summary by CodeRabbit

  • New Features

    • Introduced a new constant AGENT_STATE_CHANGE for managing agent state changes.
    • Updated event handling in the user state management to respond to agent state changes.
  • Bug Fixes

    • Improved mock functionality in tests to simulate event handling for the user state hook.
  • Documentation

    • Updated mock implementations in tests to reflect changes in the structure of the cc object.

Copy link

coderabbitai bot commented Dec 20, 2024

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request introduces changes to the contact center user state management, focusing on agent state tracking. A new constant AGENT_STATE_CHANGE is added to handle state change events. The useUserState hook is updated to listen for agent state changes, specifically tracking auxiliary code updates. The changes involve modifying the user state helper, updating tests, and bumping the Webex dependency version.

Changes

File Change Summary
packages/contact-center/store/package.json Upgraded webex dependency from 3.6.0-wxcc.5 to 3.7.0-wxcc.3
packages/contact-center/user-state/src/constants.ts Added new constant AGENT_STATE_CHANGE = 'agent:stateChange'
packages/contact-center/user-state/src/helper.ts Added event listener for AGENT_STATE_CHANGE, updating agent state and elapsed time
packages/contact-center/user-state/tests/helper.ts Updated mock cc object to include on method for event handling
packages/contact-center/user-state/tests/user-state/index.tsx Modified mock implementation of cc object in tests

Sequence Diagram

sequenceDiagram
    participant UserState as User State Hook
    participant CC as Contact Center
    participant Agent as Agent State

    CC ->> UserState: emit 'agent:stateChange' event
    UserState ->> UserState: Check event data
    alt Valid State Change
        UserState ->> Agent: Update auxiliary code
        UserState ->> UserState: Reset elapsed time
    else Invalid State
        UserState ->> UserState: Ignore event
    end
Loading

Poem

🐰 A Rabbit's Ode to State Changes 🐰

In the contact center's digital dance,
Events now whisper with a new stance
Aux codes flutter, time resets clean
Agent states shift across the screen
A melody of code, precise and bright! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mkesavan13
Copy link
Contributor Author

@coderabbitai review

Copy link

coderabbitai bot commented Dec 20, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

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 (1)
packages/contact-center/user-state/tests/user-state/index.tsx (1)

22-24: Consider enhancing test coverage for event handling.

While the test verifies the basic structure, consider adding test cases to verify:

  • Event handler registration with correct event name
  • Callback behavior when state changes occur
  • Error handling scenarios

Example enhancement:

it('registers agent state change handler correctly', () => {
  const mockCC = {
    on: jest.fn()
  };
  
  render(<UserState />);
  
  expect(mockCC.on).toHaveBeenCalledWith(
    'agent:stateChange',
    expect.any(Function)
  );
  
  // Test callback behavior
  const callback = mockCC.on.mock.calls[0][1];
  callback({ newState: 'Available' });
  // Add assertions for state update
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 31ebfb1 and 535b142.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (5)
  • packages/contact-center/store/package.json (1 hunks)
  • packages/contact-center/user-state/src/constants.ts (1 hunks)
  • packages/contact-center/user-state/src/helper.ts (2 hunks)
  • packages/contact-center/user-state/tests/helper.ts (2 hunks)
  • packages/contact-center/user-state/tests/user-state/index.tsx (2 hunks)
🧰 Additional context used
📓 Learnings (1)
packages/contact-center/user-state/src/helper.ts (1)
Learnt from: mkesavan13
PR: webex/widgets#347
File: packages/contact-center/user-state/src/helper.ts:10-19
Timestamp: 2024-12-06T01:07:12.444Z
Learning: In `packages/contact-center/user-state/src/helper.ts`, within the `useUserState` hook, the timer reset in the `useEffect` hook is handled during the state change success. Therefore, modifying the dependency array is unnecessary.
🔇 Additional comments (7)
packages/contact-center/user-state/src/constants.ts (1)

1-1: LGTM! Well-structured event constant.

The constant follows good practices with:

  • Clear, descriptive naming
  • Proper event namespacing using 'agent:' prefix
  • Consistent with the PR objective of handling agent state changes
packages/contact-center/user-state/tests/user-state/index.tsx (1)

9-11: LGTM! Mock implementation matches new functionality.

The mock correctly implements the cc.on event handler needed for state change events.

packages/contact-center/store/package.json (1)

25-25: Verify compatibility with new Webex version.

The upgrade from 3.6.0-wxcc.5 to 3.7.0-wxcc.3 is a minor version bump. Please ensure:

  • The new version supports the agent state change events
  • No breaking changes are introduced
  • All dependent features are tested with this version
packages/contact-center/user-state/src/helper.ts (3)

2-2: LGTM!

The import of the AGENT_STATE_CHANGE constant is properly added and used consistently.


19-28: LGTM: Timer reset integration

The integration with the existing timer reset logic is correct, aligning with the learnings from PR #347 where timer reset is handled during state change success.


20-20: Verify the AgentStateChangeSuccess event type string

Let's verify that 'AgentStateChangeSuccess' is the correct event type string expected by the contact center SDK.

packages/contact-center/user-state/tests/helper.ts (1)

6-7: LGTM!

The mockCC object is properly updated to include the event listener method.

Comment on lines 19 to 27
cc.on(AGENT_STATE_CHANGE, (data) => {
if (data && typeof data === 'object' && data.type === 'AgentStateChangeSuccess') {
const DEFAULT_CODE = '0'; // Default code when no aux code is present
setCurrentState({
id: data.auxCodeId?.trim() !== '' ? data.auxCodeId : DEFAULT_CODE
});
setElapsedTime(0);
}
});
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add cleanup for the event listener to prevent memory leaks

The event listener is not being cleaned up when the component unmounts, which could lead to memory leaks and stale state updates.

Add the cleanup in the useEffect's return function:

 useEffect(() => {
   setElapsedTime(0);
   const timer = setInterval(() => {
     setElapsedTime(prevTime => prevTime + 1);
   }, 1000);

-  cc.on(AGENT_STATE_CHANGE, (data) => {
+  const handleStateChange = (data) => {
     if (data && typeof data === 'object' && data.type === 'AgentStateChangeSuccess') {
       const DEFAULT_CODE = '0'; // Default code when no aux code is present
       setCurrentState({
         id: data.auxCodeId?.trim() !== '' ? data.auxCodeId : DEFAULT_CODE
       });
       setElapsedTime(0);
     }
-  });
+  };
+  
+  cc.on(AGENT_STATE_CHANGE, handleStateChange);

   return () => {
     clearInterval(timer);
+    cc.off(AGENT_STATE_CHANGE, handleStateChange);
   };
 }, []);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cc.on(AGENT_STATE_CHANGE, (data) => {
if (data && typeof data === 'object' && data.type === 'AgentStateChangeSuccess') {
const DEFAULT_CODE = '0'; // Default code when no aux code is present
setCurrentState({
id: data.auxCodeId?.trim() !== '' ? data.auxCodeId : DEFAULT_CODE
});
setElapsedTime(0);
}
});
const handleStateChange = (data) => {
if (data && typeof data === 'object' && data.type === 'AgentStateChangeSuccess') {
const DEFAULT_CODE = '0'; // Default code when no aux code is present
setCurrentState({
id: data.auxCodeId?.trim() !== '' ? data.auxCodeId : DEFAULT_CODE
});
setElapsedTime(0);
}
};
cc.on(AGENT_STATE_CHANGE, handleStateChange);

Copy link
Contributor

@sreenara sreenara left a comment

Choose a reason for hiding this comment

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

Have a few nitpick comments. In the UT, we need to ensure we are checking number of times called too.

@@ -1,4 +1,6 @@
import {useState, useEffect} from "react";
// TODO: Export & Import this AGENT_STATE_CHANGE constant from SDK
import { AGENT_STATE_CHANGE } from './constants';
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. This import statement doesn't seem to follow the same linting as the previous line?

@@ -15,8 +17,23 @@ export const useUserState = ({idleCodes, agentId, cc}) => {
setElapsedTime(prevTime => prevTime + 1);
}, 1000);

const handleStateChange = (data) => {
if (data && typeof data === 'object' && data.type === 'AgentStateChangeSuccess') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is typeof data required? All data are objects no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just to ensure that data.type doesn't throw an error just in case the data received is not an object

// Cleanup the timer on component unmount
return () => clearInterval(timer);
return () => {
clearInterval(timer);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. It's also beneficial to set timer = null to ensure there is no leak of timers. This is a recommendation I've seen before.

@@ -17,7 +20,10 @@ describe('UserState Component', () => {

render(<UserState/>);

expect(useUserStateSpy).toHaveBeenCalledWith({cc: {}, idleCodes: [], agentId: 'testAgentId'});
expect(useUserStateSpy).toHaveBeenCalledWith({cc: {
Copy link
Contributor

Choose a reason for hiding this comment

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

comment. Can we add a condition for number of times called too? It should only be called once..

@mkesavan13 mkesavan13 merged commit 21d6ce7 into webex:ccwidgets Dec 23, 2024
2 checks passed
@sreenara
Copy link
Contributor

🎉 This PR is included in version 1.28.0-ccwidgets.5 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

rsarika pushed a commit to rsarika/widgets that referenced this pull request Jan 2, 2025
# [1.28.0-ccwidgets.5](webex/widgets@v1.28.0-ccwidgets.4...v1.28.0-ccwidgets.5) (2024-12-23)

### Bug Fixes

* **user-state:** receive agent stateChange event ([webex#350](webex#350)) ([21d6ce7](webex@21d6ce7))
@sreenara
Copy link
Contributor

sreenara commented Feb 5, 2025

🎉 This PR is included in version 1.28.0-ccconnectors.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@mkesavan13 mkesavan13 deleted the agentStateEvent branch February 5, 2025 12:33
mkesavan13 pushed a commit to mkesavan13/widgets that referenced this pull request Feb 7, 2025
# [1.28.0-ccconnectors.1](webex/widgets@v1.27.5...v1.28.0-ccconnectors.1) (2025-02-05)

### Bug Fixes

* add material to all components ([webex#376](webex#376)) ([de0ca28](webex@de0ca28))
* **bundling:** fixed the multiple React instance and useRef errors  ([webex#355](webex#355)) ([473cd4f](webex@473cd4f))
* **call-control:** add-call-control-widget ([webex#362](webex#362)) ([a677f5e](webex@a677f5e))
* **cc-station-login:** material design ui ([webex#377](webex#377)) ([aec7034](webex@aec7034))
* **cc-store:** receive webex on init, add store types ([webex#341](webex#341)) ([9648969](webex@9648969))
* **cc-widgets:** ship-all-widgets-together ([webex#345](webex#345)) ([83d5a37](webex@83d5a37))
* cleanup and using mobx-react-lite ([webex#356](webex#356)) ([0b304c4](webex@0b304c4))
* convert npm to yarn ([webex#234](webex#234)) ([432ed3c](webex@432ed3c))
* **release:** add-publish-step-in-tooling ([webex#334](webex#334)) ([ca32235](webex@ca32235))
* rename agent state to user state ([webex#361](webex#361)) ([fe409db](webex@fe409db))
* **samples:** change samples index html hrefs ([webex#367](webex#367)) ([ff126ab](webex@ff126ab))
* **user-state:** receive agent stateChange event ([webex#350](webex#350)) ([21d6ce7](webex@21d6ce7))

### Features

* **cc-components:** setup and move user state sample ui comp ([webex#359](webex#359)) ([16a44d0](webex@16a44d0))
* **cc-store:** add logger from sdk ([webex#354](webex#354)) ([a62494b](webex@a62494b))
* **cc-widgets:** added Agent-Multi-Login-Alert Feature ([webex#364](webex#364)) ([f7d75ca](webex@f7d75ca))
* **release:** add new branch to circleci ([18f7bec](webex@18f7bec))
* **release:** publish pipeline for wxcc widgets ([webex#324](webex#324)) ([864fb52](webex@864fb52))
* taskList and IncomingTask widgets added ([webex#348](webex#348)) ([ce3a619](webex@ce3a619))
* **user-state:** load and change state, client timer ([webex#347](webex#347)) ([f1ccaeb](webex@f1ccaeb))
* **widget-cc-station-login:** Spark 575845 login widget ([webex#239](webex#239)) ([66b8a20](webex@66b8a20))
* **widgets:** added-relogin-logic ([webex#357](webex#357)) ([94dd415](webex@94dd415))
* **widgets:** shifted-timer-to-worker ([webex#352](webex#352)) ([c06fe9c](webex@c06fe9c))
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants