Skip to content

Conversation

@OpenStaxClaude
Copy link

@OpenStaxClaude OpenStaxClaude commented Dec 22, 2025

Summary

Refactors the monolithic HelpMenu/hooks.ts file into three focused modules following SOLID principles, particularly Single Responsibility Principle.

Changes

New Module Structure

1. fieldMapping.ts - Salesforce Field Mapping

  • Maps application field names to Salesforce hidden/visible fields
  • Handles name parsing (full name → first/last name)
  • Determines field editability based on data source
  • Synchronized with assignments/packages/frontend/src/components/SupportInfo.tsx

2. businessHours.ts - Business Hours Management

  • Tracks current business hours with automatic timeout management
  • Formats hours for display using Intl.DateTimeFormat with fallback
  • Handles grace periods for better user experience
  • Smart state comparison to avoid unnecessary re-renders

3. chatController.ts - Chat Popup Control

  • Opens and positions popup window at bottom-right of screen
  • Manages secure postMessage communication with origin validation
  • Polls for popup closure and handles cleanup
  • Lifecycle management for popup window

Backward Compatibility

The main hooks.ts file now serves as a clean re-export layer, maintaining full backward compatibility. All existing imports will continue to work without changes.

Benefits

  • Single Responsibility: Each module has one clear purpose
  • Maintainability: Easier to understand and modify individual pieces
  • Testability: Focused modules are easier to unit test
  • Documentation: Comprehensive comments explain WHY decisions were made
  • Navigation: Better code organization for developers

Code Quality

  • Added extensive JSDoc comments explaining:
    • What each function does
    • Why certain decisions were made (e.g., grace periods, security checks)
    • Expected inputs/outputs with examples
  • Extracted magic numbers to constants with descriptive names
  • Improved function names for clarity (e.g., calculateBottomRightPosition)

Testing

  • No functional changes - this is a pure refactoring
  • All exports maintained through re-exports
  • Existing tests should pass without modification

CORE-1450

🤖 Generated with Claude Code

Splits the monolithic hooks.ts file into three focused modules following
SOLID principles, particularly Single Responsibility Principle.

**New Module Structure:**

1. **fieldMapping.ts** - Handles all Salesforce field mapping logic
   - Maps application fields to Salesforce hidden/visible fields
   - Parses user names into first/last name components
   - Determines field editability based on data source
   - Extensively commented to explain mapping decisions

2. **businessHours.ts** - Manages business hours tracking and formatting
   - Tracks current business hours with automatic timeout management
   - Formats hours for display using Intl.DateTimeFormat
   - Handles grace periods for better UX
   - Comments explain timeout logic and state management

3. **chatController.ts** - Controls chat popup window and messaging
   - Opens/positions popup window at bottom-right
   - Manages postMessage communication with security checks
   - Polls for popup closure and handles cleanup
   - Comments explain security considerations and lifecycle

**Benefits:**
- Each module has a single, clear responsibility
- Easier to test individual components
- Better code organization and navigation
- Comprehensive comments explain WHY decisions were made
- hooks.ts now serves as a clean re-export layer for backward compatibility

Fixes CORE-1450

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
RoyEJohnson and others added 2 commits December 22, 2025 15:32
Added tests for two uncovered scenarios in chatController.ts:
- Line 154: Handles popup blocking when window.open returns null
- Line 164: Ignores postMessage events from sources other than the opened popup window

These tests improve the security and robustness of the chat controller by ensuring:
1. The controller gracefully handles popup blockers without throwing errors
2. PostMessage communication is validated to only accept messages from the intended popup window

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Add test coverage for chatController and businessHours branches

- Add test for line 175 else branch in chatController.ts (when popup is NOT closed)
- Add test for when nextState is undefined (not in business hours)
- Add test for state reuse when hours haven't changed (object reference stability)

These tests address the coverage gaps identified in code review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Update hooks.spec.tsx

Add comprehensive test coverage for HelpMenu/index.tsx

Added tests to cover previously untested code paths:
- contactFormUrl memoization with URL encoding
- showIframe state management and iframe rendering
- PutAway button click handler
- Message event listener registration and cleanup
- Special character encoding in contact form parameters
- NewTabIcon component export

All tests pass and provide complete coverage for the component.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Add test coverage for businessHours line 90 else branch

Added test case to cover the scenario where business hours change,
ensuring the hook returns a new object reference when hours are
different (testing the else branch that returns nextState).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Added tests to achieve 100% coverage of index.tsx:
- Test for NewTabIcon SVG rendering with correct attributes
- Test for CONTACT_FORM_SUBMITTED message event closing iframe
- Test for custom children rendering in help menu
- Test for chatConfig memoization behavior
- Test for undefined chatConfig graceful handling
- Test for openChat callback invocation when Chat With Us is clicked

All tests pass with 100% statement, branch, function, and line coverage.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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.

3 participants