// From https://tailwindui.com/components/application-ui/lists/feeds
import { Fragment, useState } from 'react'
import { ChatAltIcon, TagIcon, UserCircleIcon } 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 { Contract } from '../lib/firebase/contracts'
import { OutcomeLabel } from './outcome-label'
import { useUser } from '../hooks/use-user'
import { User } from '../lib/firebase/users'
import { Linkify } from './linkify'
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; user: User | null }) {
const { activityItem, user } = props
const { id, contractId, amount, outcome, createdTime } = activityItem
const isCreator = user?.id == activityItem.userId
const [comment, setComment] = useState('')
async function submitComment() {
if (!user) 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
)}
>
)
}
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)
}
export function ContractFeed(props: { contract: Contract }) {
const { contract } = props
const { id } = contract
const user = useUser()
let bets = useBets(id)
if (bets === 'loading') bets = []
// TODO: aggregate bets across each day window
const allItems = bets.map(toActivityItem)
return (
{allItems.map((activityItem, activityItemIdx) => (
-
{activityItemIdx !== allItems.length - 1 ? (
) : null}
{activityItem.type === 'comment' ? (
) : activityItem.type === 'bet' ? (
) : null}
))}
)
}