Do not fire SELECTED_CONVERSATION_CHANGED more than once

This commit is contained in:
Josh Perez 2023-01-05 18:34:47 -05:00 committed by GitHub
parent 569b6e14a6
commit 74abe0c1ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 28 deletions

View file

@ -1057,16 +1057,23 @@ function onArchive(
} }
function onUndoArchive( function onUndoArchive(
conversationId: string conversationId: string
): SelectedConversationChangedActionType { ): ThunkAction<
const conversation = window.ConversationController.get(conversationId); void,
if (!conversation) { RootStateType,
throw new Error('onUndoArchive: Conversation not found!'); unknown,
} SelectedConversationChangedActionType
> {
return (dispatch, getState) => {
const conversation = window.ConversationController.get(conversationId);
if (!conversation) {
throw new Error('onUndoArchive: Conversation not found!');
}
conversation.setArchived(false); conversation.setArchived(false);
return showConversation({ showConversation({
conversationId, conversationId,
}); })(dispatch, getState, null);
};
} }
function onMarkUnread(conversationId: string): ShowToastActionType { function onMarkUnread(conversationId: string): ShowToastActionType {
@ -2326,12 +2333,10 @@ function createGroup(
), ),
}, },
}); });
dispatch( showConversation({
showConversation({ conversationId: conversation.id,
conversationId: conversation.id, switchToAssociatedView: true,
switchToAssociatedView: true, })(dispatch, getState, null);
})
);
} catch (err) { } catch (err) {
log.error('Failed to create group', Errors.toLogFormat(err)); log.error('Failed to create group', Errors.toLogFormat(err));
dispatch({ type: 'CREATE_GROUP_REJECTED' }); dispatch({ type: 'CREATE_GROUP_REJECTED' });
@ -3483,14 +3488,27 @@ function showConversation({
conversationId, conversationId,
messageId, messageId,
switchToAssociatedView, switchToAssociatedView,
}: ShowConversationArgsType): SelectedConversationChangedActionType { }: ShowConversationArgsType): ThunkAction<
return { void,
type: SELECTED_CONVERSATION_CHANGED, RootStateType,
payload: { unknown,
conversationId, SelectedConversationChangedActionType
messageId, > {
switchToAssociatedView, return (dispatch, getState) => {
}, const { conversations } = getState();
if (conversationId === conversations.selectedConversationId) {
return;
}
dispatch({
type: SELECTED_CONVERSATION_CHANGED,
payload: {
conversationId,
messageId,
switchToAssociatedView,
},
});
}; };
} }

View file

@ -359,7 +359,13 @@ describe('both/state/ducks/conversations', () => {
const state = { const state = {
...getEmptyState(), ...getEmptyState(),
}; };
const action = showConversation({ conversationId: 'abc123' }); const dispatch = sinon.spy();
showConversation({ conversationId: 'abc123' })(
dispatch,
getEmptyRootState,
null
);
const action = dispatch.getCall(0).args[0];
const nextState = reducer(state, action); const nextState = reducer(state, action);
assert.equal(nextState.selectedConversationId, 'abc123'); assert.equal(nextState.selectedConversationId, 'abc123');
@ -370,10 +376,12 @@ describe('both/state/ducks/conversations', () => {
const state = { const state = {
...getEmptyState(), ...getEmptyState(),
}; };
const action = showConversation({ const dispatch = sinon.spy();
showConversation({
conversationId: 'abc123', conversationId: 'abc123',
messageId: 'xyz987', messageId: 'xyz987',
}); })(dispatch, getEmptyRootState, null);
const action = dispatch.getCall(0).args[0];
const nextState = reducer(state, action); const nextState = reducer(state, action);
assert.equal(nextState.selectedConversationId, 'abc123'); assert.equal(nextState.selectedConversationId, 'abc123');
@ -384,10 +392,12 @@ describe('both/state/ducks/conversations', () => {
let action: SelectedConversationChangedActionType; let action: SelectedConversationChangedActionType;
beforeEach(() => { beforeEach(() => {
action = showConversation({ const dispatch = sinon.spy();
showConversation({
conversationId: 'fake-conversation-id', conversationId: 'fake-conversation-id',
switchToAssociatedView: true, switchToAssociatedView: true,
}); })(dispatch, getEmptyRootState, null);
[action] = dispatch.getCall(0).args;
}); });
it('shows the inbox if the conversation is not archived', () => { it('shows the inbox if the conversation is not archived', () => {