diff --git a/app/frontend/src/components/Sidebar/Contents/Chatting/ChatList.tsx b/app/frontend/src/components/Sidebar/Contents/Chatting/ChatList.tsx index 248770542..aa083f7c2 100644 --- a/app/frontend/src/components/Sidebar/Contents/Chatting/ChatList.tsx +++ b/app/frontend/src/components/Sidebar/Contents/Chatting/ChatList.tsx @@ -74,8 +74,8 @@ export function ChatList({ return chatItem.messageType === 'talk' ? ( { - if (!userData) { - return; - } - - return () => leaveRoom(userData.id); - }, [userData, leaveRoom]); + useEffect(() => () => leaveRoom(), [leaveRoom]); if (!userData) { return ( diff --git a/app/frontend/src/components/UserChip/index.css.ts b/app/frontend/src/components/UserChip/index.css.ts index 3c6830952..97f644316 100644 --- a/app/frontend/src/components/UserChip/index.css.ts +++ b/app/frontend/src/components/UserChip/index.css.ts @@ -35,3 +35,7 @@ export const profileImage = style({ borderRadius: '50%', overflow: 'hidden', }); + +export const unknown = style({ + color: vars.color.grayscale200, +}); diff --git a/app/frontend/src/components/UserChip/index.tsx b/app/frontend/src/components/UserChip/index.tsx index 6ded92b04..f038577a1 100644 --- a/app/frontend/src/components/UserChip/index.tsx +++ b/app/frontend/src/components/UserChip/index.tsx @@ -4,7 +4,7 @@ import { vars } from '@/styles'; import * as styles from './index.css'; type UserChipProps = { - username: string; + username?: string; profileSrc?: string; }; @@ -22,7 +22,9 @@ export function UserChip({ username, profileSrc }: UserChipProps) { )} - {username} + + {username || '(알 수 없음)'} + ); } diff --git a/app/frontend/src/hooks/useChatting.ts b/app/frontend/src/hooks/useChatting.ts index 9a9cd94fc..07f21f187 100644 --- a/app/frontend/src/hooks/useChatting.ts +++ b/app/frontend/src/hooks/useChatting.ts @@ -5,14 +5,18 @@ import { ChatMessage, StatusType, } from '@morak/chat/src/interface/message.interface'; +import { useQueryClient } from '@tanstack/react-query'; import { URL } from '@/constants'; +import { queryKeys } from '@/queries'; const socketClient = new SocketClient(URL.SOCKET, URL.SOCKET_PATH); export function useChatting(postId: string) { const [chatItems, setChatItems] = useState([]); + const userIdRef = useRef(null); const lastDateRef = useRef(undefined); + const queryClient = useQueryClient(); const sendMessage = (message: string, userId: string) => { socketClient.sendMessage({ @@ -82,29 +86,46 @@ export function useChatting(postId: string) { () => socketClient.subscribeToChat( (status: StatusType, msgs: ChatMessage[]) => { - if (status === 200) { - setChatItems((items) => [...items, ...msgs]); + if (status !== 200) { + return; + } + + setChatItems((items) => [...items, ...msgs]); + if (msgs.length === 1 && msgs[0].messageType === 'notification') { + queryClient.invalidateQueries({ + queryKey: queryKeys.mogaco.detail(postId).queryKey, + }); } }, ), - [], + [postId, queryClient], ); const joinRoom = useCallback( - (userId: string, callback?: () => void) => + (userId: string, callback?: () => void) => { + if (userIdRef.current) { + return; + } + + userIdRef.current = userId; socketClient.joinRoom({ user: userId, room: postId }, (status) => { if (status === 200) { subscribeToChat(); callback?.(); } - }), + }); + }, [postId, subscribeToChat], ); - const leaveRoom = useCallback( - (userId: string) => socketClient.leaveRoom({ user: userId, room: postId }), - [postId], - ); + const leaveRoom = useCallback(() => { + if (!userIdRef.current) { + return; + } + + socketClient.leaveRoom({ user: userIdRef.current, room: postId }); + userIdRef.current = null; + }, [postId]); const resetChatItems = useCallback(() => { setChatItems([]);