Show prob at time of comment on binary markets (#255)

* Show prob at time of comment on binary markets

* unused import
This commit is contained in:
Ian Philips 2022-05-18 19:38:02 -06:00 committed by GitHub
parent 6a30a41b10
commit e6dabf97d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 25 deletions

View File

@ -1,4 +1,4 @@
import _, { Dictionary } from 'lodash' import _ from 'lodash'
import { Answer } from 'common/answer' import { Answer } from 'common/answer'
import { Bet } from 'common/bet' import { Bet } from 'common/bet'
@ -54,6 +54,7 @@ export type CommentItem = BaseActivityItem & {
type: 'comment' type: 'comment'
comment: Comment comment: Comment
betsBySameUser: Bet[] betsBySameUser: Bet[]
probAtCreatedTime?: number
truncate?: boolean truncate?: boolean
smallAvatar?: boolean smallAvatar?: boolean
} }
@ -62,7 +63,7 @@ export type CommentThreadItem = BaseActivityItem & {
type: 'commentThread' type: 'commentThread'
parentComment: Comment parentComment: Comment
comments: Comment[] comments: Comment[]
betsByUserId: Dictionary<[Bet, ...Bet[]]> bets: Bet[]
} }
export type BetGroupItem = BaseActivityItem & { export type BetGroupItem = BaseActivityItem & {
@ -325,6 +326,7 @@ function groupBetsAndComments(
} }
) { ) {
const { smallAvatar, abbreviated, reversed } = options const { smallAvatar, abbreviated, reversed } = options
// Comments in feed don't show user's position?
const commentsWithoutBets = comments const commentsWithoutBets = comments
.filter((comment) => !comment.betId) .filter((comment) => !comment.betId)
.map((comment) => ({ .map((comment) => ({
@ -364,7 +366,6 @@ function getCommentThreads(
comments: Comment[], comments: Comment[],
contract: Contract contract: Contract
) { ) {
const betsByUserId = _.groupBy(bets, (bet) => bet.userId)
const parentComments = comments.filter((comment) => !comment.replyToCommentId) const parentComments = comments.filter((comment) => !comment.replyToCommentId)
const items = parentComments.map((comment) => ({ const items = parentComments.map((comment) => ({
@ -373,7 +374,7 @@ function getCommentThreads(
contract: contract, contract: contract,
comments: comments, comments: comments,
parentComment: comment, parentComment: comment,
betsByUserId: betsByUserId, bets: bets,
})) }))
return items return items

View File

@ -2,7 +2,6 @@ import { Bet } from 'common/bet'
import { Comment } from 'common/comment' import { Comment } from 'common/comment'
import { User } from 'common/user' import { User } from 'common/user'
import { Contract } from 'common/contract' import { Contract } from 'common/contract'
import { Dictionary } from 'lodash'
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { useUser } from 'web/hooks/use-user' import { useUser } from 'web/hooks/use-user'
import { formatMoney } from 'common/util/format' import { formatMoney } from 'common/util/format'
@ -22,27 +21,22 @@ import { Linkify } from 'web/components/linkify'
import { SiteLink } from 'web/components/site-link' import { SiteLink } from 'web/components/site-link'
import { BetStatusText } from 'web/components/feed/feed-bets' import { BetStatusText } from 'web/components/feed/feed-bets'
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { getOutcomeProbability } from 'common/calculate' import { getProbability } from 'common/calculate'
import { LoadingIndicator } from 'web/components/loading-indicator' import { LoadingIndicator } from 'web/components/loading-indicator'
export function FeedCommentThread(props: { export function FeedCommentThread(props: {
contract: Contract contract: Contract
comments: Comment[] comments: Comment[]
parentComment: Comment parentComment: Comment
betsByUserId: Dictionary<[Bet, ...Bet[]]> bets: Bet[]
truncate?: boolean truncate?: boolean
smallAvatar?: boolean smallAvatar?: boolean
}) { }) {
const { const { contract, comments, bets, truncate, smallAvatar, parentComment } =
contract, props
comments,
betsByUserId,
truncate,
smallAvatar,
parentComment,
} = props
const [showReply, setShowReply] = useState(false) const [showReply, setShowReply] = useState(false)
const [replyToUsername, setReplyToUsername] = useState('') const [replyToUsername, setReplyToUsername] = useState('')
const betsByUserId = _.groupBy(bets, (bet) => bet.userId)
const user = useUser() const user = useUser()
const commentsList = comments.filter( const commentsList = comments.filter(
(comment) => (comment) =>
@ -71,6 +65,15 @@ export function FeedCommentThread(props: {
comment={comment} comment={comment}
betsBySameUser={betsByUserId[comment.userId] ?? []} betsBySameUser={betsByUserId[comment.userId] ?? []}
onReplyClick={scrollAndOpenReplyInput} onReplyClick={scrollAndOpenReplyInput}
probAtCreatedTime={
contract.outcomeType === 'BINARY'
? _.minBy(bets, (bet) => {
return bet.createdTime < comment.createdTime
? comment.createdTime - bet.createdTime
: comment.createdTime
})?.probAfter
: undefined
}
smallAvatar={smallAvatar} smallAvatar={smallAvatar}
truncate={truncate} truncate={truncate}
/> />
@ -97,6 +100,7 @@ export function FeedComment(props: {
contract: Contract contract: Contract
comment: Comment comment: Comment
betsBySameUser: Bet[] betsBySameUser: Bet[]
probAtCreatedTime?: number
truncate?: boolean truncate?: boolean
smallAvatar?: boolean smallAvatar?: boolean
onReplyClick?: (comment: Comment) => void onReplyClick?: (comment: Comment) => void
@ -105,6 +109,7 @@ export function FeedComment(props: {
contract, contract,
comment, comment,
betsBySameUser, betsBySameUser,
probAtCreatedTime,
truncate, truncate,
smallAvatar, smallAvatar,
onReplyClick, onReplyClick,
@ -130,7 +135,7 @@ export function FeedComment(props: {
}, [router.asPath]) }, [router.asPath])
// Only calculated if they don't have a matching bet // Only calculated if they don't have a matching bet
const { userPosition, outcome } = getBettorsPosition( const { userPosition, outcome } = getBettorsLargestPositionBeforeTime(
contract, contract,
comment.createdTime, comment.createdTime,
matchedBet ? [] : betsBySameUser matchedBet ? [] : betsBySameUser
@ -159,7 +164,11 @@ export function FeedComment(props: {
{!matchedBet && userPosition > 0 && ( {!matchedBet && userPosition > 0 && (
<> <>
{'is '} {'is '}
<CommentStatus outcome={outcome} contract={contract} /> <CommentStatus
prob={probAtCreatedTime}
outcome={outcome}
contract={contract}
/>
</> </>
)} )}
<> <>
@ -224,15 +233,17 @@ export function getMostRecentCommentableBet(
.pop() .pop()
} }
function CommentStatus(props: { contract: Contract; outcome: string }) { function CommentStatus(props: {
const { contract, outcome } = props contract: Contract
outcome: string
prob?: number
}) {
const { contract, outcome, prob } = props
return ( return (
<> <>
{' betting '} {' betting '}
<OutcomeLabel outcome={outcome} contract={contract} truncate="short" /> <OutcomeLabel outcome={outcome} contract={contract} truncate="short" />
{' at ' + {prob && ' at ' + Math.round(prob * 100) + '%'}
Math.round(getOutcomeProbability(contract, outcome) * 100) +
'%'}
</> </>
) )
} }
@ -295,7 +306,7 @@ export function CommentInput(props: {
setIsSubmitting(false) setIsSubmitting(false)
} }
const { userPosition, outcome } = getBettorsPosition( const { userPosition, outcome } = getBettorsLargestPositionBeforeTime(
contract, contract,
Date.now(), Date.now(),
betsByCurrentUser betsByCurrentUser
@ -323,7 +334,15 @@ export function CommentInput(props: {
{!mostRecentCommentableBet && user && userPosition > 0 && ( {!mostRecentCommentableBet && user && userPosition > 0 && (
<> <>
{"You're"} {"You're"}
<CommentStatus outcome={outcome} contract={contract} /> <CommentStatus
outcome={outcome}
contract={contract}
prob={
contract.outcomeType === 'BINARY'
? getProbability(contract)
: undefined
}
/>
</> </>
)} )}
</div> </div>
@ -435,7 +454,7 @@ export function TruncatedComment(props: {
) )
} }
function getBettorsPosition( function getBettorsLargestPositionBeforeTime(
contract: Contract, contract: Contract,
createdTime: number, createdTime: number,
bets: Bet[] bets: Bet[]