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 { firebaseLogin } from 'web/lib/firebase/users'
|
2022-06-22 16:35:50 +00:00
|
|
|
import {
|
|
|
|
createCommentOnContract,
|
|
|
|
MAX_COMMENT_LENGTH,
|
|
|
|
} from 'web/lib/firebase/comments'
|
2022-05-18 14:42:13 +00:00
|
|
|
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-06-18 03:28:16 +00:00
|
|
|
import { Tipper } from '../tipper'
|
|
|
|
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
2022-07-13 15:12:43 +00:00
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { Content, TextEditor, useTextEditor } from '../editor'
|
|
|
|
import { Editor } from '@tiptap/react'
|
2022-05-18 14:42:13 +00:00
|
|
|
|
|
|
|
export function FeedCommentThread(props: {
|
|
|
|
contract: Contract
|
|
|
|
comments: Comment[]
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTipMap
|
2022-05-18 14:42:13 +00:00
|
|
|
parentComment: Comment
|
2022-05-19 01:38:02 +00:00
|
|
|
bets: Bet[]
|
2022-05-18 14:42:13 +00:00
|
|
|
smallAvatar?: boolean
|
|
|
|
}) {
|
2022-08-06 20:39:52 +00:00
|
|
|
const { contract, comments, bets, tips, smallAvatar, parentComment } = props
|
2022-05-18 14:42:13 +00:00
|
|
|
const [showReply, setShowReply] = useState(false)
|
2022-08-06 20:39:52 +00:00
|
|
|
const [replyToUser, setReplyToUser] =
|
|
|
|
useState<{ id: string; username: string }>()
|
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)
|
2022-08-06 20:39:52 +00:00
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
function scrollAndOpenReplyInput(comment: Comment) {
|
2022-08-06 20:39:52 +00:00
|
|
|
setReplyToUser({ 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-07-21 05:46:56 +00:00
|
|
|
<Col className={'w-full gap-3 pr-1'}>
|
2022-06-08 13:24:12 +00:00
|
|
|
<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}
|
2022-06-18 03:28:16 +00:00
|
|
|
tips={tips}
|
2022-06-08 13:24:12 +00:00
|
|
|
smallAvatar={smallAvatar}
|
|
|
|
bets={bets}
|
|
|
|
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
|
|
|
/>
|
|
|
|
{showReply && (
|
2022-07-21 05:46:56 +00:00
|
|
|
<Col className={'-pb-2 ml-6'}>
|
2022-06-08 13:24:12 +00:00
|
|
|
<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}
|
2022-08-06 20:39:52 +00:00
|
|
|
replyToUser={replyToUser}
|
2022-06-08 13:24:12 +00:00
|
|
|
parentAnswerOutcome={comments[0].answerOutcome}
|
2022-08-06 20:39:52 +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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function CommentRepliesList(props: {
|
|
|
|
contract: Contract
|
|
|
|
commentsList: Comment[]
|
|
|
|
betsByUserId: Dictionary<Bet[]>
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTipMap
|
2022-06-08 13:24:12 +00:00
|
|
|
scrollAndOpenReplyInput: (comment: Comment) => void
|
|
|
|
bets: Bet[]
|
|
|
|
treatFirstIndexEqually?: boolean
|
|
|
|
smallAvatar?: boolean
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
contract,
|
|
|
|
commentsList,
|
|
|
|
betsByUserId,
|
2022-06-18 03:28:16 +00:00
|
|
|
tips,
|
2022-06-08 13:24:12 +00:00
|
|
|
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',
|
2022-07-20 23:35:07 +00:00
|
|
|
!treatFirstIndexEqually && commentIdx === 0 ? '' : 'ml-6'
|
2022-06-08 13:24:12 +00:00
|
|
|
)}
|
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}
|
2022-06-18 03:28:16 +00:00
|
|
|
tips={tips[comment.id]}
|
2022-05-18 14:42:13 +00:00
|
|
|
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}
|
|
|
|
/>
|
|
|
|
</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
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTips
|
2022-05-18 14:42:13 +00:00
|
|
|
betsBySameUser: Bet[]
|
2022-05-19 01:38:02 +00:00
|
|
|
probAtCreatedTime?: number
|
2022-05-18 14:42:13 +00:00
|
|
|
smallAvatar?: boolean
|
|
|
|
onReplyClick?: (comment: Comment) => void
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
contract,
|
|
|
|
comment,
|
2022-06-18 03:28:16 +00:00
|
|
|
tips,
|
2022-05-18 14:42:13 +00:00
|
|
|
betsBySameUser,
|
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-05-18 14:42:13 +00:00
|
|
|
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-07-06 19:30:51 +00:00
|
|
|
'flex space-x-1.5 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-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-06 20:39:52 +00:00
|
|
|
<div className="mt-2 text-[15px] text-gray-700">
|
2022-08-10 02:04:55 +00:00
|
|
|
<Content content={content || text} smallImage />
|
2022-08-06 20:39:52 +00:00
|
|
|
</div>
|
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-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
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
//TODO: move commentinput and comment input text area into their own files
|
2022-05-18 14:42:13 +00:00
|
|
|
export function CommentInput(props: {
|
|
|
|
contract: Contract
|
|
|
|
betsByCurrentUser: Bet[]
|
|
|
|
commentsByCurrentUser: Comment[]
|
2022-08-06 20:39:52 +00:00
|
|
|
replyToUser?: { id: string; username: string }
|
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-08-06 20:39:52 +00:00
|
|
|
replyToUser,
|
2022-06-08 13:24:12 +00:00
|
|
|
onSubmitComment,
|
2022-05-18 14:42:13 +00:00
|
|
|
} = props
|
|
|
|
const user = useUser()
|
2022-08-06 20:39:52 +00:00
|
|
|
const { editor, upload } = useTextEditor({
|
|
|
|
simple: true,
|
|
|
|
max: MAX_COMMENT_LENGTH,
|
|
|
|
placeholder:
|
|
|
|
!!parentCommentId || !!parentAnswerOutcome
|
|
|
|
? 'Write a reply...'
|
|
|
|
: 'Write a comment...',
|
|
|
|
})
|
2022-05-18 14:42:13 +00:00
|
|
|
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 }
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2022-08-06 20:39:52 +00:00
|
|
|
if (!editor || editor.isEmpty || isSubmitting) return
|
2022-05-18 14:42:13 +00:00
|
|
|
setIsSubmitting(true)
|
2022-06-22 16:35:50 +00:00
|
|
|
await createCommentOnContract(
|
2022-05-18 14:42:13 +00:00
|
|
|
contract.id,
|
2022-08-06 20:39:52 +00:00
|
|
|
editor.getJSON(),
|
2022-05-18 14:42:13 +00:00
|
|
|
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
|
|
|
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'}>
|
2022-06-22 16:35:50 +00:00
|
|
|
<div className={'mt-2'}>
|
2022-05-23 22:09:40 +00:00
|
|
|
<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-08-10 02:04:55 +00:00
|
|
|
<div className="pl-0.5 text-sm">
|
|
|
|
<div className="mb-1 text-gray-500">
|
2022-05-18 14:42:13 +00:00
|
|
|
{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-22 16:35:50 +00:00
|
|
|
<CommentInputTextArea
|
2022-08-06 20:39:52 +00:00
|
|
|
editor={editor}
|
|
|
|
upload={upload}
|
|
|
|
replyToUser={replyToUser}
|
2022-06-22 16:35:50 +00:00
|
|
|
user={user}
|
|
|
|
submitComment={submitComment}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
presetId={id}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 14:42:13 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function CommentInputTextArea(props: {
|
|
|
|
user: User | undefined | null
|
2022-08-06 20:39:52 +00:00
|
|
|
replyToUser?: { id: string; username: string }
|
|
|
|
editor: Editor | null
|
|
|
|
upload: Parameters<typeof TextEditor>[0]['upload']
|
2022-06-22 16:35:50 +00:00
|
|
|
submitComment: (id?: string) => void
|
|
|
|
isSubmitting: boolean
|
2022-08-06 20:39:52 +00:00
|
|
|
submitOnEnter?: boolean
|
2022-06-22 16:35:50 +00:00
|
|
|
presetId?: string
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
user,
|
2022-08-06 20:39:52 +00:00
|
|
|
editor,
|
|
|
|
upload,
|
2022-06-22 16:35:50 +00:00
|
|
|
submitComment,
|
|
|
|
presetId,
|
|
|
|
isSubmitting,
|
2022-08-06 20:39:52 +00:00
|
|
|
submitOnEnter,
|
|
|
|
replyToUser,
|
2022-06-22 16:35:50 +00:00
|
|
|
} = props
|
2022-08-06 20:39:52 +00:00
|
|
|
const isMobile = (useWindowSize().width ?? 0) < 768 // TODO: base off input device (keybord vs touch)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
editor?.setEditable(!isSubmitting)
|
|
|
|
}, [isSubmitting, editor])
|
|
|
|
|
|
|
|
const submit = () => {
|
|
|
|
submitComment(presetId)
|
|
|
|
editor?.commands?.clearContent()
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
useEffect(() => {
|
2022-08-06 20:39:52 +00:00
|
|
|
if (!editor) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// submit on Enter key
|
|
|
|
editor.setOptions({
|
|
|
|
editorProps: {
|
|
|
|
handleKeyDown: (view, event) => {
|
|
|
|
if (
|
|
|
|
submitOnEnter &&
|
|
|
|
event.key === 'Enter' &&
|
|
|
|
!event.shiftKey &&
|
|
|
|
(!isMobile || event.ctrlKey || event.metaKey) &&
|
|
|
|
// mention list is closed
|
|
|
|
!(view.state as any).mention$.active
|
|
|
|
) {
|
|
|
|
submit()
|
|
|
|
event.preventDefault()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// insert at mention and focus
|
|
|
|
if (replyToUser) {
|
|
|
|
editor
|
|
|
|
.chain()
|
|
|
|
.setContent({
|
|
|
|
type: 'mention',
|
|
|
|
attrs: { label: replyToUser.username, id: replyToUser.id },
|
|
|
|
})
|
|
|
|
.insertContent(' ')
|
|
|
|
.focus()
|
|
|
|
.run()
|
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-08-06 20:39:52 +00:00
|
|
|
}, [editor])
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-08-06 20:39:52 +00:00
|
|
|
<div>
|
|
|
|
<TextEditor editor={editor} upload={upload}>
|
2022-06-22 16:35:50 +00:00
|
|
|
{user && !isSubmitting && (
|
|
|
|
<button
|
2022-08-06 20:39:52 +00:00
|
|
|
className="btn btn-ghost btn-sm px-2 disabled:bg-inherit disabled:text-gray-300"
|
|
|
|
disabled={!editor || editor.isEmpty}
|
|
|
|
onClick={submit}
|
2022-06-22 16:35:50 +00:00
|
|
|
>
|
2022-08-06 20:39:52 +00:00
|
|
|
<PaperAirplaneIcon className="m-0 h-[25px] min-w-[22px] rotate-90 p-0" />
|
2022-06-22 16:35:50 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2022-08-06 20:39:52 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
{isSubmitting && (
|
|
|
|
<LoadingIndicator spinnerClassName={'border-gray-500'} />
|
|
|
|
)}
|
2022-08-06 20:39:52 +00:00
|
|
|
</TextEditor>
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
<Row>
|
|
|
|
{!user && (
|
|
|
|
<button
|
|
|
|
className={'btn btn-outline btn-sm mt-2 normal-case'}
|
|
|
|
onClick={() => submitComment(presetId)}
|
|
|
|
>
|
|
|
|
Sign in to comment
|
|
|
|
</button>
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|