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:
parent
7729f2767c
commit
2862e87bc6
|
|
@ -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) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue