// From https://tailwindui.com/components/application-ui/lists/feeds
import { useState } from 'react'
import {
BanIcon,
ChatAltIcon,
CheckIcon,
LockClosedIcon,
StarIcon,
UserIcon,
UsersIcon,
XIcon,
} from '@heroicons/react/solid'
import { useBets } from '../hooks/use-bets'
import { Bet } from '../lib/firebase/bets'
import { Comment, mapCommentsByBetId } from '../lib/firebase/comments'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import { OutcomeLabel } from './outcome-label'
import {
compute,
Contract,
path,
updateContract,
} from '../lib/firebase/contracts'
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'
import { formatMoney } from '../lib/util/format'
import { ResolutionOrChance } from './contract-card'
import { SiteLink } from './site-link'
import { Col } from './layout/col'
dayjs.extend(relativeTime)
function FeedComment(props: { activityItem: any }) {
const { activityItem } = props
const { person, text, amount, outcome, createdTime } = activityItem
return (
<>
>
)
}
function Timestamp(props: { time: number }) {
const { time } = props
return (
{dayjs(time).fromNow()}
)
}
function FeedBet(props: { activityItem: any }) {
const { activityItem } = props
const { id, contractId, amount, outcome, createdTime } = activityItem
const user = useUser()
const isCreator = user?.id == activityItem.userId
// The creator can comment if the bet was posted in the last hour
const canComment = isCreator && Date.now() - createdTime < 60 * 60 * 1000
const [comment, setComment] = useState('')
async function submitComment() {
if (!user || !comment) return
await createComment(contractId, id, comment, user)
}
return (
<>
{isCreator ? 'You' : 'A trader'} placed{' '}
{formatMoney(amount)} on
{' '}
{canComment && (
// Allow user to comment in an textarea if they are the creator
)}
>
)
}
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)
const newDescription = `${contract.description}\n\n${description}`.trim()
await updateContract(contract.id, { description: newDescription })
setDescription(editStatement())
}
if (!isCreator && !contract.description.trim()) return null
return (
{isCreator &&
!contract.resolution &&
(editing ? (
) : (
setEditing(true)}
>
Add to description
))}
)
}
function FeedQuestion(props: { contract: Contract }) {
const { contract } = props
const { probPercent } = compute(contract)
return (
<>
{contract.creatorName} asked{' '}
{contract.question}
>
)
}
function FeedDescription(props: { contract: Contract }) {
const { contract } = props
const user = useUser()
const isCreator = user?.id === contract.creatorId
return (
<>
{contract.creatorName} created
this market
>
)
}
function OutcomeIcon(props: { outcome?: 'YES' | 'NO' | 'CANCEL' }) {
const { outcome } = props
switch (outcome) {
case 'YES':
return
case 'NO':
return
case 'CANCEL':
default:
return
}
}
function FeedResolve(props: { contract: Contract }) {
const { contract } = props
const resolution = contract.resolution || 'CANCEL'
return (
<>
{contract.creatorName} resolved
this market to {' '}
>
)
}
function FeedClose(props: { contract: Contract }) {
const { contract } = props
return (
<>
Trading closed in this market{' '}
>
)
}
function toFeedBet(bet: Bet) {
return {
id: bet.id,
contractId: bet.contractId,
userId: bet.userId,
type: 'bet',
amount: bet.sale ? -bet.sale.amount : bet.amount,
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',
amount: bet.sale ? -bet.sale.amount : bet.amount,
outcome: bet.outcome,
createdTime: bet.createdTime,
date: dayjs(bet.createdTime).fromNow(),
// Invariant: bet.comment exists
text: comment.text,
person: {
href: `/${comment.userUsername}`,
name: comment.userName,
avatarUrl: comment.userAvatarUrl,
},
}
}
const DAY_IN_MS = 24 * 60 * 60 * 1000
// Group together bets that are:
// - Within `windowMs` of the first in the group
// - Do not have a comment
// - Were not created by this user
// Return a list of ActivityItems
function groupBets(
bets: Bet[],
comments: Comment[],
windowMs: number,
userId?: string
) {
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 &&
bet.createdTime - group[0].createdTime > windowMs
) {
// More than `windowMs` has passed; start a new group
pushGroup()
}
group.push(bet)
}
}
if (group.length > 0) {
pushGroup()
}
return items as ActivityItem[]
}
// TODO: Make this expandable to show all grouped bets?
function FeedBetGroup(props: { activityItem: any }) {
const { activityItem } = props
const bets: Bet[] = activityItem.bets
const yesAmount = bets
.filter((b) => b.outcome == 'YES')
.reduce((acc, bet) => acc + bet.amount, 0)
const yesSpan = yesAmount ? (
{formatMoney(yesAmount)} on
) : null
const noAmount = bets
.filter((b) => b.outcome == 'NO')
.reduce((acc, bet) => acc + bet.amount, 0)
const noSpan = noAmount ? (
{formatMoney(noAmount)} on
) : null
const traderCount = bets.length
const createdTime = bets[0].createdTime
return (
<>
{traderCount} traders placed {yesSpan}
{yesAmount && noAmount ? ' and ' : ''}
{noSpan}
>
)
}
// Missing feed items:
// - Bet sold?
type ActivityItem = {
id: string
type: 'bet' | 'comment' | 'start' | 'betgroup' | 'close' | 'resolve'
}
export function ContractFeed(props: {
contract: Contract
// Feed types: 'activity' = Activity feed, 'market' = Comments feed on a market
feedType: 'activity' | 'market'
}) {
const { contract, feedType } = props
const { id } = contract
const user = useUser()
let bets = useBets(id)
if (bets === 'loading') bets = []
let comments = useComments(id)
if (comments === 'loading') comments = []
const groupWindow = feedType == 'activity' ? 10 * DAY_IN_MS : DAY_IN_MS
const allItems = [
{ type: 'start', id: 0 },
...groupBets(bets, comments, groupWindow, user?.id),
]
if (contract.closeTime && contract.closeTime <= Date.now()) {
allItems.push({ type: 'close', id: `${contract.closeTime}` })
}
if (contract.resolution) {
allItems.push({ type: 'resolve', id: `${contract.resolutionTime}` })
}
return (
{allItems.map((activityItem, activityItemIdx) => (
{activityItemIdx !== allItems.length - 1 ? (
) : null}
{activityItem.type === 'start' ? (
feedType == 'activity' ? (
) : (
)
) : activityItem.type === 'comment' ? (
) : activityItem.type === 'bet' ? (
) : activityItem.type === 'betgroup' ? (
) : activityItem.type === 'close' ? (
) : activityItem.type === 'resolve' ? (
) : null}
))}
)
}