-
Notifications
You must be signed in to change notification settings - Fork 355
/
CommentContainer.tsx
659 lines (620 loc) · 20.9 KB
/
CommentContainer.tsx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import { Localized } from "@fluent/react/compat";
import cn from "classnames";
import { EventEmitter2 } from "eventemitter2";
import { clearLongTimeout, LongTimeout, setLongTimeout } from "long-settimeout";
import React, {
FunctionComponent,
MouseEvent,
useCallback,
useEffect,
useState,
} from "react";
import { graphql } from "react-relay";
import { isBeforeDate } from "coral-common/utils";
import { getURLWithCommentID } from "coral-framework/helpers";
import { useToggleState } from "coral-framework/hooks";
import { withContext } from "coral-framework/lib/bootstrap";
import withFragmentContainer from "coral-framework/lib/relay/withFragmentContainer";
import {
GQLFEATURE_FLAG,
GQLSTORY_MODE,
GQLTAG,
GQLUSER_STATUS,
} from "coral-framework/schema";
import { PropTypesOf } from "coral-framework/types";
import CLASSES from "coral-stream/classes";
import {
ShowEditFormEvent,
ShowReplyFormEvent,
ViewConversationEvent,
} from "coral-stream/events";
import {
SetCommentIDMutation,
ShowAuthPopupMutation,
withSetCommentIDMutation,
withShowAuthPopupMutation,
} from "coral-stream/mutations";
import { Ability, can } from "coral-stream/permissions";
import { Button, Flex, HorizontalGutter, Icon } from "coral-ui/components/v2";
import MatchMedia from "coral-ui/components/v2/MatchMedia";
import { CommentContainer_comment as CommentData } from "coral-stream/__generated__/CommentContainer_comment.graphql";
import { CommentContainer_settings as SettingsData } from "coral-stream/__generated__/CommentContainer_settings.graphql";
import { CommentContainer_story as StoryData } from "coral-stream/__generated__/CommentContainer_story.graphql";
import { CommentContainer_viewer as ViewerData } from "coral-stream/__generated__/CommentContainer_viewer.graphql";
import { isPublished } from "../helpers";
import AnsweredTag from "./AnsweredTag";
import UserBadgesContainer, { authorHasBadges } from "./AuthorBadgesContainer";
import ButtonsBar from "./ButtonsBar";
import EditCommentFormContainer from "./EditCommentForm";
import FeaturedTag from "./FeaturedTag";
import IndentedComment from "./IndentedComment";
import MediaSectionContainer from "./MediaSection/MediaSectionContainer";
import CaretContainer, {
RejectedTombstoneContainer,
} from "./ModerationDropdown";
import PermalinkButtonContainer from "./PermalinkButton";
import ReactionButtonContainer from "./ReactionButton";
import ReplyButton from "./ReplyButton";
import ReplyCommentFormContainer from "./ReplyCommentForm";
import ReportFlowContainer, { ReportButton } from "./ReportFlow";
import ShowConversationLink from "./ShowConversationLink";
import { UsernameContainer, UsernameWithPopoverContainer } from "./Username";
import UserTagsContainer, { commentHasTags } from "./UserTagsContainer";
import styles from "./CommentContainer.css";
interface Props {
viewer: ViewerData | null;
comment: CommentData;
story: StoryData;
settings: SettingsData;
eventEmitter: EventEmitter2;
indentLevel?: number;
showAuthPopup: ShowAuthPopupMutation;
setCommentID: SetCommentIDMutation;
/**
* localReply will integrate the mutation response into
* localReplies
*/
localReply?: boolean;
/** disableReplies will remove the ReplyButton */
disableReplies?: boolean;
/** showConversationLink will render a link to the conversation */
showConversationLink?: boolean;
highlight?: boolean;
className?: string;
hideAnsweredTag?: boolean;
hideReportButton?: boolean;
hideModerationCarat?: boolean;
onRemoveAnswered?: () => void;
collapsed?: boolean;
toggleCollapsed?: () => void;
}
export const CommentContainer: FunctionComponent<Props> = ({
className,
collapsed,
comment,
disableReplies,
hideAnsweredTag,
hideModerationCarat,
highlight,
indentLevel,
localReply,
onRemoveAnswered,
settings,
showConversationLink,
hideReportButton,
story,
toggleCollapsed,
eventEmitter,
setCommentID,
viewer,
showAuthPopup,
}) => {
const [showReplyDialog, setShowReplyDialog] = useState(false);
const [
showEditDialog,
setShowEditDialog,
toggleShowEditDialog,
] = useToggleState(false);
const [showReportFlow, , toggleShowReportFlow] = useToggleState(false);
const handleShowConversation = useCallback(
(e: MouseEvent) => {
ViewConversationEvent.emit(eventEmitter, {
commentID: comment.id,
from: "COMMENT_STREAM",
});
// Prevent the event from acting.
e.preventDefault();
// If the feature for read more new tab is enabled, then redirect the
// user.
if (settings.featureFlags.includes(GQLFEATURE_FLAG.READ_MORE_NEW_TAB)) {
const url = getURLWithCommentID(story.url, comment.id);
window.open(url, "_blank", "noreferrer");
} else {
void setCommentID({ id: comment.id });
}
// Return false to prevent the navigation from occuring.
return false;
},
[eventEmitter, comment.id, settings.featureFlags, story.url, setCommentID]
);
const isLoggedIn = !!viewer;
const openEditDialog = useCallback(() => {
if (isLoggedIn) {
ShowEditFormEvent.emit(eventEmitter, {
commentID: comment.id,
});
setShowEditDialog(true);
} else {
void showAuthPopup({ view: "SIGN_IN" });
}
}, [isLoggedIn, eventEmitter, comment.id, setShowEditDialog, showAuthPopup]);
const toggleShowReplyDialog = useCallback(() => {
if (isLoggedIn) {
if (!showReplyDialog) {
ShowReplyFormEvent.emit(eventEmitter, {
commentID: comment.id,
});
}
setShowReplyDialog((v) => !v);
} else {
void showAuthPopup({ view: "SIGN_IN" });
}
}, [
isLoggedIn,
showReplyDialog,
setShowReplyDialog,
showAuthPopup,
eventEmitter,
comment,
]);
const isViewerBanned = !!viewer?.status.current.includes(
GQLUSER_STATUS.BANNED
);
const isViewerSuspended = !!viewer?.status.current.includes(
GQLUSER_STATUS.SUSPENDED
);
const isViewerScheduledForDeletion = !!viewer?.scheduledDeletionDate;
const isViewerComment =
// Can't edit a comment if you aren't logged in!
!!viewer &&
// Can't edit a comment if there isn't an author on it!
!!comment.author &&
// Can't edit a comment if the comment isn't the viewers!
viewer.id === comment.author.id;
// The editable initial value is the result of the the funtion here.
const [editable, setEditable] = useState(() => {
// Can't edit a comment that the viewer didn't write! If the user is banned
// or suspended too they can't edit.
if (!isViewerComment || isViewerBanned || isViewerSuspended) {
return false;
}
// Can't edit a comment if the editable date is before the current date!
if (
!comment.editing.editableUntil ||
!isBeforeDate(comment.editing.editableUntil)
) {
return false;
}
// Comment is editable!
return true;
});
useEffect(() => {
// If the comment is not editable now, it can't be editable in the future,
// so exit!
if (!editable) {
return;
}
// The comment is editable, we should register a callback to remove that
// status when it is no longer editable. We know that the `editableUntil` is
// available because it was editable!
const editableFor =
new Date(comment.editing.editableUntil!).getTime() - Date.now();
if (editableFor <= 0) {
// Can't schedule a timer for the past! The comment is no longer editable.
setEditable(false);
return;
}
// Setup the timeout.
const timeout: LongTimeout | null = setLongTimeout(() => {
// Mark the comment as not editable.
setEditable(false);
}, editableFor);
return () => {
// When this component is disposed, also clear the timeout.
clearLongTimeout(timeout);
};
}, [comment.editing.editableUntil, editable]);
const hasFeaturedTag = comment.tags.some((t) => t.code === GQLTAG.FEATURED);
// We are in a Q&A if the story mode is set to QA.
const isQA = story.settings.mode === GQLSTORY_MODE.QA;
// Author is expert if comment is tagged Expert and the
// story mode is Q&A.
const authorIsExpert: boolean =
isQA && comment.tags.some((t) => t.code === GQLTAG.EXPERT);
// Only show a button to clear removed answers if this comment is by an
// expert, reply to a top level comment (question) with an answer.
const showRemoveAnswered: boolean =
!comment.deleted &&
isQA &&
authorIsExpert &&
indentLevel === 1 &&
!!onRemoveAnswered;
// When we're in Q&A and we are not un-answered (answered) and we're a top
// level comment (no parent), then we are an answered question.
const hasAnsweredTag: boolean =
!hideAnsweredTag &&
isQA &&
comment.tags.every((t) => t.code !== GQLTAG.UNANSWERED) &&
!comment.parent;
const commentTags = (
<>
{hasFeaturedTag && !isQA && <FeaturedTag collapsed={collapsed} />}
{hasAnsweredTag && isQA && <AnsweredTag collapsed={collapsed} />}
</>
);
const showModerationCaret: boolean =
!!viewer &&
story.canModerate &&
can(viewer, Ability.MODERATE) &&
!hideModerationCarat;
if (showEditDialog) {
return (
<div data-testid={`comment-${comment.id}`}>
<EditCommentFormContainer
settings={settings}
comment={comment}
story={story}
onClose={toggleShowEditDialog}
/>
</div>
);
}
const hasTags = commentHasTags(story, comment);
const hasBadges = authorHasBadges(comment);
// Comment is not published after viewer rejected it.
if (comment.lastViewerAction === "REJECT" && comment.status === "REJECTED") {
return <RejectedTombstoneContainer comment={comment} />;
}
// Comment is not published after edit, so don't render it anymore.
if (comment.lastViewerAction === "EDIT" && !isPublished(comment.status)) {
return null;
}
return (
<div
className={cn(
CLASSES.comment.$root,
`${CLASSES.comment.reacted}-${comment.actionCounts.reaction.total}`,
className
)}
data-testid={`comment-${comment.id}`}
>
<HorizontalGutter>
{!comment.deleted && (
<IndentedComment
indentLevel={indentLevel}
collapsed={collapsed}
body={comment.body}
createdAt={comment.createdAt}
blur={!!comment.pending}
showEditedMarker={comment.editing.edited}
highlight={highlight}
toggleCollapsed={toggleCollapsed}
parentAuthorName={comment.parent?.author?.username}
staticUsername={
comment.author && (
<Flex direction="row" alignItems="center">
<UsernameContainer
className={cn(
styles.staticUsername,
CLASSES.comment.topBar.username
)}
comment={comment}
/>
<UserTagsContainer
className={CLASSES.comment.topBar.userTag}
story={story}
comment={comment}
settings={settings}
/>
<UserBadgesContainer
className={CLASSES.comment.topBar.userBadge}
comment={comment}
/>
</Flex>
)
}
username={
comment.author && (
<UsernameWithPopoverContainer
className={CLASSES.comment.topBar.username}
comment={comment}
viewer={viewer}
/>
)
}
tags={
comment.author &&
hasTags && (
<UserTagsContainer
className={CLASSES.comment.topBar.userTag}
story={story}
comment={comment}
settings={settings}
/>
)
}
badges={
comment.author &&
hasBadges && (
<UserBadgesContainer
className={CLASSES.comment.topBar.userBadge}
comment={comment}
/>
)
}
staticTopBarRight={commentTags}
topBarRight={
<>
<MatchMedia gteWidth="mobile">
{(matches) => (
<>
<Flex
alignItems="center"
justifyContent="flex-end"
itemGutter
>
{matches ? commentTags : null}
{editable && (
<Button
color="stream"
variant="text"
onClick={openEditDialog}
className={cn(
CLASSES.comment.topBar.editButton,
styles.editButton
)}
data-testid="comment-edit-button"
>
<Flex alignItems="center" justifyContent="center">
<Icon className={styles.editIcon}>edit</Icon>
<Localized id="comments-commentContainer-editButton">
Edit
</Localized>
</Flex>
</Button>
)}
{showModerationCaret && (
<CaretContainer
comment={comment}
story={story}
viewer={viewer!}
/>
)}
</Flex>
{!matches ? commentTags : null}
</>
)}
</MatchMedia>
</>
}
media={
<MediaSectionContainer
comment={comment}
settings={settings}
defaultExpanded={viewer?.mediaSettings?.unfurlEmbeds}
/>
}
footer={
<>
<Flex
justifyContent="space-between"
className={CLASSES.comment.actionBar.$root}
>
<ButtonsBar className={styles.actionBar}>
<ReactionButtonContainer
comment={comment}
settings={settings}
viewer={viewer}
readOnly={isViewerBanned || isViewerSuspended}
className={cn(
styles.actionButton,
CLASSES.comment.actionBar.reactButton
)}
reactedClassName={cn(
styles.actionButton,
CLASSES.comment.actionBar.reactedButton
)}
isQA={story.settings.mode === GQLSTORY_MODE.QA}
/>
{!disableReplies &&
!isViewerBanned &&
!isViewerSuspended &&
!isViewerScheduledForDeletion && (
<ReplyButton
id={`comments-commentContainer-replyButton-${comment.id}`}
onClick={toggleShowReplyDialog}
active={showReplyDialog}
disabled={
settings.disableCommenting.enabled || story.isClosed
}
className={cn(
styles.actionButton,
CLASSES.comment.actionBar.replyButton
)}
/>
)}
<PermalinkButtonContainer
story={story}
commentID={comment.id}
className={cn(
styles.actionButton,
CLASSES.comment.actionBar.shareButton
)}
/>
</ButtonsBar>
<ButtonsBar>
{!isViewerBanned &&
!isViewerSuspended &&
!hideReportButton && (
<ReportButton
onClick={toggleShowReportFlow}
open={showReportFlow}
viewer={viewer}
comment={comment}
/>
)}
</ButtonsBar>
</Flex>
{showConversationLink && (
<ShowConversationLink
className={CLASSES.comment.readMoreOfConversation}
id={`comments-commentContainer-showConversation-${comment.id}`}
onClick={handleShowConversation}
href={getURLWithCommentID(story.url, comment.id)}
/>
)}
</>
}
/>
)}
{showReportFlow && (
<ReportFlowContainer
viewer={viewer}
comment={comment}
onClose={toggleShowReportFlow}
/>
)}
{showReplyDialog && !comment.deleted && (
<ReplyCommentFormContainer
settings={settings}
comment={comment}
story={story}
onClose={toggleShowReplyDialog}
localReply={localReply}
/>
)}
{showRemoveAnswered && (
<Localized id="qa-unansweredTab-doneAnswering">
<Button
variant="regular"
color="regular"
className={styles.removeAnswered}
onClick={onRemoveAnswered}
>
Done
</Button>
</Localized>
)}
</HorizontalGutter>
</div>
);
};
const enhanced = withContext(({ eventEmitter }) => ({ eventEmitter }))(
withSetCommentIDMutation(
withShowAuthPopupMutation(
withFragmentContainer<Props>({
viewer: graphql`
fragment CommentContainer_viewer on User {
id
status {
current
}
ignoredUsers {
id
}
badges
role
scheduledDeletionDate
mediaSettings {
unfurlEmbeds
}
...UsernameWithPopoverContainer_viewer
...ReactionButtonContainer_viewer
...ReportFlowContainer_viewer
...ReportButton_viewer
...CaretContainer_viewer
}
`,
story: graphql`
fragment CommentContainer_story on Story {
url
isClosed
canModerate
settings {
mode
}
...CaretContainer_story
...ReplyCommentFormContainer_story
...PermalinkButtonContainer_story
...EditCommentFormContainer_story
...UserTagsContainer_story
}
`,
comment: graphql`
fragment CommentContainer_comment on Comment {
id
author {
id
username
}
parent {
author {
username
}
}
body
createdAt
status
editing {
edited
editableUntil
}
tags {
code
}
pending
lastViewerAction
deleted
actionCounts {
reaction {
total
}
}
viewerActionPresence {
dontAgree
flag
}
...ReplyCommentFormContainer_comment
...EditCommentFormContainer_comment
...ReactionButtonContainer_comment
...ReportFlowContainer_comment
...ReportButton_comment
...CaretContainer_comment
...RejectedTombstoneContainer_comment
...AuthorBadgesContainer_comment
...UserTagsContainer_comment
...UsernameWithPopoverContainer_comment
...UsernameContainer_comment
...MediaSectionContainer_comment
...UsernameContainer_comment
}
`,
settings: graphql`
fragment CommentContainer_settings on Settings {
disableCommenting {
enabled
}
featureFlags
...ReactionButtonContainer_settings
...ReplyCommentFormContainer_settings
...EditCommentFormContainer_settings
...UserTagsContainer_settings
...MediaSectionContainer_settings
}
`,
})(CommentContainer)
)
)
);
export type CommentContainerProps = PropTypesOf<typeof enhanced>;
export default enhanced;