2022-05-17 15:55:26 +00:00
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { Comment } from 'common/comment'
|
|
|
|
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-06-08 13:24:12 +00:00
|
|
|
import { minBy, maxBy, groupBy, 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 { UserLink } from 'web/components/user-page'
|
|
|
|
import { OutcomeLabel } from 'web/components/outcome-label'
|
|
|
|
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
|
|
|
import { contractPath } from 'web/lib/firebase/contracts'
|
|
|
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
|
|
|
import { createComment, MAX_COMMENT_LENGTH } from 'web/lib/firebase/comments'
|
|
|
|
import Textarea from 'react-expanding-textarea'
|
|
|
|
import { Linkify } from 'web/components/linkify'
|
|
|
|
import { SiteLink } from 'web/components/site-link'
|
|
|
|
import { BetStatusText } from 'web/components/feed/feed-bets'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
2022-05-19 01:38:02 +00:00
|
|
|
import { getProbability } from 'common/calculate'
|
2022-05-18 16:08:31 +00:00
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
2022-05-23 22:09:40 +00:00
|
|
|
import { PaperAirplaneIcon } from '@heroicons/react/outline'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-05-18 14:42:13 +00:00
|
|
|
|
|
|
|
export function FeedCommentThread(props: {
|
|
|
|
contract: Contract
|
|
|
|
comments: Comment[]
|
|
|
|
parentComment: Comment
|
2022-05-19 01:38:02 +00:00
|
|
|
bets: Bet[]
|
2022-05-18 14:42:13 +00:00
|
|
|
truncate?: boolean
|
|
|
|
smallAvatar?: boolean
|
|
|
|
}) {
|
2022-05-19 01:38:02 +00:00
|
|
|
const { contract, comments, bets, truncate, smallAvatar, parentComment } =
|
|
|
|
props
|
2022-05-18 14:42:13 +00:00
|
|
|
const [showReply, setShowReply] = useState(false)
|
|
|
|
const [replyToUsername, setReplyToUsername] = useState('')
|
2022-05-22 08:36:05 +00:00
|
|
|
const betsByUserId = groupBy(bets, (bet) => bet.userId)
|
2022-05-18 14:42:13 +00:00
|
|
|
const user = useUser()
|
|
|
|
const commentsList = comments.filter(
|
2022-05-18 15:35:32 +00:00
|
|
|
(comment) =>
|
|
|
|
parentComment.id && comment.replyToCommentId === parentComment.id
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
commentsList.unshift(parentComment)
|
|
|
|
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
|
|
|
|
function scrollAndOpenReplyInput(comment: Comment) {
|
|
|
|
setReplyToUsername(comment.userUsername)
|
|
|
|
setShowReply(true)
|
|
|
|
inputRef?.focus()
|
|
|
|
}
|
|
|
|
useEffect(() => {
|
|
|
|
if (showReply && inputRef) inputRef.focus()
|
|
|
|
}, [inputRef, showReply])
|
|
|
|
return (
|
2022-06-08 13:24:12 +00:00
|
|
|
<div className={'w-full flex-col pr-1'}>
|
|
|
|
<span
|
|
|
|
className="absolute top-5 left-5 -ml-px h-[calc(100%-2rem)] w-0.5 bg-gray-200"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
<CommentRepliesList
|
|
|
|
contract={contract}
|
|
|
|
commentsList={commentsList}
|
|
|
|
betsByUserId={betsByUserId}
|
|
|
|
smallAvatar={smallAvatar}
|
|
|
|
truncate={truncate}
|
|
|
|
bets={bets}
|
|
|
|
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
|
|
|
/>
|
|
|
|
{showReply && (
|
|
|
|
<div className={'-pb-2 ml-6 flex flex-col pt-5'}>
|
|
|
|
<span
|
|
|
|
className="absolute -ml-[1px] mt-[0.8rem] h-2 w-0.5 rotate-90 bg-gray-200"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
<CommentInput
|
|
|
|
contract={contract}
|
|
|
|
betsByCurrentUser={(user && betsByUserId[user.id]) ?? []}
|
|
|
|
commentsByCurrentUser={comments.filter(
|
|
|
|
(c) => c.userId === user?.id
|
|
|
|
)}
|
|
|
|
parentCommentId={parentComment.id}
|
|
|
|
replyToUsername={replyToUsername}
|
|
|
|
parentAnswerOutcome={comments[0].answerOutcome}
|
|
|
|
setRef={setInputRef}
|
|
|
|
onSubmitComment={() => setShowReply(false)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function CommentRepliesList(props: {
|
|
|
|
contract: Contract
|
|
|
|
commentsList: Comment[]
|
|
|
|
betsByUserId: Dictionary<Bet[]>
|
|
|
|
scrollAndOpenReplyInput: (comment: Comment) => void
|
|
|
|
bets: Bet[]
|
|
|
|
treatFirstIndexEqually?: boolean
|
|
|
|
smallAvatar?: boolean
|
|
|
|
truncate?: boolean
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
contract,
|
|
|
|
commentsList,
|
|
|
|
betsByUserId,
|
|
|
|
truncate,
|
|
|
|
smallAvatar,
|
|
|
|
bets,
|
|
|
|
scrollAndOpenReplyInput,
|
|
|
|
treatFirstIndexEqually,
|
|
|
|
} = props
|
|
|
|
return (
|
|
|
|
<>
|
2022-05-18 14:42:13 +00:00
|
|
|
{commentsList.map((comment, commentIdx) => (
|
|
|
|
<div
|
|
|
|
key={comment.id}
|
|
|
|
id={comment.id}
|
2022-06-08 13:24:12 +00:00
|
|
|
className={clsx(
|
|
|
|
'relative',
|
|
|
|
!treatFirstIndexEqually && commentIdx === 0 ? '' : 'mt-3 ml-6'
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
>
|
2022-05-23 22:09:40 +00:00
|
|
|
{/*draw a gray line from the comment to the left:*/}
|
2022-06-08 13:24:12 +00:00
|
|
|
{(treatFirstIndexEqually || commentIdx != 0) && (
|
2022-05-23 22:09:40 +00:00
|
|
|
<span
|
|
|
|
className="absolute -ml-[1px] mt-[0.8rem] h-2 w-0.5 rotate-90 bg-gray-200"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
<FeedComment
|
|
|
|
contract={contract}
|
|
|
|
comment={comment}
|
|
|
|
betsBySameUser={betsByUserId[comment.userId] ?? []}
|
|
|
|
onReplyClick={scrollAndOpenReplyInput}
|
2022-05-19 01:38:02 +00:00
|
|
|
probAtCreatedTime={
|
|
|
|
contract.outcomeType === 'BINARY'
|
2022-05-22 08:36:05 +00:00
|
|
|
? minBy(bets, (bet) => {
|
2022-05-19 01:38:02 +00:00
|
|
|
return bet.createdTime < comment.createdTime
|
|
|
|
? comment.createdTime - bet.createdTime
|
|
|
|
: comment.createdTime
|
|
|
|
})?.probAfter
|
|
|
|
: undefined
|
|
|
|
}
|
2022-05-18 14:42:13 +00:00
|
|
|
smallAvatar={smallAvatar}
|
|
|
|
truncate={truncate}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))}
|
2022-06-08 13:24:12 +00:00
|
|
|
</>
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function FeedComment(props: {
|
|
|
|
contract: Contract
|
|
|
|
comment: Comment
|
|
|
|
betsBySameUser: Bet[]
|
2022-05-19 01:38:02 +00:00
|
|
|
probAtCreatedTime?: number
|
2022-05-18 14:42:13 +00:00
|
|
|
truncate?: boolean
|
|
|
|
smallAvatar?: boolean
|
|
|
|
onReplyClick?: (comment: Comment) => void
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
contract,
|
|
|
|
comment,
|
|
|
|
betsBySameUser,
|
2022-05-19 01:38:02 +00:00
|
|
|
probAtCreatedTime,
|
2022-05-18 14:42:13 +00:00
|
|
|
truncate,
|
|
|
|
onReplyClick,
|
|
|
|
} = props
|
|
|
|
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
|
|
|
let betOutcome: string | undefined,
|
|
|
|
bought: string | undefined,
|
|
|
|
money: string | undefined
|
|
|
|
|
|
|
|
const matchedBet = betsBySameUser.find((bet) => bet.id === comment.betId)
|
|
|
|
if (matchedBet) {
|
|
|
|
betOutcome = matchedBet.outcome
|
|
|
|
bought = matchedBet.amount >= 0 ? 'bought' : 'sold'
|
|
|
|
money = formatMoney(Math.abs(matchedBet.amount))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
matchedBet ? [] : betsBySameUser
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row
|
|
|
|
className={clsx(
|
2022-05-23 22:09:40 +00:00
|
|
|
'flex space-x-1.5 transition-all duration-1000 sm:space-x-3',
|
2022-05-25 22:47:08 +00:00
|
|
|
highlighted ? `-m-1 rounded bg-indigo-500/[0.2] p-2` : ''
|
2022-05-18 14:42:13 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Avatar
|
2022-05-23 22:09:40 +00:00
|
|
|
className={'ml-1'}
|
|
|
|
size={'sm'}
|
2022-05-18 14:42:13 +00:00
|
|
|
username={userUsername}
|
|
|
|
avatarUrl={userAvatarUrl}
|
|
|
|
/>
|
|
|
|
<div className="min-w-0 flex-1">
|
2022-06-01 18:26:41 +00:00
|
|
|
<div className="mt-0.5 pl-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-05-19 17:42:03 +00:00
|
|
|
{!matchedBet &&
|
|
|
|
userPosition > 0 &&
|
|
|
|
contract.outcomeType !== 'NUMERIC' && (
|
|
|
|
<>
|
|
|
|
{'is '}
|
|
|
|
<CommentStatus
|
|
|
|
prob={probAtCreatedTime}
|
|
|
|
outcome={outcome}
|
|
|
|
contract={contract}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
<>
|
|
|
|
{bought} {money}
|
|
|
|
{contract.outcomeType !== 'FREE_RESPONSE' && betOutcome && (
|
|
|
|
<>
|
|
|
|
{' '}
|
|
|
|
of{' '}
|
|
|
|
<OutcomeLabel
|
|
|
|
outcome={betOutcome ? betOutcome : ''}
|
2022-05-19 17:42:03 +00:00
|
|
|
value={(matchedBet as any).value}
|
2022-05-18 14:42:13 +00:00
|
|
|
contract={contract}
|
|
|
|
truncate="short"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
<CopyLinkDateTimeComponent
|
2022-06-08 14:43:24 +00:00
|
|
|
contractCreatorUsername={contract.creatorUsername}
|
|
|
|
contractSlug={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-05-18 14:42:13 +00:00
|
|
|
<TruncatedComment
|
|
|
|
comment={text}
|
|
|
|
moreHref={contractPath(contract)}
|
|
|
|
shouldTruncate={truncate}
|
|
|
|
/>
|
|
|
|
{onReplyClick && (
|
|
|
|
<button
|
|
|
|
className={'text-xs font-bold text-gray-500 hover:underline'}
|
|
|
|
onClick={() => onReplyClick(comment)}
|
|
|
|
>
|
|
|
|
Reply
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
export function getMostRecentCommentableBet(
|
|
|
|
betsByCurrentUser: Bet[],
|
2022-05-18 14:42:13 +00:00
|
|
|
commentsByCurrentUser: Comment[],
|
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 (
|
|
|
|
<>
|
|
|
|
{' betting '}
|
|
|
|
<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
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function CommentInput(props: {
|
|
|
|
contract: Contract
|
|
|
|
betsByCurrentUser: Bet[]
|
|
|
|
commentsByCurrentUser: Comment[]
|
|
|
|
replyToUsername?: string
|
|
|
|
setRef?: (ref: HTMLTextAreaElement) => void
|
2022-06-08 13:24:12 +00:00
|
|
|
// Reply to a free response answer
|
|
|
|
parentAnswerOutcome?: string
|
|
|
|
// Reply to another comment
|
|
|
|
parentCommentId?: string
|
|
|
|
onSubmitComment?: () => void
|
2022-05-18 14:42:13 +00:00
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
contract,
|
|
|
|
betsByCurrentUser,
|
|
|
|
commentsByCurrentUser,
|
2022-06-08 13:24:12 +00:00
|
|
|
parentAnswerOutcome,
|
|
|
|
parentCommentId,
|
2022-05-18 14:42:13 +00:00
|
|
|
replyToUsername,
|
2022-06-08 13:24:12 +00:00
|
|
|
onSubmitComment,
|
2022-05-18 14:42:13 +00:00
|
|
|
setRef,
|
|
|
|
} = props
|
|
|
|
const user = useUser()
|
|
|
|
const [comment, setComment] = useState('')
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
|
|
|
betsByCurrentUser,
|
|
|
|
commentsByCurrentUser,
|
|
|
|
user,
|
2022-06-08 13:24:12 +00:00
|
|
|
parentAnswerOutcome
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
const { id } = mostRecentCommentableBet || { id: undefined }
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!replyToUsername || !user || replyToUsername === user.username) return
|
|
|
|
const replacement = `@${replyToUsername} `
|
2022-05-21 21:48:15 +00:00
|
|
|
setComment((comment) => replacement + comment.replace(replacement, ''))
|
2022-05-18 14:42:13 +00:00
|
|
|
}, [user, replyToUsername])
|
|
|
|
|
|
|
|
async function submitComment(betId: string | undefined) {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if (!comment || isSubmitting) return
|
|
|
|
setIsSubmitting(true)
|
|
|
|
await createComment(
|
|
|
|
contract.id,
|
|
|
|
comment,
|
|
|
|
user,
|
|
|
|
betId,
|
2022-06-08 13:24:12 +00:00
|
|
|
parentAnswerOutcome,
|
|
|
|
parentCommentId
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
2022-06-08 13:24:12 +00:00
|
|
|
onSubmitComment?.()
|
2022-05-18 14:42:13 +00:00
|
|
|
setComment('')
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
|
2022-05-19 01:38:02 +00:00
|
|
|
const { userPosition, outcome } = getBettorsLargestPositionBeforeTime(
|
2022-05-18 14:42:13 +00:00
|
|
|
contract,
|
|
|
|
Date.now(),
|
|
|
|
betsByCurrentUser
|
|
|
|
)
|
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
const isNumeric = contract.outcomeType === 'NUMERIC'
|
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-05-23 22:09:40 +00:00
|
|
|
<Row className={'mb-2 gap-1 sm:gap-2'}>
|
|
|
|
<div className={''}>
|
|
|
|
<Avatar
|
|
|
|
avatarUrl={user?.avatarUrl}
|
|
|
|
username={user?.username}
|
|
|
|
size={'sm'}
|
|
|
|
className={'ml-1'}
|
|
|
|
/>
|
2022-05-18 14:42:13 +00:00
|
|
|
</div>
|
|
|
|
<div className={'min-w-0 flex-1'}>
|
2022-05-23 22:09:40 +00:00
|
|
|
<div className="pl-0.5 text-sm text-gray-500">
|
2022-05-18 14:42:13 +00:00
|
|
|
<div className={'mb-1'}>
|
|
|
|
{mostRecentCommentableBet && (
|
|
|
|
<BetStatusText
|
|
|
|
contract={contract}
|
|
|
|
bet={mostRecentCommentableBet}
|
|
|
|
isSelf={true}
|
2022-05-19 17:42:03 +00:00
|
|
|
hideOutcome={
|
|
|
|
isNumeric || contract.outcomeType === 'FREE_RESPONSE'
|
|
|
|
}
|
2022-05-18 14:42:13 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-05-19 17:42:03 +00:00
|
|
|
{!mostRecentCommentableBet &&
|
|
|
|
user &&
|
|
|
|
userPosition > 0 &&
|
|
|
|
!isNumeric && (
|
|
|
|
<>
|
|
|
|
{"You're"}
|
|
|
|
<CommentStatus
|
|
|
|
outcome={outcome}
|
|
|
|
contract={contract}
|
|
|
|
prob={
|
|
|
|
contract.outcomeType === 'BINARY'
|
|
|
|
? getProbability(contract)
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
</div>
|
|
|
|
|
2022-06-08 13:24:12 +00:00
|
|
|
<Row className="gap-1.5 text-gray-700">
|
|
|
|
<Textarea
|
|
|
|
ref={setRef}
|
|
|
|
value={comment}
|
|
|
|
onChange={(e) => setComment(e.target.value)}
|
2022-05-23 22:09:40 +00:00
|
|
|
className={clsx(
|
2022-06-08 13:24:12 +00:00
|
|
|
'textarea textarea-bordered w-full resize-none'
|
2022-05-23 22:09:40 +00:00
|
|
|
)}
|
2022-06-08 16:34:37 +00:00
|
|
|
// Make room for floating submit button.
|
|
|
|
style={{ paddingRight: 48 }}
|
2022-06-08 13:24:12 +00:00
|
|
|
placeholder={
|
|
|
|
parentCommentId || parentAnswerOutcome
|
|
|
|
? 'Write a reply... '
|
|
|
|
: 'Write a comment...'
|
|
|
|
}
|
2022-06-09 16:15:34 +00:00
|
|
|
autoFocus={false}
|
2022-06-08 13:24:12 +00:00
|
|
|
maxLength={MAX_COMMENT_LENGTH}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
|
|
|
|
e.preventDefault()
|
|
|
|
submitComment(id)
|
|
|
|
e.currentTarget.blur()
|
2022-05-18 14:42:13 +00:00
|
|
|
}
|
2022-06-08 13:24:12 +00:00
|
|
|
}}
|
|
|
|
/>
|
2022-05-23 22:09:40 +00:00
|
|
|
|
2022-06-09 16:15:34 +00:00
|
|
|
<Col className={clsx('justify-end')}>
|
2022-05-18 16:08:31 +00:00
|
|
|
{user && !isSubmitting && (
|
2022-05-18 14:42:13 +00:00
|
|
|
<button
|
|
|
|
className={clsx(
|
2022-06-08 16:38:09 +00:00
|
|
|
'btn btn-ghost btn-sm absolute right-2 flex-row pl-2 capitalize',
|
2022-06-08 13:24:12 +00:00
|
|
|
parentCommentId || parentAnswerOutcome
|
|
|
|
? ' bottom-4'
|
|
|
|
: ' bottom-2',
|
2022-06-09 16:15:34 +00:00
|
|
|
!comment && 'pointer-events-none text-gray-500'
|
2022-05-18 14:42:13 +00:00
|
|
|
)}
|
|
|
|
onClick={() => {
|
2022-06-09 16:15:34 +00:00
|
|
|
submitComment(id)
|
2022-05-18 14:42:13 +00:00
|
|
|
}}
|
|
|
|
>
|
2022-06-09 16:15:34 +00:00
|
|
|
<PaperAirplaneIcon
|
|
|
|
className={'m-0 min-w-[22px] rotate-90 p-0 '}
|
|
|
|
height={25}
|
|
|
|
/>
|
2022-05-18 14:42:13 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2022-05-18 16:08:31 +00:00
|
|
|
{isSubmitting && (
|
|
|
|
<LoadingIndicator spinnerClassName={'border-gray-500'} />
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-06-08 13:24:12 +00:00
|
|
|
<Row>
|
|
|
|
{!user && (
|
|
|
|
<button
|
|
|
|
className={'btn btn-outline btn-sm mt-2 normal-case'}
|
|
|
|
onClick={() => submitComment(id)}
|
|
|
|
>
|
|
|
|
Sign in to comment
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Row>
|
2022-05-18 14:42:13 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function TruncatedComment(props: {
|
|
|
|
comment: string
|
|
|
|
moreHref: string
|
|
|
|
shouldTruncate?: boolean
|
|
|
|
}) {
|
|
|
|
const { comment, moreHref, shouldTruncate } = props
|
|
|
|
let truncated = comment
|
|
|
|
|
|
|
|
// Keep descriptions to at most 400 characters
|
|
|
|
const MAX_CHARS = 400
|
|
|
|
if (shouldTruncate && truncated.length > MAX_CHARS) {
|
|
|
|
truncated = truncated.slice(0, MAX_CHARS)
|
|
|
|
// Make sure to end on a space
|
|
|
|
const i = truncated.lastIndexOf(' ')
|
|
|
|
truncated = truncated.slice(0, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="mt-2 whitespace-pre-line break-words text-gray-700"
|
|
|
|
style={{ fontSize: 15 }}
|
|
|
|
>
|
|
|
|
<Linkify text={truncated} />
|
|
|
|
{truncated != comment && (
|
|
|
|
<SiteLink href={moreHref} className="text-indigo-700">
|
|
|
|
... (show more)
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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 emptyReturn = {
|
|
|
|
userPosition: 0,
|
|
|
|
outcome: '',
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
return emptyReturn
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|