user.id => user.user_id1. chatroomUtils 에서 안읽은 메세지 카운트하는 함수 수정
-> 구매자일때, 판매자일때 나누던 부분을 내가 아닌 상대가 보낸 메세지 필터로 통합시킴
-> user.user_id 로 변경
=> 리팩토링시 타입 수정해야함..
export const fetchUnreadCounts = async (
user: any,
chatrooms: any[],
setUnreadCounts: (counts: { [key: string]: number }) => void
) => {
const counts: { [key: string]: number } = {};
for (const chatroom of chatrooms) {
let data, error;
// 내가 읽지 않은 메시지를 가져옴
({ data, error } = await supabase
.from('Message')
.select('is_read', { count: 'exact' })
.eq('message_chatroom_id', chatroom.chatroom_id)
.eq('is_read', false) // 읽지 않은 메시지 필터
.neq('message_user_id', user.user_id)); // 상대방이 보낸 메시지 필터
if (error) {
console.error('읽지 않은 메세지 수 가져오는 중 에러발생', error);
} else if (data) {
counts[chatroom.chatroom_id] = data.length;
}
}
setUnreadCounts(counts);
};
2. 의존성배열에 unReadCounts 추가
// HeaderIconBar
const unreadCounts = unreadCountStore.getState().unreadCounts;
// chatroomList가 업데이트되면 안 읽은 메시지 수를 계산
useEffect(() => {
const updateUnreadCounts = async () => {
if (isLogin && chatroomList.length > 0) {
const setUnreadCounts = unreadCountStore.getState().setUnreadCounts;
await fetchUnreadCounts(userData, chatroomList, setUnreadCounts);
}
};
updateUnreadCounts();
}, [chatroomList, isLogin, userData, unreadCounts]);