From a6ada7b34da599d3edb797d6bd9fce57ded7bac5 Mon Sep 17 00:00:00 2001
From: Ivan Chen <ivan@tagg.id>
Date: Thu, 6 May 2021 18:15:15 -0400
Subject: fixed freeze issue

---
 src/components/comments/CommentsContainer.tsx | 47 ++++++++++-----------------
 1 file changed, 17 insertions(+), 30 deletions(-)

(limited to 'src/components/comments/CommentsContainer.tsx')

diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx
index d5d02a92..cd9ecb02 100644
--- a/src/components/comments/CommentsContainer.tsx
+++ b/src/components/comments/CommentsContainer.tsx
@@ -39,6 +39,7 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
   const [commentsList, setCommentsList] = useState<CommentType[]>([]);
   const dispatch = useDispatch();
   const ref = useRef<FlatList<CommentType>>(null);
+  const ITEM_HEIGHT = SCREEN_HEIGHT / 7.0;
 
   useEffect(() => {
     const loadComments = async () => {
@@ -61,22 +62,24 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
     };
   }, [shouldUpdate]);
 
+  // scrolls to the comment
   useEffect(() => {
-    const performAction = () => {
-      if (commentId) {
-        swapCommentTo(commentId, 0);
-      } else if (!isThread && !commentTapped) {
-        setTimeout(() => {
-          ref.current?.scrollToEnd({animated: true});
-        }, 500);
+    if (commentId) {
+      const index = commentsList.findIndex(
+        (item) => item.comment_id === commentId,
+      );
+      if (index > 0) {
+        let comments = [...commentsList];
+        const temp = comments[index];
+        comments[index] = comments[0];
+        comments[0] = temp;
+        setCommentsList(comments);
       }
-    };
-    if (commentsList) {
-      //Bring the relevant comment to top if a comment id is present else scroll if necessary
-      performAction();
+    } else if (!isThread && !commentTapped) {
+      setTimeout(() => {
+        ref.current?.scrollToEnd({animated: true});
+      }, 500);
     }
-
-    //Clean up the reply id present in store
     return () => {
       if (commentId && isThread) {
         setTimeout(() => {
@@ -84,23 +87,7 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
         }, 200);
       }
     };
-  }, [commentsList, commentId]);
-
-  // eslint-disable-next-line no-shadow
-  const swapCommentTo = (commentId: string, toIndex: number) => {
-    const index = commentsList.findIndex(
-      (item) => item.comment_id === commentId,
-    );
-    if (index > 0) {
-      let comments = [...commentsList];
-      const temp = comments[index];
-      comments[index] = comments[toIndex];
-      comments[toIndex] = temp;
-      setCommentsList(comments);
-    }
-  };
-
-  const ITEM_HEIGHT = SCREEN_HEIGHT / 7.0;
+  }, [commentId]);
 
   const renderComment = ({item}: {item: CommentType | CommentThreadType}) => (
     <CommentTile
-- 
cgit v1.2.3-70-g09d2

