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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import {
forwardRef,
useCallback,
useState,
type ForwardRefRenderFunction,
} from 'react';
import { useIntl } from 'react-intl';
import type { Nullable } from '../../../types';
import { Heading, List, ListItem, type ListProps } from '../../atoms';
import {
ApprovedComment,
type ApprovedCommentProps,
PendingComment,
ReplyCommentForm,
type ReplyCommentFormProps,
} from '../comment';
import styles from './comments-list.module.scss';
export type CommentData = Pick<
ApprovedCommentProps,
'author' | 'content' | 'id' | 'publicationDate'
> & {
isApproved: boolean;
replies?: CommentData[];
};
export type CommentsListProps = Omit<
ListProps<true, false>,
| 'children'
| 'hideMarker'
| 'isHierarchical'
| 'isInline'
| 'isOrdered'
| 'onSubmit'
| 'spacing'
> &
Pick<ReplyCommentFormProps, 'onSubmit'> & {
/**
* Should we forbid replies on comments when depth is not exceed?
*
* @default false
*/
areRepliesForbidden?: boolean;
/**
* The comments.
*/
comments: CommentData[];
/**
* A positive integer. When depth is set to `0`, replies are not used.
*
* @default 0
*/
depth?: number;
};
const CommentsListWithRef: ForwardRefRenderFunction<
HTMLOListElement,
CommentsListProps
> = (
{ areRepliesForbidden = false, comments, depth = 0, onSubmit, ...props },
ref
) => {
const [replyingTo, setReplyingTo] = useState<Nullable<number>>(null);
const intl = useIntl();
const toggleReply = useCallback((id: number) => {
setReplyingTo((prevId) => {
if (prevId === id) return null;
return id;
});
}, []);
const getComments = useCallback(
(data: CommentData[], currentDepth = 0) => {
const isLastLevel = depth === currentDepth;
return data.map(({ isApproved, replies, ...comment }) => {
const replyFormHeading = intl.formatMessage(
{
defaultMessage: 'Leave a reply to {name}',
description: 'CommentsList: comment form title',
id: 'c1Ju/q',
},
{ name: comment.author.name }
);
const replyBtnLabel =
replyingTo === comment.id
? intl.formatMessage({
defaultMessage: 'Cancel reply',
description: 'CommentsList: cancel reply button',
id: 'uZj4QI',
})
: intl.formatMessage({
defaultMessage: 'Reply',
description: 'CommentsList: reply button',
id: 'Qa9twM',
});
return (
<ListItem key={comment.id}>
{isApproved ? (
<>
<ApprovedComment
{...comment}
onReply={
isLastLevel || areRepliesForbidden ? undefined : toggleReply
}
replyBtn={
isLastLevel || areRepliesForbidden
? undefined
: replyBtnLabel
}
/>
{replyingTo === comment.id ? (
<ReplyCommentForm
className={styles.reply}
commentId={comment.id}
heading={<Heading level={2}>{replyFormHeading}</Heading>}
onSubmit={onSubmit}
/>
) : null}
{replies?.length && !isLastLevel ? (
<List
hideMarker
isOrdered
// eslint-disable-next-line react/jsx-no-literals
spacing="sm"
>
{getComments(replies, currentDepth + 1)}
</List>
) : null}
</>
) : (
<PendingComment />
)}
</ListItem>
);
});
},
[areRepliesForbidden, depth, intl, onSubmit, replyingTo, toggleReply]
);
return (
<List
{...props}
hideMarker
isOrdered
ref={ref}
// eslint-disable-next-line react/jsx-no-literals
spacing="sm"
>
{getComments(comments)}
</List>
);
};
/**
* CommentsList component
*
* Render a list of comments.
*/
export const CommentsList = forwardRef(CommentsListWithRef);
|