refactor(07-02): extract optimistic message creation helper

Extract createOptimisticMessage helper function:
- Improves code clarity and maintainability
- Centralizes optimistic message structure
- Makes testing easier if needed in future
- All tests still passing

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Nuwan 2026-01-27 08:18:11 +05:30
parent 7729f2767c
commit 2862e87bc6
1 changed files with 21 additions and 7 deletions

View File

@ -89,6 +89,21 @@ const getChannelKey = (message) => {
return message.channel; // 'global'
};
/**
* Helper function to create an optimistic message
* @param {Object} params - Message parameters
* @returns {Object} Optimistic message object
*/
const createOptimisticMessage = ({ optimisticId, userId, userName, message, channel }) => ({
id: optimisticId,
senderId: userId,
senderName: userName,
message,
createdAt: new Date().toISOString(),
channel,
isOptimistic: true
});
const sessionChatSlice = createSlice({
name: 'sessionChat',
initialState,
@ -232,15 +247,14 @@ const sessionChatSlice = createSlice({
state.messagesByChannel[channel] = [];
}
state.messagesByChannel[channel].push({
id: optimisticId,
senderId: userId,
senderName: userName,
const optimisticMessage = createOptimisticMessage({
optimisticId,
userId,
userName,
message,
createdAt: new Date().toISOString(),
channel,
isOptimistic: true
channel
});
state.messagesByChannel[channel].push(optimisticMessage);
})
// sendMessage fulfilled - replace optimistic message
.addCase(sendMessage.fulfilled, (state, action) => {