({
post,
origin,
sortBy,
isComposerOpen = false,
onShare,
onClickUpvote,
modalParentSelector,
permissionNotificationCommentId,
joinNotificationCommentId,
className = {},
onCommented,
removeTopSpacing = false,
}: PostCommentsProps)
| 58 | const noopShowUpvotes = (): void => {}; |
| 59 | |
| 60 | export function PostComments({ |
| 61 | post, |
| 62 | origin, |
| 63 | sortBy, |
| 64 | isComposerOpen = false, |
| 65 | onShare, |
| 66 | onClickUpvote, |
| 67 | modalParentSelector, |
| 68 | permissionNotificationCommentId, |
| 69 | joinNotificationCommentId, |
| 70 | className = {}, |
| 71 | onCommented, |
| 72 | removeTopSpacing = false, |
| 73 | }: PostCommentsProps): ReactElement { |
| 74 | const { id } = post; |
| 75 | const container = useRef<HTMLDivElement | null>(null); |
| 76 | const isModalThread = threadCommentOrigins.has(origin); |
| 77 | const { tokenRefreshed } = useContext(AuthContext); |
| 78 | const { requestMethod } = useRequestProtocol(); |
| 79 | const queryKey = generateCommentsQueryKey({ postId: id, sortBy }); |
| 80 | const { data: comments, isLoading: isLoadingComments } = |
| 81 | useQuery<PostCommentsData>({ |
| 82 | queryKey, |
| 83 | |
| 84 | queryFn: (): Promise<PostCommentsData> => |
| 85 | requestMethod( |
| 86 | POST_COMMENTS_QUERY, |
| 87 | { postId: id, [initialDataKey]: comments, first: 500, sortBy }, |
| 88 | { requestKey: JSON.stringify(queryKey) }, |
| 89 | ), |
| 90 | enabled: !!id && tokenRefreshed, |
| 91 | refetchInterval: 60 * 1000, |
| 92 | refetchOnWindowFocus: false, |
| 93 | }); |
| 94 | |
| 95 | useCommentContentPreferenceMutationSubscription({ queryKey }); |
| 96 | |
| 97 | const { hash: commentHash } = globalThis?.window?.location || {}; |
| 98 | const commentsCount = comments?.postComments?.edges?.length || 0; |
| 99 | const commentRef = useRef<HTMLElement | null>(null); |
| 100 | const { deleteComment } = useDeleteComment(); |
| 101 | |
| 102 | const [scrollToComment, setScrollToComment] = useState(!!commentHash); |
| 103 | useEffect(() => { |
| 104 | if (commentsCount > 0 && scrollToComment && commentRef.current) { |
| 105 | commentRef.current.scrollIntoView({ block: 'center', inline: 'nearest' }); |
| 106 | setScrollToComment(false); |
| 107 | } |
| 108 | }, [commentsCount, scrollToComment]); |
| 109 | |
| 110 | if (isLoadingComments || isNullOrUndefined(comments)) { |
| 111 | return <PlaceholderCommentList placeholderAmount={post.numComments} />; |
| 112 | } |
| 113 | |
| 114 | if (commentsCount === 0) { |
| 115 | return ( |
| 116 | <CharmEmptyState |
| 117 | className="mb-12 mt-8" |
nothing calls this directly
no test coverage detected