import { Answer } from 'common/answer' import { Bet } from 'common/bet' import { Comment } from 'common/comment' import { formatPercent } from 'common/util/format' import React, { useEffect, useState } from 'react' import { Col } from 'web/components/layout/col' import { Modal } from 'web/components/layout/modal' import { AnswerBetPanel } from 'web/components/answers/answer-bet-panel' import { Row } from 'web/components/layout/row' import { Avatar } from 'web/components/avatar' import { UserLink } from 'web/components/user-page' import { Linkify } from 'web/components/linkify' import clsx from 'clsx' import { tradingAllowed } from 'web/lib/firebase/contracts' import { BuyButton } from 'web/components/yes-no-selector' import { CommentInput, CommentRepliesList, getMostRecentCommentableBet, } from 'web/components/feed/feed-comments' import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time' import { useRouter } from 'next/router' import { groupBy } from 'lodash' import { User } from 'common/user' import { useEvent } from 'web/hooks/use-event' import { getDpmOutcomeProbability } from 'common/calculate-dpm' export function FeedAnswerCommentGroup(props: { contract: any user: User | undefined | null answer: Answer comments: Comment[] bets: Bet[] }) { const { answer, contract, comments, bets, user } = props const { username, avatarUrl, name, text } = answer const [replyToUsername, setReplyToUsername] = useState('') const [open, setOpen] = useState(false) const [showReply, setShowReply] = useState(false) const [inputRef, setInputRef] = useState(null) const [highlighted, setHighlighted] = useState(false) const router = useRouter() const answerElementId = `answer-${answer.id}` const betsByUserId = groupBy(bets, (bet) => bet.userId) const commentsByUserId = groupBy(comments, (comment) => comment.userId) const answerComments = comments.filter( (comment) => comment.answerOutcome === answer.number.toString() ) const commentReplies = comments.filter( (comment) => comment.replyToCommentId && !comment.answerOutcome && answerComments.map((c) => c.id).includes(comment.replyToCommentId) ) const commentsList = answerComments.concat(commentReplies) const thisAnswerProb = getDpmOutcomeProbability( contract.totalShares, answer.id ) const probPercent = formatPercent(thisAnswerProb) const betsByCurrentUser = (user && betsByUserId[user.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?: Comment, answer?: Answer) => { setReplyToUsername(comment?.userUsername ?? answer?.username ?? '') setShowReply(true) inputRef?.focus() } ) useEffect(() => { if ( mostRecentCommentableBet && usersMostRecentBetTimeAtLoad !== undefined && mostRecentCommentableBet.createdTime > usersMostRecentBetTimeAtLoad && !showReply ) scrollAndOpenReplyInput(undefined, answer) }, [ answer, usersMostRecentBetTimeAtLoad, mostRecentCommentableBet, scrollAndOpenReplyInput, showReply, ]) useEffect(() => { // Only show one comment input for a bet at a time const usersMostRecentBet = bets .filter((b) => b.userId === user?.id) .sort((a, b) => b.createdTime - a.createdTime)[0] if ( usersMostRecentBet && usersMostRecentBet.outcome !== answer.number.toString() ) { setShowReply(false) } }, [answer.number, bets, user]) useEffect(() => { if (showReply && inputRef) inputRef.focus() }, [inputRef, showReply]) useEffect(() => { if (router.asPath.endsWith(`#${answerElementId}`)) { setHighlighted(true) } }, [answerElementId, router.asPath]) return ( setOpen(false)} className="sm:max-w-84 !rounded-md bg-white !px-8 !py-6" isModal={true} />
answered
{isFreeResponseContractPage && (
)}
{probPercent} setOpen(true)} />
{isFreeResponseContractPage && ( )}
{showReply && (
)} ) }