diff options
author | Ivan Chen <ivan@tagg.id> | 2021-05-07 18:48:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-07 18:48:51 -0400 |
commit | fa51a3c6894028828679c551027ec232ed8203b1 (patch) | |
tree | b55ece7257c868f3cb9eeae4a70fb7b220040fb5 /src/components/comments/CommentsContainer.tsx | |
parent | 893e7bf7a49eb612a975dddae4d792a035b9f420 (diff) | |
parent | 82bfc01d95275ebe4a63695326b3b775807c2408 (diff) |
Merge pull request #404 from IvanIFChen/hotfix-mentions-bugfixes
[HOTFIX] Mentions Bugfixes
Diffstat (limited to 'src/components/comments/CommentsContainer.tsx')
-rw-r--r-- | src/components/comments/CommentsContainer.tsx | 47 |
1 files changed, 17 insertions, 30 deletions
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 |