AgoraIO-Usecase / AgoraChat-UIKit-web

MIT License
5 stars 8 forks source link

incorrect unreadCount displayed for the unread messages. #70

Open nomanshahzad opened 2 weeks ago

nomanshahzad commented 2 weeks ago

I'm trying to display the unread message count accurately in the conversation list. However, if I set the unreadCount prop to undefined or null, the count appears as one less than the actual value and sometime it shows a random value. Additionally, when this happens, the username or nickname is not displayed, and instead, the user ID is shown. Could anyone suggest any workarounds or solutions for this?

Below is the code snippet:


import React, { useState, useEffect } from 'react';
import { UIKitProvider, Chat, ConversationList, useClient, rootStore, ConversationItem, Avatar } from 'agora-chat-uikit';
useEffect(() => {
    const getContacts = async () => {
      if (isLoggedIn) {
        try {
          const response = await axios.get(
            `https://${agora_host}/${agora_org_name}/${agora_app_name}/users/${user_id}/contacts/users`,
            {
              headers: {
                Authorization: `Bearer ${user_token}`,
              },
            }
          );
          const contacts = response.data.data;

          const attributes = await axios.post(
            `https://${agora_host}/${agora_org_name}/${agora_app_name}/metadata/user/get`,
            {
              properties: ["avatar", "ext", "nickname"],
              targets: contacts
            },
            {
              headers: {
                'Authorization': `Bearer ${app_token}`, // Replace {YourAppToken} with your actual token
                'Content-Type': 'application/json'
              }
            }
          );
          const contactsAttributes = attributes.data.data;
          console.log(contactsAttributes)

          for (const contact of contacts) {
            const name = contactsAttributes[contact].nickname
            rootStore.conversationStore.addConversation({
              chatType: "singleChat",
              conversationId: contact,
              name,
              avatarUrl: "https://picsum.photos/200/300",
              lastMessage: {},
              unreadCount: ""
            });
          }
          console.log('contactsAttributes', contactsAttributes)
          setContacts(contacts);
        } catch (error) {
          console.error('Failed to fetch contacts:', error);
        }
      }
    };

    getContacts();
  }, [isLoggedIn, user_id, user_token, app_token, agora_host, agora_org_name, agora_app_name]);
myZhangDong commented 1 week ago

Uikit internally retrieves a conversation list from the Agora server, which contains unread data. The addConversation method is suitable for creating a new conversation