2022-08-19 08:06:40 +00:00
|
|
|
import { ContractComment } from 'common/comment'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import React, { useEffect, useState } from 'react'
|
|
|
|
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: {
|
|
|
|
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-18 14:42:13 +00:00
|
|
|
}) {
|
2022-09-20 22:25:58 +00:00
|
|
|
const { contract, threadComments, 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]}
|
|
|
|
onReplyClick={scrollAndOpenReplyInput}
|
|
|
|
/>
|
|
|
|
))}
|
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}
|
|
|
|
parentCommentId={parentComment.id}
|
2022-08-30 09:41:47 +00:00
|
|
|
replyToUser={replyTo}
|
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-08-30 09:41:47 +00:00
|
|
|
indent?: boolean
|
2022-08-19 08:06:40 +00:00
|
|
|
onReplyClick?: (comment: ContractComment) => void
|
2022-05-18 14:42:13 +00:00
|
|
|
}) {
|
2022-09-18 22:57:50 +00:00
|
|
|
const { contract, comment, tips, indent, onReplyClick } = props
|
2022-05-18 14:42:13 +00:00
|
|
|
const {
|
2022-09-18 22:57:50 +00:00
|
|
|
text,
|
|
|
|
content,
|
|
|
|
userUsername,
|
|
|
|
userName,
|
|
|
|
userAvatarUrl,
|
|
|
|
commenterPositionProb,
|
|
|
|
commenterPositionShares,
|
|
|
|
commenterPositionOutcome,
|
|
|
|
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
|
|
|
|
|
|
|
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-18 22:57:50 +00:00
|
|
|
{comment.betId == null &&
|
|
|
|
commenterPositionProb != null &&
|
|
|
|
commenterPositionOutcome != null &&
|
|
|
|
commenterPositionShares != null &&
|
|
|
|
commenterPositionShares > 0 &&
|
2022-05-19 17:42:03 +00:00
|
|
|
contract.outcomeType !== 'NUMERIC' && (
|
|
|
|
<>
|
|
|
|
{'is '}
|
|
|
|
<CommentStatus
|
2022-09-18 22:57:50 +00:00
|
|
|
prob={commenterPositionProb}
|
|
|
|
outcome={commenterPositionOutcome}
|
2022-05-19 17:42:03 +00:00
|
|
|
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
|
|
|
|
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
|
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-20 22:25:58 +00:00
|
|
|
async function onSubmitComment(editor: Editor) {
|
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,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-09-12 21:50:31 +00:00
|
|
|
<CommentInput
|
|
|
|
replyToUser={props.replyToUser}
|
|
|
|
parentAnswerOutcome={props.parentAnswerOutcome}
|
|
|
|
parentCommentId={props.parentCommentId}
|
|
|
|
onSubmitComment={onSubmitComment}
|
|
|
|
className={props.className}
|
|
|
|
/>
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
}
|