refactor(07-03): add JSDoc comments to CHAT_MESSAGE handler

- Add comprehensive JSDoc to getChannelKeyFromMessage helper
- Document channel key mapping logic (session, lesson, global)
- Add JSDoc to CHAT_MESSAGE handler with unread increment rules
- Extract shouldIncrementUnread for clarity
- All tests still passing after refactoring

Part of Phase 7 Plan 3 (WebSocket Integration & Selectors)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Nuwan 2026-01-27 12:41:27 +05:30
parent 6c712bba57
commit 7a05f6e453
1 changed files with 27 additions and 6 deletions

View File

@ -35,9 +35,17 @@ import {
} from '../store/features/sessionChatSlice';
/**
* Helper function to get channel key from chat message
* Session messages use sessionId directly, lesson uses lessonSessionId, global uses 'global'
* Helper function to get channel key from WebSocket chat message
*
* Channel keys map Protocol Buffer channel types to Redux state keys:
* - Session messages: Use session_id directly
* - Lesson messages: Use lesson_session_id directly
* - Global messages: Use 'global' literal
*
* @param {Object} message - WebSocket message with channel and ID fields
* @param {string} message.channel - Channel type ('session', 'lesson', or 'global')
* @param {string} [message.session_id] - Session ID (for session channel)
* @param {string} [message.lesson_session_id] - Lesson session ID (for lesson channel)
* @returns {string} Channel key for Redux state
*/
const getChannelKeyFromMessage = (message) => {
@ -187,7 +195,19 @@ export const useSessionWebSocket = (sessionId) => {
}
},
// Phase 7 Plan 3: Handle CHAT_MESSAGE from WebSocket
/**
* Phase 7 Plan 3: Handle CHAT_MESSAGE from WebSocket
*
* Transforms Protocol Buffer message format to Redux format and handles
* unread count increment based on chat window state.
*
* Unread increment logic:
* - Increment if window is closed
* - Increment if window is open but viewing a different channel
* - Do NOT increment if window is open and viewing the same channel
*
* @param {Object} message - Protocol Buffer formatted chat message
*/
CHAT_MESSAGE: (message) => {
console.log('Chat message received:', message);
@ -206,12 +226,13 @@ export const useSessionWebSocket = (sessionId) => {
claimedRecording: message.claimed_recording || null
};
// Add message to Redux
// Add message to Redux state
dispatch(addMessageFromWebSocket(chatMessage));
// Increment unread count if window closed or different channel active
// Increment unread count if window closed or viewing different channel
const messageChannelKey = getChannelKeyFromMessage(message);
if (!chatState.isWindowOpen || chatState.activeChannel !== messageChannelKey) {
const shouldIncrementUnread = !chatState.isWindowOpen || chatState.activeChannel !== messageChannelKey;
if (shouldIncrementUnread) {
dispatch(incrementUnreadCount({ channel: messageChannelKey }));
}
},