diff --git a/app/soapbox/features/chats/components/chat_list.js b/app/soapbox/features/chats/components/chat_list.js index 84fd34244..7cd03401d 100644 --- a/app/soapbox/features/chats/components/chat_list.js +++ b/app/soapbox/features/chats/components/chat_list.js @@ -7,10 +7,21 @@ import { fetchChats } from 'soapbox/actions/chats'; import ChatListAccount from './chat_list_account'; import { makeGetChat } from 'soapbox/selectors'; +const chatDateComparator = (chatA, chatB) => { + // Sort most recently updated chats at the top + const a = new Date(chatA.get('updated_at')); + const b = new Date(chatB.get('updated_at')); + + if (a === b) return 0; + if (a > b) return -1; + if (a < b) return 1; + return 0; +}; + const mapStateToProps = state => { const getChat = makeGetChat(); return { - chats: state.get('chats').map(chat => getChat(state, chat.toJS())).toList(), + chats: state.get('chats').map(chat => getChat(state, chat.toJS())).toList().sort(chatDateComparator), }; }; diff --git a/app/soapbox/reducers/chats.js b/app/soapbox/reducers/chats.js index 6c85bfe7b..7659e38e7 100644 --- a/app/soapbox/reducers/chats.js +++ b/app/soapbox/reducers/chats.js @@ -8,27 +8,16 @@ const importChat = (state, chat) => state.set(chat.id, fromJS(normalizeChat(chat const importChats = (state, chats) => state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat))); -const chatDateComparator = (chatA, chatB) => { - // Sort most recently updated chats at the top - const a = new Date(chatA.get('updated_at')); - const b = new Date(chatB.get('updated_at')); - - if (a === b) return 0; - if (a > b) return -1; - if (a < b) return 1; - return 0; -}; - const initialState = ImmutableMap(); export default function chats(state = initialState, action) { switch(action.type) { case CHATS_FETCH_SUCCESS: - return importChats(state, action.chats).sort(chatDateComparator); + return importChats(state, action.chats); case STREAMING_CHAT_UPDATE: - return importChats(state, [action.chat]).sort(chatDateComparator); + return importChats(state, [action.chat]); case CHAT_FETCH_SUCCESS: - return importChats(state, [action.chat]).sort(chatDateComparator); + return importChats(state, [action.chat]); default: return state; }