// From https://tailwindui.com/components/application-ui/lists/feeds
import { useState } from 'react'
import {
ChatAltIcon,
StarIcon,
UserIcon,
UsersIcon,
} from '@heroicons/react/solid'
import { useBets } from '../hooks/use-bets'
import { Bet, createComment } from '../lib/firebase/bets'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import { OutcomeLabel } from './outcome-label'
import { Contract, setContract } from '../lib/firebase/contracts'
import { useUser } from '../hooks/use-user'
import { Linkify } from './linkify'
import { Row } from './layout/row'
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
const [comment, setComment] = useState('')
async function submitComment() {
if (!user || !comment) return
await createComment(contractId, id, comment, user)
}
return (
<>
{isCreator ? 'You' : 'Someone'}{' '}
placed M$ {amount} on
{' '}
{isCreator && (
// 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)
contract.description = `${contract.description}\n${description}`.trim()
await setContract(contract)
setDescription(editStatement())
}
return (
{isCreator &&
!contract.resolution &&
(editing ? (
) : (
))}
)
}
function FeedStart(props: { contract: Contract }) {
const { contract } = props
const user = useUser()
const isCreator = user?.id === contract.creatorId
return (
<>
{contract.creatorName} created
this market
>
)
}
function toFeedBet(bet: Bet) {
return {
id: bet.id,
contractId: bet.contractId,
userId: bet.userId,
type: 'bet',
amount: bet.amount,
outcome: bet.outcome,
createdTime: bet.createdTime,
date: dayjs(bet.createdTime).fromNow(),
}
}
function toComment(bet: Bet) {
return {
id: bet.id,
contractId: bet.contractId,
userId: bet.userId,
type: 'comment',
amount: bet.amount,
outcome: bet.outcome,
createdTime: bet.createdTime,
date: dayjs(bet.createdTime).fromNow(),
// Invariant: bet.comment exists
text: bet.comment!.text,
person: {
href: `/${bet.comment!.userUsername}`,
name: bet.comment!.userName,
avatarUrl: bet.comment!.userAvatarUrl,
},
}
}
function toActivityItem(bet: Bet) {
return bet.comment ? toComment(bet) : toFeedBet(bet)
}
// Group together bets that are:
// - Within 24h of the first in the group
// - Do not have a comment
// - Were not created by this user
// Return a list of ActivityItems
function group(bets: Bet[], userId?: string) {
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] })
}
group = []
}
for (const bet of bets) {
const isCreator = userId === bet.userId
if (bet.comment || isCreator) {
pushGroup()
// Create a single item for this
items.push(toActivityItem(bet))
} else {
if (
group.length > 0 &&
dayjs(bet.createdTime).diff(dayjs(group[0].createdTime), 'hour') > 24
) {
// More than 24h 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 ? (
M$ {yesAmount} on
) : null
const noAmount = bets
.filter((b) => b.outcome == 'NO')
.reduce((acc, bet) => acc + bet.amount, 0)
const noSpan = noAmount ? (
M$ {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?
// - Market closed
// - Market resolved
type ActivityItem = {
id: string
type: 'bet' | 'comment' | 'start' | 'betgroup'
}
export function ContractFeed(props: { contract: Contract }) {
const { contract } = props
const { id } = contract
const user = useUser()
let bets = useBets(id)
if (bets === 'loading') bets = []
const allItems = [{ type: 'start', id: 0 }, ...group(bets, user?.id)]
return (
{allItems.map((activityItem, activityItemIdx) => (
-
{activityItemIdx !== allItems.length - 1 ? (
) : null}
{activityItem.type === 'start' ? (
) : activityItem.type === 'comment' ? (
) : activityItem.type === 'bet' ? (
) : activityItem.type === 'betgroup' ? (
) : null}
))}
)
}