2022-01-04 07:21:14 +00:00
|
|
|
// From https://tailwindui.com/components/application-ui/lists/feeds
|
2022-01-19 07:33:46 +00:00
|
|
|
import { ReactChild, useState } from 'react'
|
2022-01-18 03:36:33 +00:00
|
|
|
import _ from 'lodash'
|
2022-01-04 07:21:14 +00:00
|
|
|
import {
|
|
|
|
BanIcon,
|
|
|
|
ChatAltIcon,
|
|
|
|
CheckIcon,
|
2022-01-22 17:46:59 +00:00
|
|
|
DotsVerticalIcon,
|
2022-01-04 07:21:14 +00:00
|
|
|
LockClosedIcon,
|
|
|
|
StarIcon,
|
|
|
|
UserIcon,
|
|
|
|
UsersIcon,
|
|
|
|
XIcon,
|
|
|
|
} from '@heroicons/react/solid'
|
2022-01-18 03:36:33 +00:00
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime'
|
2022-01-18 03:36:33 +00:00
|
|
|
dayjs.extend(relativeTime)
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
import { OutcomeLabel } from './outcome-label'
|
2022-01-11 16:56:26 +00:00
|
|
|
import {
|
2022-01-12 19:01:04 +00:00
|
|
|
contractMetrics,
|
2022-01-11 16:56:26 +00:00
|
|
|
Contract,
|
2022-01-12 19:01:04 +00:00
|
|
|
contractPath,
|
2022-01-11 16:56:26 +00:00
|
|
|
updateContract,
|
|
|
|
} from '../lib/firebase/contracts'
|
2022-01-04 07:21:14 +00:00
|
|
|
import { useUser } from '../hooks/use-user'
|
|
|
|
import { Linkify } from './linkify'
|
|
|
|
import { Row } from './layout/row'
|
|
|
|
import { createComment } from '../lib/firebase/comments'
|
|
|
|
import { useComments } from '../hooks/use-comments'
|
2022-01-04 15:55:34 +00:00
|
|
|
import { formatMoney } from '../lib/util/format'
|
2022-01-11 16:56:26 +00:00
|
|
|
import { ResolutionOrChance } from './contract-card'
|
|
|
|
import { SiteLink } from './site-link'
|
2022-01-11 20:57:44 +00:00
|
|
|
import { Col } from './layout/col'
|
2022-01-13 21:16:47 +00:00
|
|
|
import { UserLink } from './user-page'
|
2022-01-16 09:16:15 +00:00
|
|
|
import { DateTimeTooltip } from './datetime-tooltip'
|
2022-01-18 03:36:33 +00:00
|
|
|
import { useBets } from '../hooks/use-bets'
|
|
|
|
import { Bet, withoutAnteBets } from '../lib/firebase/bets'
|
|
|
|
import { Comment, mapCommentsByBetId } from '../lib/firebase/comments'
|
|
|
|
import { JoinSpans } from './join-spans'
|
2022-01-19 06:43:54 +00:00
|
|
|
import Textarea from 'react-expanding-textarea'
|
2022-01-23 00:14:41 +00:00
|
|
|
import { outcome } from '../../common/contract'
|
2022-01-04 07:21:14 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
export function AvatarWithIcon(props: { username: string; avatarUrl: string }) {
|
2022-01-19 07:54:00 +00:00
|
|
|
const { username, avatarUrl } = props
|
2022-01-19 07:33:46 +00:00
|
|
|
return (
|
|
|
|
<SiteLink className="relative" href={`/${username}`}>
|
|
|
|
<img
|
2022-01-23 00:26:56 +00:00
|
|
|
className="rounded-full bg-gray-400 flex items-center justify-center"
|
2022-01-19 07:33:46 +00:00
|
|
|
src={avatarUrl}
|
2022-01-23 00:26:56 +00:00
|
|
|
width={40}
|
|
|
|
height={40}
|
2022-01-19 07:33:46 +00:00
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</SiteLink>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-21 18:53:51 +00:00
|
|
|
function FeedComment(props: {
|
|
|
|
activityItem: any
|
|
|
|
moreHref: string
|
|
|
|
feedType: 'activity' | 'market'
|
|
|
|
}) {
|
|
|
|
const { activityItem, moreHref, feedType } = props
|
2022-01-04 07:21:14 +00:00
|
|
|
const { person, text, amount, outcome, createdTime } = activityItem
|
2022-01-18 03:36:33 +00:00
|
|
|
|
|
|
|
const bought = amount >= 0 ? 'bought' : 'sold'
|
|
|
|
const money = formatMoney(Math.abs(amount))
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-01-19 07:54:00 +00:00
|
|
|
<AvatarWithIcon username={person.username} avatarUrl={person.avatarUrl} />
|
2022-01-04 07:21:14 +00:00
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
<div>
|
|
|
|
<p className="mt-0.5 text-sm text-gray-500">
|
2022-01-13 21:24:14 +00:00
|
|
|
<UserLink
|
|
|
|
className="text-gray-500"
|
|
|
|
username={person.username}
|
|
|
|
name={person.name}
|
|
|
|
/>{' '}
|
2022-01-18 03:36:33 +00:00
|
|
|
{bought} {money} of <OutcomeLabel outcome={outcome} />{' '}
|
2022-01-04 07:21:14 +00:00
|
|
|
<Timestamp time={createdTime} />
|
|
|
|
</p>
|
|
|
|
</div>
|
2022-01-21 18:53:51 +00:00
|
|
|
<TruncatedComment
|
|
|
|
comment={text}
|
|
|
|
moreHref={moreHref}
|
|
|
|
shouldTruncate={feedType == 'activity'}
|
|
|
|
/>
|
2022-01-04 07:21:14 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Timestamp(props: { time: number }) {
|
|
|
|
const { time } = props
|
|
|
|
return (
|
2022-01-16 09:16:15 +00:00
|
|
|
<DateTimeTooltip time={time}>
|
|
|
|
<span className="whitespace-nowrap text-gray-400 ml-1">
|
|
|
|
{dayjs(time).fromNow()}
|
|
|
|
</span>
|
|
|
|
</DateTimeTooltip>
|
2022-01-04 07:21:14 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function FeedBet(props: { activityItem: any }) {
|
|
|
|
const { activityItem } = props
|
|
|
|
const { id, contractId, amount, outcome, createdTime } = activityItem
|
|
|
|
const user = useUser()
|
|
|
|
const isCreator = user?.id == activityItem.userId
|
2022-01-12 06:47:56 +00:00
|
|
|
// The creator can comment if the bet was posted in the last hour
|
|
|
|
const canComment = isCreator && Date.now() - createdTime < 60 * 60 * 1000
|
2022-01-04 07:21:14 +00:00
|
|
|
|
|
|
|
const [comment, setComment] = useState('')
|
|
|
|
async function submitComment() {
|
|
|
|
if (!user || !comment) return
|
|
|
|
await createComment(contractId, id, comment, user)
|
|
|
|
}
|
2022-01-18 03:36:33 +00:00
|
|
|
|
|
|
|
const bought = amount >= 0 ? 'bought' : 'sold'
|
|
|
|
const money = formatMoney(Math.abs(amount))
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<div className="relative px-1">
|
2022-01-19 08:15:09 +00:00
|
|
|
<div className="h-8 w-8 bg-gray-200 rounded-full flex items-center justify-center">
|
2022-01-04 07:21:14 +00:00
|
|
|
<UserIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1 py-1.5">
|
|
|
|
<div className="text-sm text-gray-500">
|
2022-01-18 03:36:33 +00:00
|
|
|
<span>{isCreator ? 'You' : 'A trader'}</span> {bought} {money} of{' '}
|
|
|
|
<OutcomeLabel outcome={outcome} /> <Timestamp time={createdTime} />
|
2022-01-12 06:47:56 +00:00
|
|
|
{canComment && (
|
2022-01-04 07:21:14 +00:00
|
|
|
// Allow user to comment in an textarea if they are the creator
|
|
|
|
<div className="mt-2">
|
2022-01-19 06:43:54 +00:00
|
|
|
<Textarea
|
2022-01-04 07:21:14 +00:00
|
|
|
value={comment}
|
|
|
|
onChange={(e) => setComment(e.target.value)}
|
|
|
|
className="textarea textarea-bordered w-full"
|
|
|
|
placeholder="Add a comment..."
|
2022-01-19 06:43:54 +00:00
|
|
|
rows={3}
|
2022-01-04 07:21:14 +00:00
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className="btn btn-outline btn-sm mt-1"
|
|
|
|
onClick={submitComment}
|
|
|
|
>
|
|
|
|
Comment
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ContractDescription(props: {
|
|
|
|
contract: Contract
|
|
|
|
isCreator: boolean
|
|
|
|
}) {
|
|
|
|
const { contract, isCreator } = props
|
|
|
|
const [editing, setEditing] = useState(false)
|
|
|
|
const editStatement = () => `${dayjs().format('MMM D, h:mma')}: `
|
|
|
|
const [description, setDescription] = useState(editStatement())
|
|
|
|
|
|
|
|
// Append the new description (after a newline)
|
|
|
|
async function saveDescription(e: any) {
|
|
|
|
e.preventDefault()
|
|
|
|
setEditing(false)
|
2022-01-07 19:27:59 +00:00
|
|
|
|
2022-01-09 05:09:19 +00:00
|
|
|
const newDescription = `${contract.description}\n\n${description}`.trim()
|
2022-01-07 19:27:59 +00:00
|
|
|
await updateContract(contract.id, { description: newDescription })
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
setDescription(editStatement())
|
|
|
|
}
|
|
|
|
|
2022-01-11 20:57:44 +00:00
|
|
|
if (!isCreator && !contract.description.trim()) return null
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
return (
|
|
|
|
<div className="whitespace-pre-line break-words mt-2 text-gray-700">
|
|
|
|
<Linkify text={contract.description} />
|
|
|
|
<br />
|
|
|
|
{isCreator &&
|
|
|
|
(editing ? (
|
|
|
|
<form className="mt-4">
|
2022-01-19 06:43:54 +00:00
|
|
|
<Textarea
|
2022-01-04 07:21:14 +00:00
|
|
|
className="textarea h-24 textarea-bordered w-full mb-1"
|
2022-01-19 06:43:54 +00:00
|
|
|
rows={3}
|
2022-01-04 07:21:14 +00:00
|
|
|
value={description}
|
|
|
|
onChange={(e) => setDescription(e.target.value || '')}
|
|
|
|
autoFocus
|
|
|
|
onFocus={(e) =>
|
|
|
|
// Focus starts at end of description.
|
|
|
|
e.target.setSelectionRange(
|
|
|
|
description.length,
|
|
|
|
description.length
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<Row className="gap-2">
|
|
|
|
<button
|
|
|
|
className="btn btn-neutral btn-outline btn-sm"
|
|
|
|
onClick={saveDescription}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="btn btn-error btn-outline btn-sm"
|
|
|
|
onClick={() => setEditing(false)}
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</button>
|
|
|
|
</Row>
|
|
|
|
</form>
|
|
|
|
) : (
|
|
|
|
<Row>
|
|
|
|
<button
|
|
|
|
className="btn btn-neutral btn-outline btn-sm mt-4"
|
|
|
|
onClick={() => setEditing(true)}
|
|
|
|
>
|
|
|
|
Add to description
|
|
|
|
</button>
|
|
|
|
</Row>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-21 18:53:51 +00:00
|
|
|
function TruncatedComment(props: {
|
|
|
|
comment: string
|
|
|
|
moreHref: string
|
|
|
|
shouldTruncate?: boolean
|
|
|
|
}) {
|
|
|
|
const { comment, moreHref, shouldTruncate } = props
|
|
|
|
let truncated = comment
|
|
|
|
|
|
|
|
// Keep descriptions to at most 400 characters
|
2022-01-22 17:46:59 +00:00
|
|
|
const MAX_CHARS = 400
|
|
|
|
if (shouldTruncate && truncated.length > MAX_CHARS) {
|
|
|
|
truncated = truncated.slice(0, MAX_CHARS)
|
2022-01-21 18:53:51 +00:00
|
|
|
// Make sure to end on a space
|
|
|
|
const i = truncated.lastIndexOf(' ')
|
|
|
|
truncated = truncated.slice(0, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="whitespace-pre-line break-words mt-2 text-gray-700">
|
|
|
|
<Linkify text={truncated} />
|
|
|
|
{truncated != comment && (
|
|
|
|
<SiteLink href={moreHref} className="text-indigo-700">
|
|
|
|
... (show more)
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-11 16:56:26 +00:00
|
|
|
function FeedQuestion(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
2022-01-13 21:16:47 +00:00
|
|
|
const { creatorName, creatorUsername, createdTime, question, resolution } =
|
|
|
|
contract
|
2022-01-12 19:01:04 +00:00
|
|
|
const { probPercent } = contractMetrics(contract)
|
2022-01-11 16:56:26 +00:00
|
|
|
|
2022-01-19 07:10:54 +00:00
|
|
|
// Currently hidden on mobile; ideally we'd fit this in somewhere.
|
|
|
|
const closeMessage =
|
|
|
|
contract.isResolved || !contract.closeTime ? null : (
|
|
|
|
<span className="float-right text-gray-400 hidden sm:inline">
|
|
|
|
{contract.closeTime > Date.now() ? 'Closes' : 'Closed'}
|
|
|
|
<Timestamp time={contract.closeTime || 0} />
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
|
2022-01-11 16:56:26 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-01-19 07:33:46 +00:00
|
|
|
{contract.creatorAvatarUrl ? (
|
|
|
|
<AvatarWithIcon
|
|
|
|
username={contract.creatorUsername}
|
|
|
|
avatarUrl={contract.creatorAvatarUrl}
|
2022-01-19 07:54:00 +00:00
|
|
|
/>
|
2022-01-19 07:33:46 +00:00
|
|
|
) : (
|
|
|
|
// TODO: After 2022-03-01, can just assume that all contracts have an avatarUrl
|
2022-01-11 16:56:26 +00:00
|
|
|
<div className="relative px-1">
|
2022-01-19 08:15:09 +00:00
|
|
|
<div className="h-8 w-8 bg-gray-200 rounded-full flex items-center justify-center">
|
2022-01-11 16:56:26 +00:00
|
|
|
<StarIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-01-19 07:33:46 +00:00
|
|
|
)}
|
2022-01-11 16:56:26 +00:00
|
|
|
<div className="min-w-0 flex-1 py-1.5">
|
2022-01-11 20:57:44 +00:00
|
|
|
<div className="text-sm text-gray-500 mb-2">
|
2022-01-13 21:16:47 +00:00
|
|
|
<UserLink
|
|
|
|
className="text-gray-900"
|
|
|
|
name={creatorName}
|
|
|
|
username={creatorUsername}
|
|
|
|
/>{' '}
|
2022-01-22 00:10:38 +00:00
|
|
|
asked
|
2022-01-19 07:10:54 +00:00
|
|
|
{closeMessage}
|
2022-01-11 16:56:26 +00:00
|
|
|
</div>
|
2022-01-19 07:10:54 +00:00
|
|
|
<Col className="items-start sm:flex-row justify-between gap-2 sm:gap-4 mb-4">
|
2022-01-11 20:57:44 +00:00
|
|
|
<SiteLink
|
2022-01-12 19:01:04 +00:00
|
|
|
href={contractPath(contract)}
|
2022-01-11 20:57:44 +00:00
|
|
|
className="text-lg sm:text-xl text-indigo-700"
|
|
|
|
>
|
2022-01-13 21:16:47 +00:00
|
|
|
{question}
|
2022-01-11 16:56:26 +00:00
|
|
|
</SiteLink>
|
|
|
|
<ResolutionOrChance
|
|
|
|
className="items-center"
|
2022-01-13 21:16:47 +00:00
|
|
|
resolution={resolution}
|
2022-01-11 16:56:26 +00:00
|
|
|
probPercent={probPercent}
|
|
|
|
/>
|
2022-01-11 20:57:44 +00:00
|
|
|
</Col>
|
2022-01-21 18:53:51 +00:00
|
|
|
<TruncatedComment
|
|
|
|
comment={contract.description}
|
|
|
|
moreHref={contractPath(contract)}
|
|
|
|
shouldTruncate
|
|
|
|
/>
|
2022-01-11 16:56:26 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function FeedDescription(props: { contract: Contract }) {
|
2022-01-04 07:21:14 +00:00
|
|
|
const { contract } = props
|
2022-01-13 21:16:47 +00:00
|
|
|
const { creatorName, creatorUsername } = contract
|
2022-01-04 07:21:14 +00:00
|
|
|
const user = useUser()
|
|
|
|
const isCreator = user?.id === contract.creatorId
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-01-19 08:16:22 +00:00
|
|
|
{contract.creatorAvatarUrl ? (
|
|
|
|
<AvatarWithIcon
|
|
|
|
username={contract.creatorUsername}
|
|
|
|
avatarUrl={contract.creatorAvatarUrl}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
// TODO: After 2022-03-01, can just assume that all contracts have an avatarUrl
|
2022-01-04 07:21:14 +00:00
|
|
|
<div className="relative px-1">
|
2022-01-19 08:15:09 +00:00
|
|
|
<div className="h-8 w-8 bg-gray-200 rounded-full flex items-center justify-center">
|
2022-01-04 07:21:14 +00:00
|
|
|
<StarIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-01-19 08:16:22 +00:00
|
|
|
)}
|
2022-01-04 07:21:14 +00:00
|
|
|
<div className="min-w-0 flex-1 py-1.5">
|
|
|
|
<div className="text-sm text-gray-500">
|
2022-01-13 21:16:47 +00:00
|
|
|
<UserLink
|
|
|
|
className="text-gray-900"
|
|
|
|
name={creatorName}
|
|
|
|
username={creatorUsername}
|
|
|
|
/>{' '}
|
|
|
|
created this market <Timestamp time={contract.createdTime} />
|
2022-01-04 07:21:14 +00:00
|
|
|
</div>
|
|
|
|
<ContractDescription contract={contract} isCreator={isCreator} />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-23 00:14:41 +00:00
|
|
|
function OutcomeIcon(props: { outcome?: outcome }) {
|
2022-01-04 07:21:14 +00:00
|
|
|
const { outcome } = props
|
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
return <CheckIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
|
|
|
case 'NO':
|
|
|
|
return <XIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
|
|
|
case 'CANCEL':
|
|
|
|
default:
|
|
|
|
return <BanIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function FeedResolve(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
2022-01-13 21:16:47 +00:00
|
|
|
const { creatorName, creatorUsername } = contract
|
2022-01-04 07:21:14 +00:00
|
|
|
const resolution = contract.resolution || 'CANCEL'
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<div className="relative px-1">
|
2022-01-19 08:15:09 +00:00
|
|
|
<div className="h-8 w-8 bg-gray-200 rounded-full flex items-center justify-center">
|
2022-01-04 07:21:14 +00:00
|
|
|
<OutcomeIcon outcome={resolution} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1 py-1.5">
|
|
|
|
<div className="text-sm text-gray-500">
|
2022-01-13 21:16:47 +00:00
|
|
|
<UserLink
|
|
|
|
className="text-gray-900"
|
|
|
|
name={creatorName}
|
|
|
|
username={creatorUsername}
|
|
|
|
/>{' '}
|
|
|
|
resolved this market to <OutcomeLabel outcome={resolution} />{' '}
|
2022-01-04 07:21:14 +00:00
|
|
|
<Timestamp time={contract.resolutionTime || 0} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function FeedClose(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<div className="relative px-1">
|
2022-01-19 08:15:09 +00:00
|
|
|
<div className="h-8 w-8 bg-gray-200 rounded-full flex items-center justify-center">
|
2022-01-04 07:21:14 +00:00
|
|
|
<LockClosedIcon
|
|
|
|
className="h-5 w-5 text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1 py-1.5">
|
|
|
|
<div className="text-sm text-gray-500">
|
|
|
|
Trading closed in this market{' '}
|
|
|
|
<Timestamp time={contract.closeTime || 0} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFeedBet(bet: Bet) {
|
|
|
|
return {
|
|
|
|
id: bet.id,
|
|
|
|
contractId: bet.contractId,
|
|
|
|
userId: bet.userId,
|
|
|
|
type: 'bet',
|
2022-01-10 17:38:44 +00:00
|
|
|
amount: bet.sale ? -bet.sale.amount : bet.amount,
|
2022-01-04 07:21:14 +00:00
|
|
|
outcome: bet.outcome,
|
|
|
|
createdTime: bet.createdTime,
|
|
|
|
date: dayjs(bet.createdTime).fromNow(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFeedComment(bet: Bet, comment: Comment) {
|
|
|
|
return {
|
|
|
|
id: bet.id,
|
|
|
|
contractId: bet.contractId,
|
|
|
|
userId: bet.userId,
|
|
|
|
type: 'comment',
|
2022-01-10 17:38:44 +00:00
|
|
|
amount: bet.sale ? -bet.sale.amount : bet.amount,
|
2022-01-04 07:21:14 +00:00
|
|
|
outcome: bet.outcome,
|
|
|
|
createdTime: bet.createdTime,
|
|
|
|
date: dayjs(bet.createdTime).fromNow(),
|
|
|
|
|
|
|
|
// Invariant: bet.comment exists
|
|
|
|
text: comment.text,
|
|
|
|
person: {
|
2022-01-13 21:24:14 +00:00
|
|
|
username: comment.userUsername,
|
2022-01-04 07:21:14 +00:00
|
|
|
name: comment.userName,
|
|
|
|
avatarUrl: comment.userAvatarUrl,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 16:56:26 +00:00
|
|
|
const DAY_IN_MS = 24 * 60 * 60 * 1000
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
// Group together bets that are:
|
2022-01-11 16:56:26 +00:00
|
|
|
// - Within `windowMs` of the first in the group
|
2022-01-04 07:21:14 +00:00
|
|
|
// - Do not have a comment
|
|
|
|
// - Were not created by this user
|
|
|
|
// Return a list of ActivityItems
|
2022-01-11 16:56:26 +00:00
|
|
|
function groupBets(
|
|
|
|
bets: Bet[],
|
|
|
|
comments: Comment[],
|
|
|
|
windowMs: number,
|
|
|
|
userId?: string
|
|
|
|
) {
|
2022-01-04 07:21:14 +00:00
|
|
|
const commentsMap = mapCommentsByBetId(comments)
|
|
|
|
const items: any[] = []
|
|
|
|
let group: Bet[] = []
|
|
|
|
|
|
|
|
// Turn the current group into an ActivityItem
|
|
|
|
function pushGroup() {
|
|
|
|
if (group.length == 1) {
|
|
|
|
items.push(toActivityItem(group[0]))
|
|
|
|
} else if (group.length > 1) {
|
|
|
|
items.push({ type: 'betgroup', bets: [...group], id: group[0].id })
|
|
|
|
}
|
|
|
|
group = []
|
|
|
|
}
|
|
|
|
|
|
|
|
function toActivityItem(bet: Bet) {
|
|
|
|
const comment = commentsMap[bet.id]
|
|
|
|
return comment ? toFeedComment(bet, comment) : toFeedBet(bet)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const bet of bets) {
|
|
|
|
const isCreator = userId === bet.userId
|
|
|
|
|
|
|
|
if (commentsMap[bet.id] || isCreator) {
|
|
|
|
pushGroup()
|
|
|
|
// Create a single item for this
|
|
|
|
items.push(toActivityItem(bet))
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
group.length > 0 &&
|
2022-01-11 16:56:26 +00:00
|
|
|
bet.createdTime - group[0].createdTime > windowMs
|
2022-01-04 07:21:14 +00:00
|
|
|
) {
|
2022-01-11 16:56:26 +00:00
|
|
|
// More than `windowMs` has passed; start a new group
|
2022-01-04 07:21:14 +00:00
|
|
|
pushGroup()
|
|
|
|
}
|
|
|
|
group.push(bet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (group.length > 0) {
|
|
|
|
pushGroup()
|
|
|
|
}
|
|
|
|
return items as ActivityItem[]
|
|
|
|
}
|
|
|
|
|
2022-01-18 03:36:33 +00:00
|
|
|
function BetGroupSpan(props: { bets: Bet[]; outcome: 'YES' | 'NO' }) {
|
|
|
|
const { bets, outcome } = props
|
|
|
|
|
|
|
|
const numberTraders = _.uniqBy(bets, (b) => b.userId).length
|
|
|
|
|
|
|
|
const [buys, sells] = _.partition(bets, (bet) => bet.amount >= 0)
|
|
|
|
const buyTotal = _.sumBy(buys, (b) => b.amount)
|
|
|
|
const sellTotal = _.sumBy(sells, (b) => -b.amount)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{numberTraders} {numberTraders > 1 ? 'traders' : 'trader'}{' '}
|
|
|
|
<JoinSpans>
|
|
|
|
{buyTotal > 0 && <>bought {formatMoney(buyTotal)} </>}
|
|
|
|
{sellTotal > 0 && <>sold {formatMoney(sellTotal)} </>}
|
|
|
|
</JoinSpans>
|
|
|
|
of <OutcomeLabel outcome={outcome} />
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
// TODO: Make this expandable to show all grouped bets?
|
|
|
|
function FeedBetGroup(props: { activityItem: any }) {
|
|
|
|
const { activityItem } = props
|
|
|
|
const bets: Bet[] = activityItem.bets
|
|
|
|
|
2022-01-18 03:36:33 +00:00
|
|
|
const [yesBets, noBets] = _.partition(bets, (bet) => bet.outcome === 'YES')
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
const createdTime = bets[0].createdTime
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<div className="relative px-1">
|
2022-01-19 08:15:09 +00:00
|
|
|
<div className="h-8 w-8 bg-gray-200 rounded-full flex items-center justify-center">
|
2022-01-19 07:54:00 +00:00
|
|
|
<UsersIcon className="h-5 w-5 text-gray-500" aria-hidden="true" />
|
2022-01-04 07:21:14 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-01-19 07:54:00 +00:00
|
|
|
<div className="min-w-0 flex-1">
|
2022-01-04 07:21:14 +00:00
|
|
|
<div className="text-sm text-gray-500">
|
2022-01-18 03:36:33 +00:00
|
|
|
{yesBets.length > 0 && <BetGroupSpan outcome="YES" bets={yesBets} />}
|
|
|
|
{yesBets.length > 0 && noBets.length > 0 && <br />}
|
|
|
|
{noBets.length > 0 && <BetGroupSpan outcome="NO" bets={noBets} />}
|
|
|
|
<Timestamp time={createdTime} />
|
2022-01-04 07:21:14 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:46:59 +00:00
|
|
|
// TODO: Should highlight the entire Feed segment
|
|
|
|
function FeedExpand(props: { setExpanded: (expanded: boolean) => void }) {
|
|
|
|
const { setExpanded } = props
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<button onClick={() => setExpanded(true)}>
|
|
|
|
<div className="relative px-1">
|
|
|
|
<div className="h-8 w-8 bg-gray-200 hover:bg-gray-300 rounded-full flex items-center justify-center">
|
|
|
|
<DotsVerticalIcon
|
|
|
|
className="h-5 w-5 text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button onClick={() => setExpanded(true)}>
|
|
|
|
<div className="min-w-0 flex-1 py-1.5">
|
|
|
|
<div className="text-sm text-gray-500 hover:text-gray-700">
|
|
|
|
<span>Show all activity</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
// Missing feed items:
|
|
|
|
// - Bet sold?
|
|
|
|
type ActivityItem = {
|
|
|
|
id: string
|
2022-01-22 17:46:59 +00:00
|
|
|
type:
|
|
|
|
| 'bet'
|
|
|
|
| 'comment'
|
|
|
|
| 'start'
|
|
|
|
| 'betgroup'
|
|
|
|
| 'close'
|
|
|
|
| 'resolve'
|
|
|
|
| 'expand'
|
2022-01-04 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 16:56:26 +00:00
|
|
|
export function ContractFeed(props: {
|
|
|
|
contract: Contract
|
2022-01-15 00:16:25 +00:00
|
|
|
bets: Bet[]
|
|
|
|
comments: Comment[]
|
2022-01-11 16:56:26 +00:00
|
|
|
// Feed types: 'activity' = Activity feed, 'market' = Comments feed on a market
|
|
|
|
feedType: 'activity' | 'market'
|
|
|
|
}) {
|
|
|
|
const { contract, feedType } = props
|
2022-01-04 07:21:14 +00:00
|
|
|
const { id } = contract
|
2022-01-22 17:46:59 +00:00
|
|
|
const [expanded, setExpanded] = useState(false)
|
2022-01-04 07:21:14 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
let bets = useBets(id) ?? props.bets
|
2022-01-13 17:01:20 +00:00
|
|
|
bets = withoutAnteBets(contract, bets)
|
2022-01-04 07:21:14 +00:00
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
const comments = useComments(id) ?? props.comments
|
2022-01-04 07:21:14 +00:00
|
|
|
|
2022-01-11 16:56:26 +00:00
|
|
|
const groupWindow = feedType == 'activity' ? 10 * DAY_IN_MS : DAY_IN_MS
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
const allItems = [
|
|
|
|
{ type: 'start', id: 0 },
|
2022-01-11 16:56:26 +00:00
|
|
|
...groupBets(bets, comments, groupWindow, user?.id),
|
2022-01-04 07:21:14 +00:00
|
|
|
]
|
2022-01-04 07:41:52 +00:00
|
|
|
if (contract.closeTime && contract.closeTime <= Date.now()) {
|
2022-01-04 07:21:14 +00:00
|
|
|
allItems.push({ type: 'close', id: `${contract.closeTime}` })
|
|
|
|
}
|
|
|
|
if (contract.resolution) {
|
|
|
|
allItems.push({ type: 'resolve', id: `${contract.resolutionTime}` })
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:46:59 +00:00
|
|
|
// If there are more than 5 items, only show the first, an expand item, and last 3
|
|
|
|
let items = allItems
|
|
|
|
if (!expanded && allItems.length > 5 && feedType == 'activity') {
|
|
|
|
items = [
|
|
|
|
allItems[0],
|
|
|
|
{ type: 'expand', id: 'expand' },
|
|
|
|
...allItems.slice(-3),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
return (
|
|
|
|
<div className="flow-root">
|
|
|
|
<ul role="list" className="-mb-8">
|
2022-01-22 17:46:59 +00:00
|
|
|
{items.map((activityItem, activityItemIdx) => (
|
2022-01-04 07:21:14 +00:00
|
|
|
<li key={activityItem.id}>
|
|
|
|
<div className="relative pb-8">
|
2022-01-22 17:46:59 +00:00
|
|
|
{activityItemIdx !== items.length - 1 ? (
|
2022-01-04 07:21:14 +00:00
|
|
|
<span
|
2022-01-19 08:15:09 +00:00
|
|
|
className="absolute top-5 left-5 -ml-px h-[calc(100%-2rem)] w-0.5 bg-gray-200"
|
2022-01-04 07:21:14 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
<div className="relative flex items-start space-x-3">
|
|
|
|
{activityItem.type === 'start' ? (
|
2022-01-11 16:56:26 +00:00
|
|
|
feedType == 'activity' ? (
|
|
|
|
<FeedQuestion contract={contract} />
|
|
|
|
) : (
|
|
|
|
<FeedDescription contract={contract} />
|
|
|
|
)
|
2022-01-04 07:21:14 +00:00
|
|
|
) : activityItem.type === 'comment' ? (
|
2022-01-21 18:53:51 +00:00
|
|
|
<FeedComment
|
|
|
|
activityItem={activityItem}
|
|
|
|
moreHref={contractPath(contract)}
|
|
|
|
feedType={feedType}
|
|
|
|
/>
|
2022-01-04 07:21:14 +00:00
|
|
|
) : activityItem.type === 'bet' ? (
|
|
|
|
<FeedBet activityItem={activityItem} />
|
|
|
|
) : activityItem.type === 'betgroup' ? (
|
|
|
|
<FeedBetGroup activityItem={activityItem} />
|
|
|
|
) : activityItem.type === 'close' ? (
|
|
|
|
<FeedClose contract={contract} />
|
|
|
|
) : activityItem.type === 'resolve' ? (
|
|
|
|
<FeedResolve contract={contract} />
|
2022-01-22 17:46:59 +00:00
|
|
|
) : activityItem.type === 'expand' ? (
|
|
|
|
<FeedExpand setExpanded={setExpanded} />
|
2022-01-04 07:21:14 +00:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|