2022-05-17 15:55:26 +00:00
|
|
|
import { Answer } from 'common/answer'
|
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { Comment } from 'common/comment'
|
2022-06-13 04:42:41 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-05-17 15:55:26 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
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'
|
2022-05-18 14:42:13 +00:00
|
|
|
import {
|
|
|
|
CommentInput,
|
2022-06-08 13:24:12 +00:00
|
|
|
CommentRepliesList,
|
2022-05-18 14:42:13 +00:00
|
|
|
getMostRecentCommentableBet,
|
|
|
|
} from 'web/components/feed/feed-comments'
|
2022-05-17 15:55:26 +00:00
|
|
|
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
|
|
|
import { useRouter } from 'next/router'
|
2022-06-08 13:24:12 +00:00
|
|
|
import { groupBy } from 'lodash'
|
|
|
|
import { User } from 'common/user'
|
2022-06-08 23:09:49 +00:00
|
|
|
import { useEvent } from 'web/hooks/use-event'
|
2022-06-18 03:28:16 +00:00
|
|
|
import { CommentTipMap } from 'web/hooks/use-tip-txns'
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
export function FeedAnswerCommentGroup(props: {
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: any
|
2022-06-08 13:24:12 +00:00
|
|
|
user: User | undefined | null
|
2022-05-17 15:55:26 +00:00
|
|
|
answer: Answer
|
2022-06-08 13:24:12 +00:00
|
|
|
comments: Comment[]
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTipMap
|
2022-06-08 13:24:12 +00:00
|
|
|
bets: Bet[]
|
2022-05-17 15:55:26 +00:00
|
|
|
}) {
|
2022-06-18 03:28:16 +00:00
|
|
|
const { answer, contract, comments, tips, bets, user } = props
|
2022-05-17 15:55:26 +00:00
|
|
|
const { username, avatarUrl, name, text } = answer
|
2022-06-08 13:24:12 +00:00
|
|
|
|
2022-08-06 20:39:52 +00:00
|
|
|
const [replyToUser, setReplyToUser] =
|
|
|
|
useState<Pick<User, 'id' | 'username'>>()
|
2022-06-08 13:24:12 +00:00
|
|
|
const [showReply, setShowReply] = useState(false)
|
|
|
|
const [highlighted, setHighlighted] = useState(false)
|
|
|
|
const router = useRouter()
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
const answerElementId = `answer-${answer.id}`
|
2022-06-08 13:24:12 +00:00
|
|
|
const betsByUserId = groupBy(bets, (bet) => bet.userId)
|
|
|
|
const commentsByUserId = groupBy(comments, (comment) => comment.userId)
|
2022-06-10 21:15:52 +00:00
|
|
|
const commentsList = comments.filter(
|
2022-06-08 13:24:12 +00:00
|
|
|
(comment) => comment.answerOutcome === answer.number.toString()
|
|
|
|
)
|
|
|
|
const betsByCurrentUser = (user && betsByUserId[user.id]) ?? []
|
|
|
|
const commentsByCurrentUser = (user && commentsByUserId[user.id]) ?? []
|
2022-05-18 14:42:13 +00:00
|
|
|
const isFreeResponseContractPage = !!commentsByCurrentUser
|
2022-06-08 23:09:49 +00:00
|
|
|
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
|
|
|
betsByCurrentUser,
|
|
|
|
commentsByCurrentUser,
|
|
|
|
user,
|
|
|
|
answer.number.toString()
|
|
|
|
)
|
2022-06-09 14:53:01 +00:00
|
|
|
const [usersMostRecentBetTimeAtLoad, setUsersMostRecentBetTimeAtLoad] =
|
|
|
|
useState<number | undefined>(
|
|
|
|
!user ? undefined : mostRecentCommentableBet?.createdTime ?? 0
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user && usersMostRecentBetTimeAtLoad === undefined)
|
|
|
|
setUsersMostRecentBetTimeAtLoad(
|
|
|
|
mostRecentCommentableBet?.createdTime ?? 0
|
|
|
|
)
|
|
|
|
}, [
|
|
|
|
mostRecentCommentableBet?.createdTime,
|
|
|
|
user,
|
|
|
|
usersMostRecentBetTimeAtLoad,
|
|
|
|
])
|
2022-06-08 23:09:49 +00:00
|
|
|
|
|
|
|
const scrollAndOpenReplyInput = useEvent(
|
|
|
|
(comment?: Comment, answer?: Answer) => {
|
2022-08-06 20:39:52 +00:00
|
|
|
setReplyToUser(
|
|
|
|
comment
|
|
|
|
? { id: comment.userId, username: comment.userUsername }
|
|
|
|
: answer
|
|
|
|
? { id: answer.userId, username: answer.username }
|
|
|
|
: undefined
|
|
|
|
)
|
2022-06-08 23:09:49 +00:00
|
|
|
setShowReply(true)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-06-08 13:24:12 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// Only show one comment input for a bet at a time
|
|
|
|
if (
|
2022-06-10 21:15:52 +00:00
|
|
|
betsByCurrentUser.length > 1 &&
|
2022-08-06 20:39:52 +00:00
|
|
|
// inputRef?.textContent?.length === 0 && //TODO: editor.isEmpty
|
2022-06-10 21:15:52 +00:00
|
|
|
betsByCurrentUser.sort((a, b) => b.createdTime - a.createdTime)[0]
|
|
|
|
?.outcome !== answer.number.toString()
|
|
|
|
)
|
2022-06-08 13:24:12 +00:00
|
|
|
setShowReply(false)
|
2022-06-10 21:15:52 +00:00
|
|
|
// Even if we pass memoized bets this still runs on every render, which we don't want
|
2022-06-09 16:15:34 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-06-10 21:15:52 +00:00
|
|
|
}, [betsByCurrentUser.length, user, answer.number])
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-17 16:13:29 +00:00
|
|
|
if (router.asPath.endsWith(`#${answerElementId}`)) {
|
2022-05-17 15:55:26 +00:00
|
|
|
setHighlighted(true)
|
|
|
|
}
|
2022-05-21 21:48:15 +00:00
|
|
|
}, [answerElementId, router.asPath])
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
return (
|
2022-07-20 23:35:07 +00:00
|
|
|
<Col className={'relative flex-1 gap-3'} key={answer.id + 'comment'}>
|
2022-05-17 15:55:26 +00:00
|
|
|
<Row
|
|
|
|
className={clsx(
|
2022-07-21 03:13:37 +00:00
|
|
|
'flex gap-3 space-x-3 pt-4 transition-all duration-1000',
|
2022-05-17 15:55:26 +00:00
|
|
|
highlighted ? `-m-2 my-3 rounded bg-indigo-500/[0.2] p-2` : ''
|
|
|
|
)}
|
|
|
|
id={answerElementId}
|
|
|
|
>
|
2022-07-21 03:13:37 +00:00
|
|
|
<Avatar username={username} avatarUrl={avatarUrl} />
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
<Col className="min-w-0 flex-1 lg:gap-1">
|
|
|
|
<div className="text-sm text-gray-500">
|
|
|
|
<UserLink username={username} name={name} /> answered
|
|
|
|
<CopyLinkDateTimeComponent
|
2022-06-22 16:35:50 +00:00
|
|
|
prefix={contract.creatorUsername}
|
|
|
|
slug={contract.slug}
|
2022-05-17 15:55:26 +00:00
|
|
|
createdTime={answer.createdTime}
|
|
|
|
elementId={answerElementId}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-07-21 03:13:37 +00:00
|
|
|
<Col className="align-items justify-between gap-2 sm:flex-row">
|
2022-05-17 15:55:26 +00:00
|
|
|
<span className="whitespace-pre-line text-lg">
|
|
|
|
<Linkify text={text} />
|
|
|
|
</span>
|
|
|
|
|
2022-07-21 03:13:37 +00:00
|
|
|
{isFreeResponseContractPage && (
|
|
|
|
<div className={'sm:hidden'}>
|
|
|
|
<button
|
|
|
|
className={'text-xs font-bold text-gray-500 hover:underline'}
|
|
|
|
onClick={() => scrollAndOpenReplyInput(undefined, answer)}
|
|
|
|
>
|
|
|
|
Reply
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-05-17 15:55:26 +00:00
|
|
|
</Col>
|
|
|
|
{isFreeResponseContractPage && (
|
|
|
|
<div className={'justify-initial hidden sm:block'}>
|
|
|
|
<button
|
|
|
|
className={'text-xs font-bold text-gray-500 hover:underline'}
|
2022-06-08 13:24:12 +00:00
|
|
|
onClick={() => scrollAndOpenReplyInput(undefined, answer)}
|
2022-05-17 15:55:26 +00:00
|
|
|
>
|
|
|
|
Reply
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-06-08 13:24:12 +00:00
|
|
|
<CommentRepliesList
|
|
|
|
contract={contract}
|
|
|
|
commentsList={commentsList}
|
|
|
|
betsByUserId={betsByUserId}
|
|
|
|
smallAvatar={true}
|
|
|
|
bets={bets}
|
2022-06-18 03:28:16 +00:00
|
|
|
tips={tips}
|
2022-06-08 13:24:12 +00:00
|
|
|
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
|
|
|
treatFirstIndexEqually={true}
|
|
|
|
/>
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
{showReply && (
|
2022-07-21 03:37:43 +00:00
|
|
|
<div className={'ml-6'}>
|
2022-06-08 13:24:12 +00:00
|
|
|
<span
|
2022-07-21 03:37:43 +00:00
|
|
|
className="absolute -ml-[1px] mt-[1.25rem] h-2 w-0.5 rotate-90 bg-gray-200"
|
2022-06-08 13:24:12 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2022-05-17 15:55:26 +00:00
|
|
|
<CommentInput
|
|
|
|
contract={contract}
|
2022-06-08 13:24:12 +00:00
|
|
|
betsByCurrentUser={betsByCurrentUser}
|
|
|
|
commentsByCurrentUser={commentsByCurrentUser}
|
|
|
|
parentAnswerOutcome={answer.number.toString()}
|
2022-08-06 20:39:52 +00:00
|
|
|
replyToUser={replyToUser}
|
|
|
|
onSubmitComment={() => setShowReply(false)}
|
2022-05-17 15:55:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|