Replies to comments dont trigger live object update

Recently we’ve updated from 6.4.5 to 6.11.0 and the comments are not updated in real time when sending the parentId param.
Captura de pantalla 2023-11-01 a la(s) 7.28.39 p.m.

Code:

const [isSubscribed, setIsSubscribed] = useState(false);
  const {data: comments = [], hasNextPage, loading, onNextPage} = commentsCollection ?? {};

  const subscribeCommentTopic = (user: Amity.User) => {
    if (isSubscribed) return;

    disposers.push(subscribeTopic(getUserTopic(user, SubscriptionLevels.COMMENT)));
    setIsSubscribed(true);
  };
useEffect(() => {
    if (postOwner?.data) {
      const unsubscribe = CommentRepository.getComments(params, data => {
        setCommentsCollection(data);
        subscribeCommentTopic(postOwner.data);
      });

      disposers.push(unsubscribe);

      return () => {
        disposers.forEach(fn => fn());
      };
    }
  }, [postOwner?.data]);
1 Like

Hello, we have forwarded this to our team for further investigation, and we will keep you updated on our progress. :blush::+1:

2 Likes

Hello, we are unable to replicate this issue. Could you please double-check and ensure that the “parentId” is correct?

Also, if you don’t send the “parentId” parameter, does it update correctly? Is this issue occurring with all comments or only some?

Hi! The parentId is correct because if I make another comment the update is triggered and you see the reply to the comment and the new comment. The issue is occurring in all comments.

Hello, thank you for your clarification. Could you please help share more code around getComments(params) ?

Sure, here’s the full hook i’m using

import {CommentRepository, SubscriptionLevels, getUserTopic, subscribeTopic} from '@amityco/ts-sdk';
import {useEffect, useState} from 'react';

const disposers: Amity.Unsubscriber[] = [];
let isSubscribed = false;

const subscribeCommentTopic = (user: Amity.User) => {
  if (isSubscribed) return;

  disposers.push(subscribeTopic(getUserTopic(user, SubscriptionLevels.COMMENT)));
  isSubscribed = true;
};

export function useGetPostParentComments(
  postId: string,
  postOwner: Amity.LiveObject<Amity.User> | undefined,
) {
  const [commentsCollection, setCommentsCollection] =
    useState<Amity.LiveCollection<Amity.Comment<any>>>();
  const [isLoading, setIsLoading] = useState(true);
  const {data: comments = [], hasNextPage, loading, onNextPage} = commentsCollection ?? {};

  const parentComments = comments.filter(comment => !comment.parentId);

  const params: Amity.CommentLiveCollection = {
    referenceType: 'post',
    referenceId: postId,
    limit: 20,
    sortBy: 'firstCreated',
    dataTypes: {
      values: ['text'],
      matchType: 'exact',
    },
  };

  useEffect(() => {
    if (postOwner?.data) {
      const unsubscribe = CommentRepository.getComments(params, data => {
        setCommentsCollection(data);
        subscribeCommentTopic(postOwner.data);
      });

      disposers.push(unsubscribe);

      return () => {
        disposers.forEach(fn => fn());
      };
    }
  }, [postOwner?.data]);

  useEffect(() => {
    if (loading) {
      setIsLoading(true);
    } else if (loading === false) {
      setIsLoading(false);
    }
  }, [loading]);

  const handleLoadMore = () => {
    if (hasNextPage && onNextPage) {
      onNextPage();
    }
  };

  return {parentComments, loading: isLoading, handleLoadMore};
}

1 Like

Thank you, we will look into this :slight_smile:

Hello @RxmvnLD could you try adding the parentId in the params when getComments, please?

Hello!
We use that custom hook on the parent component, on each comment component we use this code to get the responses.

useEffect(() => {
    const getComments = async () => {
      if (children.length > 0) {
        const comments = await CommentRepository.getCommentByIds(children);
        setComments(comments.data);
      } else {
        setComments([]);
      }
    };

    void getComments();
  }, [children]);

Let me try to get the responses with the CommentRepository.getComments to see if the result changes.

1 Like