// From https://tailwindui.com/components/application-ui/lists/feeds
import { Fragment, useState } from 'react'
import * as _ from 'lodash'
import {
BanIcon,
CheckIcon,
DotsVerticalIcon,
LockClosedIcon,
UserIcon,
UsersIcon,
XIcon,
} from '@heroicons/react/solid'
import dayjs from 'dayjs'
import clsx from 'clsx'
import Textarea from 'react-expanding-textarea'
import { OutcomeLabel } from '../outcome-label'
import {
contractMetrics,
Contract,
contractPath,
updateContract,
tradingAllowed,
} from '../../lib/firebase/contracts'
import { useUser } from '../../hooks/use-user'
import { Linkify } from '../linkify'
import { Row } from '../layout/row'
import {
canAddComment,
createComment,
MAX_COMMENT_LENGTH,
} from '../../lib/firebase/comments'
import { formatMoney } from '../../../common/util/format'
import { Comment } from '../../../common/comment'
import { ResolutionOrChance } from '../contract-card'
import { SiteLink } from '../site-link'
import { Col } from '../layout/col'
import { UserLink } from '../user-page'
import { DateTimeTooltip } from '../datetime-tooltip'
import { Bet } from '../../lib/firebase/bets'
import { JoinSpans } from '../join-spans'
import { fromNow } from '../../lib/util/time'
import BetRow from '../bet-row'
import { parseTags } from '../../../common/util/parse'
import { Avatar } from '../avatar'
import { useAdmin } from '../../hooks/use-admin'
import { Answer } from '../../../common/answer'
import { ActivityItem } from './activity-items'
import { FreeResponse, FullContract } from '../../../common/contract'
export function FeedItems(props: {
contract: Contract
items: ActivityItem[]
betRowClassName?: string
}) {
const { contract, items, betRowClassName } = props
const { outcomeType } = contract
return (
{items.map((item, activityItemIdx) => (
{activityItemIdx !== items.length - 1 ||
item.type === 'answergroup' ? (
) : null}
))}
{outcomeType === 'BINARY' && tradingAllowed(contract) && (
)}
)
}
function FeedItem(props: { item: ActivityItem }) {
const { item } = props
switch (item.type) {
case 'question':
return
case 'description':
return
case 'comment':
return
case 'bet':
return
case 'createanswer':
return
case 'betgroup':
return
case 'answergroup':
return
case 'close':
return
case 'resolve':
return
}
}
function FeedComment(props: {
contract: Contract
comment: Comment
bet: Bet
hideOutcome: boolean
truncate: boolean
smallAvatar: boolean
}) {
const { contract, comment, bet, hideOutcome, truncate, smallAvatar } = props
const { amount, outcome } = bet
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
const bought = amount >= 0 ? 'bought' : 'sold'
const money = formatMoney(Math.abs(amount))
return (
<>
{' '}
{bought} {money}
{!hideOutcome && (
<>
{' '}
of
>
)}
>
)
}
function RelativeTimestamp(props: { time: number }) {
const { time } = props
return (
{fromNow(time)}
)
}
function FeedBet(props: {
contract: Contract
bet: Bet
hideOutcome: boolean
smallAvatar: boolean
}) {
const { contract, bet, hideOutcome, smallAvatar } = props
const { id, amount, outcome, createdTime, userId } = bet
const user = useUser()
const isSelf = user?.id === userId
const isCreator = contract.creatorId === userId
// You can comment if your bet was posted in the last hour
const canComment = canAddComment(createdTime, isSelf)
const [comment, setComment] = useState('')
async function submitComment() {
if (!user || !comment) return
await createComment(contract.id, id, comment, user)
}
const bought = amount >= 0 ? 'bought' : 'sold'
const money = formatMoney(Math.abs(amount))
return (
<>
{isSelf ? (
) : isCreator ? (
) : (
)}
{isSelf ? 'You' : isCreator ? contract.creatorName : 'A trader'}
{' '}
{bought} {money}
{!hideOutcome && (
<>
{' '}
of
>
)}
{canComment && (
// Allow user to comment in an textarea if they are the creator
)}
>
)
}
function EditContract(props: {
text: string
onSave: (newText: string) => void
buttonText: string
}) {
const [text, setText] = useState(props.text)
const [editing, setEditing] = useState(false)
const onSave = (newText: string) => {
setEditing(false)
setText(props.text) // Reset to original text
props.onSave(newText)
}
return editing ? (
) : (
setEditing(true)}
>
{props.buttonText}
)
}
export function ContractDescription(props: {
contract: Contract
isCreator: boolean
}) {
const { contract, isCreator } = props
const descriptionTimestamp = () => `${dayjs().format('MMM D, h:mma')}: `
const isAdmin = useAdmin()
// Append the new description (after a newline)
async function saveDescription(newText: string) {
const newDescription = `${contract.description}\n\n${newText}`.trim()
const tags = parseTags(
`${newDescription} ${contract.tags.map((tag) => `#${tag}`).join(' ')}`
)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
await updateContract(contract.id, {
description: newDescription,
tags,
lowercaseTags,
})
}
if (!isCreator && !contract.description.trim()) return null
return (
{isCreator && (
)}
{isAdmin && (
updateContract(contract.id, { question })}
buttonText="ADMIN: Edit question"
/>
)}
{/* {isAdmin && (
updateContract(contract.id, { createdTime: Number(time) })
}
buttonText="ADMIN: Edit createdTime"
/>
)} */}
)
}
function TruncatedComment(props: {
comment: string
moreHref: string
shouldTruncate?: boolean
}) {
const { comment, moreHref, shouldTruncate } = props
let truncated = comment
// Keep descriptions to at most 400 characters
const MAX_CHARS = 400
if (shouldTruncate && truncated.length > MAX_CHARS) {
truncated = truncated.slice(0, MAX_CHARS)
// Make sure to end on a space
const i = truncated.lastIndexOf(' ')
truncated = truncated.slice(0, i)
}
return (
{truncated != comment && (
... (show more)
)}
)
}
export function FeedQuestion(props: {
contract: Contract
showDescription?: boolean
}) {
const { contract, showDescription } = props
const { creatorName, creatorUsername, question, resolution, outcomeType } =
contract
const { liquidityLabel } = contractMetrics(contract)
const isBinary = outcomeType === 'BINARY'
const closeMessage =
contract.isResolved || !contract.closeTime ? null : (
<>
•
{contract.closeTime > Date.now() ? 'Closes' : 'Closed'}
>
)
return (
<>
{' '}
asked
{/* Currently hidden on mobile; ideally we'd fit this in somewhere. */}
{liquidityLabel}
{closeMessage}
{question}
{!showDescription && (
See more...
)}
{(isBinary || resolution) && (
)}
{showDescription && (
)}
>
)
}
function FeedDescription(props: { contract: Contract }) {
const { contract } = props
const { creatorName, creatorUsername } = contract
const user = useUser()
const isCreator = user?.id === contract.creatorId
return (
<>
{' '}
created this market
>
)
}
function FeedCreateAnswer(props: {
contract: FullContract
answer: Answer
}) {
const { answer } = props
return (
<>
{' '}
submitted this answer
>
)
}
function OutcomeIcon(props: { outcome?: string }) {
const { outcome } = props
switch (outcome) {
case 'YES':
return
case 'NO':
return
case 'CANCEL':
return
default:
return
}
}
function FeedResolve(props: { contract: Contract }) {
const { contract } = props
const { creatorName, creatorUsername } = contract
const resolution = contract.resolution || 'CANCEL'
return (
<>
{' '}
resolved this market to {' '}
>
)
}
function FeedClose(props: { contract: Contract }) {
const { contract } = props
return (
<>
Trading closed in this market{' '}
>
)
}
function BetGroupSpan(props: { bets: Bet[]; outcome?: string }) {
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 (
{numberTraders} {numberTraders > 1 ? 'traders' : 'trader'}{' '}
{buyTotal > 0 && <>bought {formatMoney(buyTotal)} >}
{sellTotal > 0 && <>sold {formatMoney(sellTotal)} >}
{outcome && (
<>
{' '}
of
>
)}{' '}
)
}
function FeedBetGroup(props: {
contract: Contract
bets: Bet[]
hideOutcome: boolean
}) {
const { bets, hideOutcome } = props
const betGroups = _.groupBy(bets, (bet) => bet.outcome)
const outcomes = Object.keys(betGroups)
// Use the time of the last bet for the entire group
const createdTime = bets[bets.length - 1].createdTime
return (
<>
{outcomes.map((outcome, index) => (
{index !== outcomes.length - 1 && }
))}
>
)
}
function FeedAnswerGroup(props: {
contract: FullContract
answer: Answer
items: ActivityItem[]
}) {
const { answer, items } = props
const { username, avatarUrl, name, text } = answer
return (
answered
{items.map((item, index) => (
{index !== items.length - 1 ? (
) : null}
))}
)
}
// TODO: Should highlight the entire Feed segment
function FeedExpand(props: { setExpanded: (expanded: boolean) => void }) {
const { setExpanded } = props
return (
<>
setExpanded(true)}>
setExpanded(true)}>
>
)
}