blob: af0152ac044cd056d69a3e17ce1b743f06669338 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import type { FC } from 'react';
import type { SingleComment } from '../../../types';
import { List, ListItem } from '../../atoms';
import { UserComment, type UserCommentProps } from './comment';
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
export type CommentsListDepth = 0 | 1 | 2 | 3 | 4;
export type CommentsListProps = Pick<
UserCommentProps,
'Notice' | 'saveComment'
> & {
/**
* An array of comments.
*/
comments: SingleComment[];
/**
* The maximum depth. Use `0` to not display nested comments.
*/
depth: CommentsListDepth;
};
/**
* CommentsList component
*
* Render a comments list.
*/
export const CommentsList: FC<CommentsListProps> = ({
comments,
depth,
Notice,
saveComment,
}) => {
/**
* Get each comment wrapped in a list item.
*
* @param {SingleComment[]} commentsList - An array of comments.
* @returns {JSX.Element[]} The list items.
*/
const getItems = (
commentsList: SingleComment[],
startLevel: number
): JSX.Element[] => {
const isLastLevel = startLevel === depth;
return commentsList.map(({ replies, ...comment }) => (
<ListItem key={comment.id}>
<UserComment
canReply={!isLastLevel}
Notice={Notice}
saveComment={saveComment}
{...comment}
/>
{replies.length && !isLastLevel ? (
<List hideMarker isOrdered spacing="sm">
{getItems(replies, startLevel + 1)}
</List>
) : null}
</ListItem>
));
};
return (
<List hideMarker isOrdered spacing="sm">
{getItems(comments, 0)}
</List>
);
};
|