diff --git a/src/features/Chat/components/ActiveWindow/index.tsx b/src/features/Chat/components/ActiveWindow/index.tsx index 3763fe65..c6230cea 100644 --- a/src/features/Chat/components/ActiveWindow/index.tsx +++ b/src/features/Chat/components/ActiveWindow/index.tsx @@ -4,7 +4,6 @@ import styled, { css } from 'styled-components'; import { selectActiveChat, selectIsLoadingBuddyMessages, - selectDefaultChat, } from '@/features/Chat/selectors'; import { selectUserId } from '@/features/Authentication/selectors'; import { setActiveChat } from '@/features/Chat/chatSlice'; @@ -35,18 +34,16 @@ const ActiveWindow = () => { const userId = useAppSelector(selectUserId); const activeChat = useAppSelector(selectActiveChat); - const defaultChat = useAppSelector(selectDefaultChat); - const chat = activeChat ?? defaultChat; useEffect(() => { - dispatch(setActiveChat(chat.buddyId)); + dispatch(setActiveChat(activeChat.buddyId)); setMessage(''); - }, [chat.buddyId]); + }, [activeChat.buddyId]); const [message, setMessage] = useState(''); const isLoadingBuddyMessages = useAppSelector( - selectIsLoadingBuddyMessages(chat?.buddyId), + selectIsLoadingBuddyMessages(activeChat?.buddyId), ); const [isLoadingSentMessage, setIsLoadingSentMessage] = useState(false); const isSendingDisabled = isLoadingSentMessage || message.trim().length < 1; @@ -58,7 +55,7 @@ const ActiveWindow = () => { setIsLoadingSentMessage(true); await sendMessage({ userId, - message: toSendMessage(chat.buddyId, userId, message), + message: toSendMessage(activeChat.buddyId, userId, message), }).unwrap(); } catch (error) { setIsLoadingSentMessage(false); @@ -71,19 +68,19 @@ const ActiveWindow = () => { setMessage(''); setIsLoadingSentMessage(false); } - }, [chat.messages]); + }, [activeChat.messages]); return ( - chat && ( + activeChat && ( -
+
- {chat.status === 'ok' && ( + {activeChat.status === 'ok' && ( (activeChatId ? chats[activeChatId] : null), + ({ activeChatId, chats }) => { + if (activeChatId) return chats[activeChatId]; + + // Returns most recent unread chat, or the most recent if all are read + const unreadChats = Object.values(chats).filter(chat => { + if (chat.status !== 'ok') return false; + return chat.messages.some(message => !message.opened); + }); + + return unreadChats[0] ?? chats[0]; + }, ); export const selectActiveChatExists = createSelector( @@ -76,16 +86,6 @@ export const selectActiveChatExists = createSelector( ({ activeChatId, chats }) => Boolean(activeChatId && chats[activeChatId]), ); -// Returns most recent unread chat, or the most recent if all are read -export const selectDefaultChat = createSelector(selectChats, chats => { - const unreadChats = chats.filter(chat => { - if (chat.status !== 'ok') return false; - return chat.messages.some(message => !message.opened); - }); - - return unreadChats[0] ?? chats[0]; -}); - export const selectBuddyMessages = (buddyId: string) => createSelector( selectChatState,