Only allow user to comment on most recent bet

This commit is contained in:
Ian Philips 2022-05-17 15:01:12 -06:00
parent db4323294f
commit 2b11d717cd
3 changed files with 33 additions and 23 deletions

View File

@ -28,7 +28,7 @@ type BaseActivityItem = {
export type CommentInputItem = BaseActivityItem & {
type: 'commentInput'
betsByCurrentUser: Bet[]
comments: Comment[]
commentsByCurrentUser: Comment[]
answerOutcome?: string
}
@ -76,7 +76,7 @@ export type AnswerGroupItem = BaseActivityItem & {
answer: Answer
items: ActivityItem[]
betsByCurrentUser?: Bet[]
comments?: Comment[]
commentsByCurrentUser?: Comment[]
}
export type CloseItem = BaseActivityItem & {
@ -302,7 +302,9 @@ function getAnswerAndCommentInputGroups(
items,
user,
betsByCurrentUser: bets.filter((bet) => bet.userId === user?.id),
comments: answerComments,
commentsByCurrentUser: answerComments.filter(
(comment) => comment.userId === user?.id
),
}
})
.filter((group) => group.answer) as ActivityItem[]
@ -430,7 +432,7 @@ export function getAllContractActivityItems(
id: 'commentInput',
contract,
betsByCurrentUser: [],
comments: [],
commentsByCurrentUser: [],
})
} else {
items.push(
@ -456,7 +458,7 @@ export function getAllContractActivityItems(
id: 'commentInput',
contract,
betsByCurrentUser: [],
comments: [],
commentsByCurrentUser: [],
})
}
@ -571,10 +573,12 @@ export function getSpecificContractActivityItems(
type: 'commentInput',
id: 'commentInput',
contract,
betsByCurrentUser: user
? nonFreeResponseBets.filter((bet) => bet.userId === user.id)
: [],
comments: nonFreeResponseComments,
betsByCurrentUser: nonFreeResponseBets.filter(
(bet) => bet.userId === user?.id
),
commentsByCurrentUser: nonFreeResponseComments.filter(
(comment) => comment.userId === user?.id
),
})
break
case 'free-response-comment-answer-groups':

View File

@ -17,8 +17,11 @@ import { Linkify } from 'web/components/linkify'
import clsx from 'clsx'
import { tradingAllowed } from 'web/lib/firebase/contracts'
import { BuyButton } from 'web/components/yes-no-selector'
import { CommentInput, FeedItem } from 'web/components/feed/feed-items'
import { getMostRecentCommentableBet } from 'web/components/feed/feed-comments'
import { FeedItem } from 'web/components/feed/feed-items'
import {
CommentInput,
getMostRecentCommentableBet,
} from 'web/components/feed/feed-comments'
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
import { useRouter } from 'next/router'
@ -28,15 +31,16 @@ export function FeedAnswerCommentGroup(props: {
items: ActivityItem[]
type: string
betsByCurrentUser?: Bet[]
comments?: Comment[]
commentsByCurrentUser?: Comment[]
}) {
const { answer, items, contract, betsByCurrentUser, comments } = props
const { answer, items, contract, betsByCurrentUser, commentsByCurrentUser } =
props
const { username, avatarUrl, name, text } = answer
const answerElementId = `answer-${answer.id}`
const user = useUser()
const mostRecentCommentableBet = getMostRecentCommentableBet(
betsByCurrentUser ?? [],
comments ?? [],
commentsByCurrentUser ?? [],
user,
answer.number + ''
)
@ -44,7 +48,7 @@ export function FeedAnswerCommentGroup(props: {
const probPercent = formatPercent(prob)
const [open, setOpen] = useState(false)
const [showReply, setShowReply] = useState(false)
const isFreeResponseContractPage = comments
const isFreeResponseContractPage = commentsByCurrentUser
if (mostRecentCommentableBet && !showReply) setShowReplyAndFocus(true)
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
@ -174,7 +178,7 @@ export function FeedAnswerCommentGroup(props: {
<CommentInput
contract={contract}
betsByCurrentUser={betsByCurrentUser ?? []}
comments={comments ?? []}
commentsByCurrentUser={commentsByCurrentUser ?? []}
answerOutcome={answer.number + ''}
replyToUsername={answer.username}
setRef={setInputRef}

View File

@ -78,7 +78,7 @@ export function FeedCommentThread(props: {
contract={contract}
// Should we allow replies to contain recent bet info?
betsByCurrentUser={(user && betsByUserId[user.id]) ?? []}
comments={comments}
commentsByCurrentUser={comments}
parentComment={parentComment}
replyToUsername={replyToUsername}
answerOutcome={comments[0].answerOutcome}
@ -205,7 +205,7 @@ export function FeedComment(props: {
export function getMostRecentCommentableBet(
betsByCurrentUser: Bet[],
comments: Comment[],
commentsByCurrentUser: Comment[],
user?: User | null,
answerOutcome?: string
) {
@ -213,8 +213,10 @@ export function getMostRecentCommentableBet(
.filter((bet) => {
if (
canCommentOnBet(bet, user) &&
// The bet doesn't already have a comment
!comments.some((comment) => comment.betId == bet.id)
// There is no comment with a createdTime greater than this bet
!commentsByCurrentUser.some(
(comment) => comment.createdTime > bet.createdTime
)
) {
if (!answerOutcome) return true
// If we're in free response, don't allow commenting on ante bet
@ -229,7 +231,7 @@ export function getMostRecentCommentableBet(
export function CommentInput(props: {
contract: Contract
betsByCurrentUser: Bet[]
comments: Comment[]
commentsByCurrentUser: Comment[]
// Tie a comment to an free response answer outcome
answerOutcome?: string
// Tie a comment to another comment
@ -240,7 +242,7 @@ export function CommentInput(props: {
const {
contract,
betsByCurrentUser,
comments,
commentsByCurrentUser,
answerOutcome,
parentComment,
replyToUsername,
@ -252,7 +254,7 @@ export function CommentInput(props: {
const mostRecentCommentableBet = getMostRecentCommentableBet(
betsByCurrentUser,
comments,
commentsByCurrentUser,
user,
answerOutcome
)