import { useCallback, useEffect } from 'react' import { useTrack } from '@audius/common/api' import { CommentSectionProvider, useCurrentCommentSection } from '@audius/common/context' import { commentsMessages as messages } from '@audius/common/messages' import { ID } from '@audius/common/models' import { Flex, IconCaretRight, IconMessage, Paper, PlainButton, Skeleton, Text } from '@audius/harmony' import { useDispatch } from 'react-redux' import { Link, useSearchParams, useNavigate } from 'react-router' import { push as pushRoute } from 'utils/navigation' import { CommentBlock } from './CommentBlock' import { CommentForm } from './CommentForm' const CommentPreviewHeader = () => { const { entityId, commentSectionLoading: isLoading, commentIds, commentCount } = useCurrentCommentSection() const { data: trackPermalink } = useTrack(entityId, { select: (track) => track.permalink }) const isShowingComments = !isLoading && commentIds?.length return ( Comments {isShowingComments ? (  ({commentCount}) ) : null} {isShowingComments ? ( {messages.viewAll} ) : null} ) } const CommentPreviewContent = () => { const { entityId, currentUserId, commentSectionLoading: isLoading, commentIds } = useCurrentCommentSection() const dispatch = useDispatch() const { data: trackPermalink } = useTrack(entityId, { select: (track) => track.permalink }) const handleClick = useCallback(() => { dispatch(pushRoute(`${trackPermalink}/comments`)) }, [trackPermalink, dispatch]) if (isLoading) { return ( ) } if (!commentIds || !commentIds.length) { return ( {messages.noComments} ) } return ( ) } type CommentPreviewProps = { entityId: ID } export const CommentPreview = (props: CommentPreviewProps) => { const { entityId } = props const dispatch = useDispatch() const { data: trackPermalink } = useTrack(entityId, { select: (track) => track.permalink }) const [searchParams] = useSearchParams() const showComments = searchParams.get('showComments') const commentId = searchParams.get('commentId') const navigate = useNavigate() useEffect(() => { if ((showComments || commentId) && trackPermalink) { navigate({ search: '' }, { replace: true }) dispatch( pushRoute( `${trackPermalink}/comments${commentId ? `?commentId=${commentId}` : ''}` ) ) } }, [ showComments, commentId, trackPermalink, dispatch, searchParams, navigate ]) return ( ) }