Skip to content
Snippets Groups Projects
Commit 0e544620 authored by Lukas's avatar Lukas
Browse files

Update PostComments component to show nested comments

parent 2f9d35ac
No related branches found
No related tags found
1 merge request!510Lbudginas/#411 add post comments
import React from "react";
import { useRouter } from "next/router";
import Hive from "@/types/Hive"; import Hive from "@/types/Hive";
import PostContent from "./PostContent"; import PostContent from "./PostContent";
import usePostContentReplies from "@/hooks/api/postPage/usePostContentReplies";
interface PostCommentsProps { interface NestedCommentProps {
comments: Hive.Content[]; comment: Hive.Content;
} }
const PostComments: React.FC<PostCommentsProps> = ({ comments }) => { const NestedComment: React.FC<NestedCommentProps> = ({ comment }) => {
if (!comments || !comments.length) return null; const shouldFetch = comment.children > 0;
return comments.map((comment) => { const { data: nestedReplies } = usePostContentReplies(
return ( comment.author,
<div comment.permlink,
className="flex mt-4 justify-center"
key={comment.id} {
> enabled: shouldFetch,
<div className="w-[70%]"> }
<PostContent );
active_votes={comment.active_votes}
data={comment} return (
/> <div
</div> className="flex mt-4 justify-end"
key={comment.id}
>
<div className="w-[90%]">
<PostContent
isComment={true}
active_votes={comment.active_votes}
data={comment}
/>
{shouldFetch && nestedReplies && nestedReplies.length > 0
? nestedReplies.map((child) => (
<NestedComment
key={child.id}
comment={child}
/>
))
: null}
</div> </div>
); </div>
}); );
};
const PostComments = () => {
const router = useRouter();
const { post } = router?.query;
let accountName = post?.[1] ?? "";
let permlink = post?.[2] ?? "";
const { data: comments } = usePostContentReplies(accountName, permlink);
if (!comments || !comments.length) {
return null;
}
return (
<>
{comments.map((comment) => (
<NestedComment
key={comment.id}
comment={comment}
/>
))}
</>
);
}; };
export default PostComments; export default PostComments;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment