2022-05-17 15:55:26 +00:00
|
|
|
import { Bet } from 'common/bet'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { ContractComment } from 'common/comment'
|
2022-09-18 00:12:35 +00:00
|
|
|
import { User } from 'common/user'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-08-30 09:41:47 +00:00
|
|
|
import { minBy, maxBy, partition, sumBy, Dictionary } from 'lodash'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { Avatar } from 'web/components/avatar'
|
|
|
|
import { OutcomeLabel } from 'web/components/outcome-label'
|
|
|
|
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
|
|
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
2022-09-07 22:09:20 +00:00
|
|
|
import { createCommentOnContract } from 'web/lib/firebase/comments'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-06-18 03:28:16 +00:00
|
|
|
import { Tipper } from '../tipper'
|
|
|
|
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
2022-09-07 22:09:20 +00:00
|
|
|
import { Content } from '../editor'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { Editor } from '@tiptap/react'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { UserLink } from 'web/components/user-link'
|
2022-09-07 22:09:20 +00:00
|
|
|
import { CommentInput } from '../comment-input'
|
2022-05-18 14:42:13 +00:00
|
|
|
|
|
|
|
export function FeedCommentThread(props: {
|
2022-08-30 09:41:47 +00:00
|
|
|
user: User | null | undefined
|
2022-05-18 14:42:13 +00:00
|
|
|
contract: Contract
|
2022-08-30 09:41:47 +00:00
|
|
|
threadComments: ContractComment[]
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTipMap
|
2022-08-19 08:06:40 +00:00
|
|
|
parentComment: ContractComment
|
2022-05-19 01:38:02 +00:00
|
|
|
bets: Bet[]
|
2022-08-30 09:41:47 +00:00
|
|
|
betsByUserId: Dictionary<Bet[]>
|
|
|
|
commentsByUserId: Dictionary<ContractComment[]>
|
2022-05-18 14:42:13 +00:00
|
|
|
}) {
|
2022-08-30 09:41:47 +00:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
contract,
|
|
|
|
threadComments,
|
|
|
|
commentsByUserId,
|
|
|
|
bets,
|
|
|
|
betsByUserId,
|
|
|
|
tips,
|
|
|
|
parentComment,
|
|
|
|
} = props
|
2022-05-18 14:42:13 +00:00
|
|
|
const [showReply, setShowReply] = useState(false)
|
2022-08-30 09:41:47 +00:00
|
|
|
const [replyTo, setReplyTo] = useState<{ id: string; username: string }>()
|
2022-08-06 20:39:52 +00:00
|
|
|
|
2022-08-19 08:06:40 +00:00
|
|
|
function scrollAndOpenReplyInput(comment: ContractComment) {
|
2022-08-30 09:41:47 +00:00
|
|
|
setReplyTo({ id: comment.userId, username: comment.userUsername })
|
2022-05-18 14:42:13 +00:00
|
|
|
setShowReply(true)
|
|
|
|
}
|
2022-08-06 20:39:52 +00:00
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
return (
|
2022-08-30 09:41:47 +00:00
|
|
|
<Col className="relative w-full items-stretch gap-3 pb-4">
|
2022-06-08 13:24:12 +00:00
|
|
|
<span
|
2022-08-30 09:41:47 +00:00
|
|
|
className="absolute top-5 left-4 -ml-px h-[calc(100%-2rem)] w-0.5 bg-gray-200"
|
2022-06-08 13:24:12 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2022-08-30 09:41:47 +00:00
|
|
|
{[parentComment].concat(threadComments).map((comment, commentIdx) => (
|
|
|
|
<FeedComment
|
|
|
|
key={comment.id}
|
|
|
|
indent={commentIdx != 0}
|
|
|
|
contract={contract}
|
|
|
|
comment={comment}
|
|
|
|
tips={tips[comment.id]}
|
|
|
|
betsBySameUser={betsByUserId[comment.userId] ?? []}
|
|
|
|
onReplyClick={scrollAndOpenReplyInput}
|
|
|
|
probAtCreatedTime={
|
|
|
|
contract.outcomeType === 'BINARY'
|
|
|
|
? minBy(bets, (bet) => {
|
|
|
|
return bet.createdTime < comment.createdTime
|
|
|
|
? comment.createdTime - bet.createdTime
|
|
|
|
: comment.createdTime
|
|
|
|
})?.probAfter
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
2022-06-08 13:24:12 +00:00
|
|
|
{showReply && (
|
2022-08-30 09:41:47 +00:00
|
|
|
<Col className="-pb-2 relative ml-6">
|
2022-06-08 13:24:12 +00:00
|
|
|
<span
|
2022-08-30 09:41:47 +00:00
|
|
|
className="absolute -left-1 -ml-[1px] mt-[0.8rem] h-2 w-0.5 rotate-90 bg-gray-200"
|
2022-06-08 13:24:12 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2022-09-07 22:09:20 +00:00
|
|
|
<ContractCommentInput
|
2022-06-08 13:24:12 +00:00
|
|
|
contract={contract}
|
|
|
|
betsByCurrentUser={(user && betsByUserId[user.id]) ?? []}
|
2022-08-30 09:41:47 +00:00
|
|
|
commentsByCurrentUser={(user && commentsByUserId[user.id]) ?? []}
|
2022-06-08 13:24:12 +00:00
|
|
|
parentCommentId={parentComment.id}
|
2022-08-30 09:41:47 +00:00
|
|
|
replyToUser={replyTo}
|
|
|
|
parentAnswerOutcome={parentComment.answerOutcome}
|
2022-09-07 22:09:20 +00:00
|
|
|
onSubmitComment={() => {
|
|
|
|
setShowReply(false)
|
|
|
|
}}
|
2022-06-08 13:24:12 +00:00
|
|
|
/>
|
2022-07-21 05:46:56 +00:00
|
|
|
</Col>
|
2022-06-08 13:24:12 +00:00
|
|
|
)}
|
2022-07-21 05:46:56 +00:00
|
|
|
</Col>
|
2022-06-08 13:24:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
export function FeedComment(props: {
|
|
|
|
contract: Contract
|
2022-08-19 08:06:40 +00:00
|
|
|
comment: ContractComment
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTips
|
2022-05-18 14:42:13 +00:00
|
|
|
betsBySameUser: Bet[]
|
2022-08-30 09:41:47 +00:00
|
|
|
indent?: boolean
|
2022-05-19 01:38:02 +00:00
|
|
|
probAtCreatedTime?: number
|
2022-08-19 08:06:40 +00:00
|
|
|
onReplyClick?: (comment: ContractComment) => void
|
2022-05-18 14:42:13 +00:00
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
contract,
|
|
|
|
comment,
|
2022-06-18 03:28:16 +00:00
|
|
|
tips,
|
2022-05-18 14:42:13 +00:00
|
|
|
betsBySameUser,
|
2022-08-30 09:41:47 +00:00
|
|
|
indent,
|
2022-05-19 01:38:02 +00:00
|
|
|
probAtCreatedTime,
|
2022-05-18 14:42:13 +00:00
|
|
|
onReplyClick,
|
|
|
|
} = props
|
2022-08-06 20:39:52 +00:00
|
|
|
const { text, content, userUsername, userName, userAvatarUrl, createdTime } =
|
|
|
|
comment
|
2022-09-04 21:28:45 +00:00
|
|
|
const betOutcome = comment.betOutcome
|
|
|
|
let bought: string | undefined
|
|
|
|
let money: string | undefined
|
|
|
|
if (comment.betAmount != null) {
|
|
|
|
bought = comment.betAmount >= 0 ? 'bought' : 'sold'
|
|
|
|
money = formatMoney(Math.abs(comment.betAmount))
|
2022-05-18 14:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const [highlighted, setHighlighted] = useState(false)
|
|
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
|
|
if (router.asPath.endsWith(`#${comment.id}`)) {
|
|
|
|
setHighlighted(true)
|
|
|
|
}
|
2022-05-21 21:48:15 +00:00
|
|
|
}, [comment.id, router.asPath])
|
2022-05-18 14:42:13 +00:00
|
|
|
|
|
|
|
// Only calculated if they don't have a matching bet
|
2022-05-19 01:38:02 +00:00
|
|
|
const { userPosition, outcome } = getBettorsLargestPositionBeforeTime(
|
2022-05-18 14:42:13 +00:00
|
|
|
contract,
|
|
|
|
comment.createdTime,
|
2022-09-04 21:28:45 +00:00
|
|
|
comment.betId ? [] : betsBySameUser
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row
|
2022-08-30 09:41:47 +00:00
|
|
|
id={comment.id}
|
2022-05-18 14:42:13 +00:00
|
|
|
className={clsx(
|
2022-08-30 09:41:47 +00:00
|
|
|
'relative',
|
|
|
|
indent ? 'ml-6' : '',
|
|
|
|
highlighted ? `-m-1.5 rounded bg-indigo-500/[0.2] p-1.5` : ''
|
2022-05-18 14:42:13 +00:00
|
|
|
)}
|
|
|
|
>
|
2022-08-30 09:41:47 +00:00
|
|
|
{/*draw a gray line from the comment to the left:*/}
|
|
|
|
{indent ? (
|
|
|
|
<span
|
|
|
|
className="absolute -left-1 -ml-[1px] mt-[0.8rem] h-2 w-0.5 rotate-90 bg-gray-200"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
<Avatar size="sm" username={userUsername} avatarUrl={userAvatarUrl} />
|
|
|
|
<div className="ml-1.5 min-w-0 flex-1 pl-0.5 sm:ml-3">
|
|
|
|
<div className="mt-0.5 text-sm text-gray-500">
|
2022-05-18 14:42:13 +00:00
|
|
|
<UserLink
|
|
|
|
className="text-gray-500"
|
|
|
|
username={userUsername}
|
|
|
|
name={userName}
|
|
|
|
/>{' '}
|
2022-09-04 21:28:45 +00:00
|
|
|
{!comment.betId != null &&
|
2022-05-19 17:42:03 +00:00
|
|
|
userPosition > 0 &&
|
|
|
|
contract.outcomeType !== 'NUMERIC' && (
|
|
|
|
<>
|
|
|
|
{'is '}
|
|
|
|
<CommentStatus
|
|
|
|
prob={probAtCreatedTime}
|
|
|
|
outcome={outcome}
|
|
|
|
contract={contract}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2022-08-30 09:41:47 +00:00
|
|
|
{bought} {money}
|
|
|
|
{contract.outcomeType !== 'FREE_RESPONSE' && betOutcome && (
|
|
|
|
<>
|
|
|
|
{' '}
|
|
|
|
of{' '}
|
|
|
|
<OutcomeLabel
|
|
|
|
outcome={betOutcome ? betOutcome : ''}
|
|
|
|
contract={contract}
|
|
|
|
truncate="short"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
<CopyLinkDateTimeComponent
|
2022-06-22 16:35:50 +00:00
|
|
|
prefix={contract.creatorUsername}
|
|
|
|
slug={contract.slug}
|
2022-05-18 14:42:13 +00:00
|
|
|
createdTime={createdTime}
|
|
|
|
elementId={comment.id}
|
|
|
|
/>
|
2022-06-01 18:26:41 +00:00
|
|
|
</div>
|
2022-08-30 09:41:47 +00:00
|
|
|
<Content
|
|
|
|
className="mt-2 text-[15px] text-gray-700"
|
|
|
|
content={content || text}
|
|
|
|
smallImage
|
|
|
|
/>
|
2022-06-18 03:28:16 +00:00
|
|
|
<Row className="mt-2 items-center gap-6 text-xs text-gray-500">
|
|
|
|
<Tipper comment={comment} tips={tips ?? {}} />
|
|
|
|
{onReplyClick && (
|
|
|
|
<button
|
|
|
|
className="font-bold hover:underline"
|
|
|
|
onClick={() => onReplyClick(comment)}
|
|
|
|
>
|
|
|
|
Reply
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Row>
|
2022-05-18 14:42:13 +00:00
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
export function getMostRecentCommentableBet(
|
|
|
|
betsByCurrentUser: Bet[],
|
2022-08-19 08:06:40 +00:00
|
|
|
commentsByCurrentUser: ContractComment[],
|
2022-05-17 15:55:26 +00:00
|
|
|
user?: User | null,
|
|
|
|
answerOutcome?: string
|
|
|
|
) {
|
2022-06-08 13:24:12 +00:00
|
|
|
let sortedBetsByCurrentUser = betsByCurrentUser.sort(
|
|
|
|
(a, b) => b.createdTime - a.createdTime
|
|
|
|
)
|
|
|
|
if (answerOutcome) {
|
|
|
|
sortedBetsByCurrentUser = sortedBetsByCurrentUser.slice(0, 1)
|
|
|
|
}
|
|
|
|
return sortedBetsByCurrentUser
|
2022-05-17 15:55:26 +00:00
|
|
|
.filter((bet) => {
|
|
|
|
if (
|
|
|
|
canCommentOnBet(bet, user) &&
|
2022-05-18 14:42:13 +00:00
|
|
|
!commentsByCurrentUser.some(
|
|
|
|
(comment) => comment.createdTime > bet.createdTime
|
|
|
|
)
|
2022-05-17 15:55:26 +00:00
|
|
|
) {
|
|
|
|
if (!answerOutcome) return true
|
2022-05-18 14:42:13 +00:00
|
|
|
return answerOutcome === bet.outcome
|
2022-05-17 15:55:26 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
.pop()
|
|
|
|
}
|
|
|
|
|
2022-05-19 01:38:02 +00:00
|
|
|
function CommentStatus(props: {
|
|
|
|
contract: Contract
|
|
|
|
outcome: string
|
|
|
|
prob?: number
|
|
|
|
}) {
|
|
|
|
const { contract, outcome, prob } = props
|
2022-05-18 14:42:13 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-09-18 00:10:34 +00:00
|
|
|
{` predicting `}
|
2022-05-18 14:42:13 +00:00
|
|
|
<OutcomeLabel outcome={outcome} contract={contract} truncate="short" />
|
2022-05-19 01:38:02 +00:00
|
|
|
{prob && ' at ' + Math.round(prob * 100) + '%'}
|
2022-05-18 14:42:13 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-07 22:09:20 +00:00
|
|
|
export function ContractCommentInput(props: {
|
2022-05-18 14:42:13 +00:00
|
|
|
contract: Contract
|
|
|
|
betsByCurrentUser: Bet[]
|
2022-08-19 08:06:40 +00:00
|
|
|
commentsByCurrentUser: ContractComment[]
|
2022-08-30 09:41:47 +00:00
|
|
|
className?: string
|
2022-09-07 22:09:20 +00:00
|
|
|
parentAnswerOutcome?: string | undefined
|
2022-08-06 20:39:52 +00:00
|
|
|
replyToUser?: { id: string; username: string }
|
2022-06-08 13:24:12 +00:00
|
|
|
parentCommentId?: string
|
|
|
|
onSubmitComment?: () => void
|
2022-05-18 14:42:13 +00:00
|
|
|
}) {
|
|
|
|
const user = useUser()
|
2022-09-07 22:09:20 +00:00
|
|
|
async function onSubmitComment(editor: Editor, betId: string | undefined) {
|
2022-05-18 14:42:13 +00:00
|
|
|
if (!user) {
|
2022-06-15 21:34:34 +00:00
|
|
|
track('sign in to comment')
|
2022-05-18 14:42:13 +00:00
|
|
|
return await firebaseLogin()
|
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
await createCommentOnContract(
|
2022-09-07 22:09:20 +00:00
|
|
|
props.contract.id,
|
2022-08-06 20:39:52 +00:00
|
|
|
editor.getJSON(),
|
2022-05-18 14:42:13 +00:00
|
|
|
user,
|
|
|
|
betId,
|
2022-09-07 22:09:20 +00:00
|
|
|
props.parentAnswerOutcome,
|
|
|
|
props.parentCommentId
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
2022-09-07 22:09:20 +00:00
|
|
|
props.onSubmitComment?.()
|
2022-05-18 14:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:09:20 +00:00
|
|
|
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
|
|
|
props.betsByCurrentUser,
|
|
|
|
props.commentsByCurrentUser,
|
|
|
|
user,
|
|
|
|
props.parentAnswerOutcome
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
|
2022-09-07 22:09:20 +00:00
|
|
|
const { id } = mostRecentCommentableBet || { id: undefined }
|
2022-08-28 05:23:25 +00:00
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
return (
|
2022-09-12 21:50:31 +00:00
|
|
|
<CommentInput
|
|
|
|
replyToUser={props.replyToUser}
|
|
|
|
parentAnswerOutcome={props.parentAnswerOutcome}
|
|
|
|
parentCommentId={props.parentCommentId}
|
|
|
|
onSubmitComment={onSubmitComment}
|
|
|
|
className={props.className}
|
|
|
|
presetId={id}
|
|
|
|
/>
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-19 01:38:02 +00:00
|
|
|
function getBettorsLargestPositionBeforeTime(
|
2022-05-18 14:42:13 +00:00
|
|
|
contract: Contract,
|
|
|
|
createdTime: number,
|
|
|
|
bets: Bet[]
|
|
|
|
) {
|
|
|
|
let yesFloorShares = 0,
|
|
|
|
yesShares = 0,
|
|
|
|
noShares = 0,
|
|
|
|
noFloorShares = 0
|
|
|
|
|
|
|
|
const previousBets = bets.filter(
|
|
|
|
(prevBet) => prevBet.createdTime < createdTime && !prevBet.isAnte
|
|
|
|
)
|
|
|
|
|
|
|
|
if (contract.outcomeType === 'FREE_RESPONSE') {
|
|
|
|
const answerCounts: { [outcome: string]: number } = {}
|
|
|
|
for (const bet of previousBets) {
|
|
|
|
if (bet.outcome) {
|
|
|
|
if (!answerCounts[bet.outcome]) {
|
|
|
|
answerCounts[bet.outcome] = bet.amount
|
|
|
|
} else {
|
|
|
|
answerCounts[bet.outcome] += bet.amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const majorityAnswer =
|
2022-05-22 08:36:05 +00:00
|
|
|
maxBy(Object.keys(answerCounts), (outcome) => answerCounts[outcome]) ?? ''
|
2022-05-18 14:42:13 +00:00
|
|
|
return {
|
|
|
|
userPosition: answerCounts[majorityAnswer] || 0,
|
|
|
|
outcome: majorityAnswer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bets.length === 0) {
|
2022-08-30 09:41:47 +00:00
|
|
|
return { userPosition: 0, outcome: '' }
|
2022-05-18 14:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const [yesBets, noBets] = partition(
|
2022-05-18 14:42:13 +00:00
|
|
|
previousBets ?? [],
|
|
|
|
(bet) => bet.outcome === 'YES'
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
yesShares = sumBy(yesBets, (bet) => bet.shares)
|
|
|
|
noShares = sumBy(noBets, (bet) => bet.shares)
|
2022-05-18 14:42:13 +00:00
|
|
|
yesFloorShares = Math.floor(yesShares)
|
|
|
|
noFloorShares = Math.floor(noShares)
|
|
|
|
|
|
|
|
const userPosition = yesFloorShares || noFloorShares
|
|
|
|
const outcome = yesFloorShares > noFloorShares ? 'YES' : 'NO'
|
|
|
|
return { userPosition, outcome }
|
|
|
|
}
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
function canCommentOnBet(bet: Bet, user?: User | null) {
|
|
|
|
const { userId, createdTime, isRedemption } = bet
|
|
|
|
const isSelf = user?.id === userId
|
|
|
|
// You can comment if your bet was posted in the last hour
|
|
|
|
return !isRedemption && isSelf && Date.now() - createdTime < 60 * 60 * 1000
|
|
|
|
}
|