Skip to content

Commit

Permalink
Fix chat message bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kuosmark committed Nov 14, 2024
1 parent a632c58 commit 3b64731
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
25 changes: 11 additions & 14 deletions src/features/Chat/components/ActiveWindow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -71,19 +68,19 @@ const ActiveWindow = () => {
setMessage('');
setIsLoadingSentMessage(false);
}
}, [chat.messages]);
}, [activeChat.messages]);

return (
chat && (
activeChat && (
<Container isTablet={isTablet}>
<Header chat={chat} />
<Header chat={activeChat} />
<MessageList
messageList={chat.messages}
buddyId={chat.buddyId}
status={chat.status}
messageList={activeChat.messages}
buddyId={activeChat.buddyId}
status={activeChat.status}
isLoading={isLoadingBuddyMessages}
/>
{chat.status === 'ok' && (
{activeChat.status === 'ok' && (
<MessageField
handleSend={handleMessageSend}
isInputDisabled={isLoadingSentMessage}
Expand Down
22 changes: 11 additions & 11 deletions src/features/Chat/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ export const selectOngoingChatsExist = createSelector(

export const selectActiveChat = createSelector(
selectChatState,
({ activeChatId, chats }) => (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(
selectChatState,
({ 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,
Expand Down

0 comments on commit 3b64731

Please sign in to comment.