From 0e86cd3c972e54cf700cca65bb2493e84056276c Mon Sep 17 00:00:00 2001
From: Ashm Walia <ashmwalia@outlook.com>
Date: Fri, 29 Jan 2021 10:55:14 -0800
Subject: kind of works

---
 src/components/comments/CommentsContainer.tsx | 75 ++++++++++++++++++---------
 1 file changed, 51 insertions(+), 24 deletions(-)

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

diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx
index d8134caf..d2f11b96 100644
--- a/src/components/comments/CommentsContainer.tsx
+++ b/src/components/comments/CommentsContainer.tsx
@@ -1,16 +1,17 @@
 import React, {useEffect, useRef, useState} from 'react';
 import {StyleSheet} from 'react-native';
-import {ScrollView} from 'react-native-gesture-handler';
+import {FlatList} from 'react-native-gesture-handler';
 import {useDispatch, useSelector} from 'react-redux';
 import {CommentTile} from '.';
 import {getComments} from '../../services';
 import {RootState} from '../../store/rootReducer';
 import {CommentType, ScreenType, TypeOfComment} from '../../types';
+import {SCREEN_HEIGHT} from '../../utils';
 export type CommentsContainerProps = {
   screenType: ScreenType;
-
   //objectId can be either moment_id or comment_id
   objectId: string;
+  commentId?: string;
   setCommentsLength?: (count: number) => void;
   newCommentsAvailable: boolean;
   setNewCommentsAvailable: (value: boolean) => void;
@@ -32,18 +33,21 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
   typeOfComment,
   setCommentObjectInFocus,
   commentObjectInFocus,
+  commentId,
 }) => {
   const {username: loggedInUsername} = useSelector(
     (state: RootState) => state.user.user,
   );
   const [commentsList, setCommentsList] = useState<CommentType[]>([]);
   const dispatch = useDispatch();
-  const ref = useRef<ScrollView>(null);
+  const ref = useRef<FlatList<CommentType>>(null);
+  const [initialIndex, setInitialIndex] = useState<number>(0);
 
   useEffect(() => {
     //Scroll only if a new comment and not a reply was posted
     const shouldScroll = () =>
-      typeOfComment === 'Comment' && !commentObjectInFocus;
+      (typeOfComment === 'Comment' && !commentObjectInFocus) ||
+      typeOfComment === 'Thread';
     const loadComments = async () => {
       const comments = await getComments(objectId, typeOfComment === 'Thread');
       setCommentsList(comments);
@@ -55,9 +59,15 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
     if (newCommentsAvailable) {
       loadComments();
       if (shouldScroll()) {
-        setTimeout(() => {
-          ref.current?.scrollToEnd();
-        }, 500);
+        if (commentId) {
+          const index = commentsList.findIndex(
+            (item) => item.comment_id === commentId,
+          );
+          setInitialIndex(index);
+        } else {
+          setInitialIndex(commentsList.length - 1);
+          ref.current?.scrollToEnd({animated: true});
+        }
       }
     }
   }, [
@@ -68,27 +78,44 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
     setCommentsLength,
     typeOfComment,
     commentObjectInFocus,
+    commentId,
+    commentsList,
   ]);
 
+  const ITEM_HEIGHT = SCREEN_HEIGHT / 7.5;
+
+  const renderComment = ({item}: {item: CommentType}) => (
+    <CommentTile
+      key={item.comment_id}
+      comment_object={item}
+      screenType={screenType}
+      typeOfComment={typeOfComment}
+      setCommentObjectInFocus={setCommentObjectInFocus}
+      newCommentsAvailable={newCommentsAvailable}
+      setNewCommentsAvailable={setNewCommentsAvailable}
+      canDelete={item.commenter.username === loggedInUsername}
+    />
+  );
+
   return (
-    <ScrollView
+    <FlatList
+      data={commentsList}
       ref={ref}
-      style={styles.scrollView}
-      contentContainerStyle={styles.scrollViewContent}>
-      {commentsList &&
-        commentsList.map((comment: CommentType) => (
-          <CommentTile
-            key={comment.comment_id}
-            comment_object={comment}
-            screenType={screenType}
-            typeOfComment={typeOfComment}
-            setCommentObjectInFocus={setCommentObjectInFocus}
-            newCommentsAvailable={newCommentsAvailable}
-            setNewCommentsAvailable={setNewCommentsAvailable}
-            canDelete={comment.commenter.username === loggedInUsername}
-          />
-        ))}
-    </ScrollView>
+      keyExtractor={(item, index) => index.toString()}
+      decelerationRate={'fast'}
+      snapToAlignment={'start'}
+      snapToInterval={ITEM_HEIGHT}
+      renderItem={renderComment}
+      showsVerticalScrollIndicator={false}
+      initialScrollIndex={initialIndex}
+      contentContainerStyle={styles.scrollViewContent}
+      getItemLayout={(data, index) => ({
+        length: ITEM_HEIGHT,
+        offset: ITEM_HEIGHT * index,
+        index,
+      })}
+      pagingEnabled
+    />
   );
 };
 
-- 
cgit v1.2.3-70-g09d2

