import { Answer } from 'common/answer' import { Bet } from 'common/bet' import { FreeResponseContract } from 'common/contract' import { ContractComment } from 'common/comment' import React, { useEffect, useState } from 'react' import { Col } from 'web/components/layout/col' import { Row } from 'web/components/layout/row' import { Avatar } from 'web/components/avatar' import { Linkify } from 'web/components/linkify' import clsx from 'clsx' import { ContractCommentInput, FeedComment, getMostRecentCommentableBet, } from 'web/components/feed/feed-comments' import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time' import { useRouter } from 'next/router' import { Dictionary } from 'lodash' import { User } from 'common/user' import { useEvent } from 'web/hooks/use-event' import { CommentTipMap } from 'web/hooks/use-tip-txns' import { UserLink } from 'web/components/user-link' export function FeedAnswerCommentGroup(props: { contract: FreeResponseContract user: User | undefined | null answer: Answer answerComments: ContractComment[] tips: CommentTipMap betsByCurrentUser: Bet[] commentsByUserId: Dictionary }) { const { answer, contract, answerComments, tips, betsByCurrentUser, commentsByUserId, user, } = props const { username, avatarUrl, name, text } = answer const [replyToUser, setReplyToUser] = useState>() const [showReply, setShowReply] = useState(false) const [highlighted, setHighlighted] = useState(false) const router = useRouter() const answerElementId = `answer-${answer.id}` const commentsByCurrentUser = (user && commentsByUserId[user.id]) ?? [] const isFreeResponseContractPage = !!commentsByCurrentUser const mostRecentCommentableBet = getMostRecentCommentableBet( betsByCurrentUser, commentsByCurrentUser, user, answer.number.toString() ) const [usersMostRecentBetTimeAtLoad, setUsersMostRecentBetTimeAtLoad] = useState( !user ? undefined : mostRecentCommentableBet?.createdTime ?? 0 ) useEffect(() => { if (user && usersMostRecentBetTimeAtLoad === undefined) setUsersMostRecentBetTimeAtLoad( mostRecentCommentableBet?.createdTime ?? 0 ) }, [ mostRecentCommentableBet?.createdTime, user, usersMostRecentBetTimeAtLoad, ]) const scrollAndOpenReplyInput = useEvent( (comment?: ContractComment, answer?: Answer) => { setReplyToUser( comment ? { id: comment.userId, username: comment.userUsername } : answer ? { id: answer.userId, username: answer.username } : undefined ) setShowReply(true) } ) useEffect(() => { // Only show one comment input for a bet at a time if ( betsByCurrentUser.length > 1 && // inputRef?.textContent?.length === 0 && //TODO: editor.isEmpty betsByCurrentUser.sort((a, b) => b.createdTime - a.createdTime)[0] ?.outcome !== answer.number.toString() ) setShowReply(false) // Even if we pass memoized bets this still runs on every render, which we don't want // eslint-disable-next-line react-hooks/exhaustive-deps }, [betsByCurrentUser.length, user, answer.number]) useEffect(() => { if (router.asPath.endsWith(`#${answerElementId}`)) { setHighlighted(true) } }, [answerElementId, router.asPath]) return (
answered
{isFreeResponseContractPage && (
)} {isFreeResponseContractPage && ( )}
{answerComments.map((comment) => ( ))} {showReply && (
)} ) }