caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

372 lines
11 KiB
TypeScript

import clsx from 'clsx'
import { memo, ReactNode, useEffect, useMemo, useRef, useState } from 'react'
import { Bet } from 'common/bet'
import { ContractComment } from 'common/comment'
import { Contract, MarketContract } from 'common/contract'
import { CommentView } from 'common/events'
import { HOUR_MS } from 'common/util/time'
import { last, orderBy, sumBy } from 'lodash'
import Link from 'next/link'
import { FeedReplyBet } from 'web/components/feed/feed-bets'
import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row'
import { Avatar } from 'web/components/widgets/avatar'
import { useIsVisible } from 'web/hooks/use-is-visible'
import { useDisplayUserById } from 'web/hooks/use-user-supabase'
import { usePartialUpdater } from 'web/hooks/use-partial-updater'
import { track } from 'web/lib/service/analytics'
import { scrollIntoViewCentered } from 'web/lib/util/scroll'
import { UserHovercard } from '../user/user-hovercard'
import { Content } from '../widgets/editor'
import { CommentActions } from './comment-actions'
import { CommentReplyHeader, FeedCommentHeader } from './comment-header'
import { ReplyToggle } from './reply-toggle'
export type ReplyToUserInfo = { id: string; username: string }
export const roundThreadColor = 'border-ink-100 dark:border-ink-300'
const straightThreadColor = 'bg-ink-100 dark:bg-ink-300'
export const FeedComment = memo(function FeedComment(props: {
playContract: Contract
liveContract: Contract
comment: ContractComment
trackingLocation: string
highlighted?: boolean
onReplyClick?: (comment: ContractComment) => void
children?: ReactNode
inTimeline?: boolean
isParent?: boolean
bets?: Bet[]
lastInReplyChain?: boolean
isPinned?: boolean
showParentLine?: boolean
blockedUserIds?: string[]
}) {
const {
playContract,
liveContract,
highlighted,
onReplyClick,
children,
trackingLocation,
inTimeline,
isParent,
bets,
lastInReplyChain,
isPinned,
showParentLine,
blockedUserIds,
} = props
// for optimistic updates
const [comment, updateComment] = usePartialUpdater(props.comment)
useEffect(() => updateComment(props.comment), [props.comment])
const groupedBets = useMemo(() => {
// Sort the bets by createdTime
const sortedBets = orderBy(bets, 'createdTime', 'asc')
const tempGrouped: Bet[][] = []
sortedBets.forEach((currentBet) => {
// Check if the bet was made within the last 2 hours
const isRecentBet = Date.now() - currentBet.createdTime < 2 * HOUR_MS
if (isRecentBet) {
// If the bet was made within the last 2 hours, add it as an individual group
tempGrouped.push([currentBet])
return
}
let foundGroup = false
for (const group of tempGrouped) {
const lastBetInGroup = last(group)
if (!lastBetInGroup) break
const outcomesMatch = currentBet.outcome === lastBetInGroup.outcome
const sameSign =
Math.sign(currentBet.amount) === Math.sign(lastBetInGroup.amount)
if (outcomesMatch && sameSign) {
group.push(currentBet)
foundGroup = true
break
}
}
if (!foundGroup) tempGrouped.push([currentBet])
})
updateComment({
betReplyAmountsByOutcome: {
YES: sumBy(
bets?.filter((bet) => bet.outcome === 'YES'),
'amount'
),
NO: sumBy(
bets?.filter((bet) => bet.outcome === 'NO'),
'amount'
),
},
})
return tempGrouped
}, [bets?.length])
const { userUsername, userAvatarUrl, userId } = comment
const displayUser = useDisplayUserById(userId)
const ref = useRef<HTMLDivElement>(null)
const isBetParent = !!bets?.length
useEffect(() => {
if (highlighted && ref.current) {
scrollIntoViewCentered(ref.current)
}
}, [highlighted])
// Don't render deleted comments at all
if (comment.deleted) {
return null
}
return (
<Col className="group">
{!commenterAndBettorMatch(comment) && (
<CommentReplyHeader
comment={comment}
contract={playContract}
hideBetHeader={false}
/>
)}
<Row ref={ref} className={clsx(isParent ? 'gap-2' : 'gap-1')}>
<Row className="relative">
{/*// Curved reply line*/}
{!isParent && (
<div
className={clsx(
roundThreadColor,
'-mt-4 ml-4 h-6 w-4 rounded-bl-xl border-b-2 border-l-2'
)}
/>
)}
<UserHovercard userId={userId} className="z-10 self-start">
<Avatar
username={userUsername}
size={isParent ? 'sm' : '2xs'}
avatarUrl={userAvatarUrl}
entitlements={displayUser?.entitlements}
displayContext="market_comments"
/>
</UserHovercard>
{/* Outer vertical reply line*/}
{(showParentLine || !isParent) && (
<div
className={clsx(
straightThreadColor,
'absolute bottom-0 left-4 w-0.5',
isParent ? 'top-0' : '-top-1',
!isBetParent && 'group-last:hidden',
lastInReplyChain && 'hidden'
)}
/>
)}
{/* Inner vertical reply line*/}
{isBetParent && !isParent && (
<div
className={clsx(
straightThreadColor,
'absolute bottom-0 left-10 top-0 w-0.5'
)}
/>
)}
</Row>
<Col
className={clsx(
'grow rounded-lg rounded-tl-none px-3 pb-0.5 pt-1 transition-colors',
highlighted
? 'bg-primary-100 border-primary-300 border-2'
: isPinned
? 'bg-canvas-50 border-primary-300 border-2'
: 'bg-canvas-50'
)}
>
<FeedCommentHeader
comment={comment}
menuProps={{
liveContractId: liveContract.id,
updateComment: updateComment,
}}
playContract={playContract}
inTimeline={inTimeline}
isParent={isParent}
isPinned={isPinned}
/>
<HideableContent comment={comment} blockedUserIds={blockedUserIds} />
<Row>
{children}
<CommentActions
playContract={playContract}
onReplyClick={onReplyClick}
onAward={(total) => updateComment({ bountyAwarded: total })}
comment={comment}
liveContract={liveContract}
trackingLocation={trackingLocation}
/>
</Row>
</Col>
</Row>
{!!bets?.length && (
<Row>
<Col className={'w-full'}>
{groupedBets?.map((bets) => {
return (
<Row
className={'relative mt-1 w-full'}
key={bets.map((b) => b.id) + '-reply'}
>
{/* Curved bet reply line*/}
<div
className={clsx(
roundThreadColor,
'rounded-bl-xl border-b-2 border-l-2 ',
isParent ? '-mt-2 ml-4 h-4 w-4' : '-mt-7 ml-10 h-10 w-4'
)}
/>
{/* Outer vertical bet reply line */}
<div
className={clsx(
straightThreadColor,
'absolute bottom-0 left-4 w-0.5 group-last:hidden ',
isParent ? 'top-0' : '-top-1',
lastInReplyChain ? 'hidden' : ''
)}
/>
<FeedReplyBet
className={'bg-canvas-50'}
avatarSize={'2xs'}
contract={playContract as MarketContract}
bets={bets}
/>
</Row>
)
})}
</Col>
</Row>
)}
</Col>
)
})
export const ParentFeedComment = memo(function ParentFeedComment(props: {
playContract: Contract
liveContract: Contract
comment: ContractComment
highlighted?: boolean
seeReplies: boolean
numReplies: number
onReplyClick?: (comment: ContractComment) => void
onSeeReplyClick?: () => void
trackingLocation: string
inTimeline?: boolean
childrenBountyTotal?: number
bets?: Bet[]
isPinned?: boolean
blockedUserIds?: string[]
}) {
const {
playContract,
liveContract,
comment,
highlighted,
onReplyClick,
onSeeReplyClick,
seeReplies,
numReplies,
trackingLocation,
inTimeline,
childrenBountyTotal,
bets,
isPinned,
blockedUserIds,
} = props
const { ref } = useIsVisible(
() =>
track('view comment thread', {
contractId: playContract.id,
commentId: comment.id,
} as CommentView),
false
)
// Don't render deleted comments at all
if (comment.deleted) {
return null
}
return (
<FeedComment
playContract={playContract}
liveContract={liveContract}
comment={comment}
onReplyClick={onReplyClick}
highlighted={highlighted}
trackingLocation={trackingLocation}
inTimeline={inTimeline}
isParent={true}
bets={bets}
isPinned={isPinned}
showParentLine={seeReplies && numReplies > 0}
blockedUserIds={blockedUserIds}
>
{isPinned && (
<Link
className="self-center text-xs text-ink-400 hover:text-primary-400 hover:underline"
href={`#${comment.id}`}
>
View original context
</Link>
)}
<div ref={ref} />
<ReplyToggle
seeReplies={seeReplies}
numComments={numReplies}
childrenBountyTotal={childrenBountyTotal}
onSeeReplyClick={onSeeReplyClick}
/>
</FeedComment>
)
})
function HideableContent(props: {
comment: ContractComment
blockedUserIds?: string[]
}) {
const { comment, blockedUserIds } = props
const { text, content } = comment
//hides if enough dislikes
const dislikes = comment.dislikes ?? 0
const likes = comment.likes ?? 0
const majorityDislikes = dislikes > 10 && dislikes / (likes + dislikes) >= 0.8
const isBlocked = blockedUserIds?.includes(comment.userId)
const initiallyHidden = majorityDislikes || comment.hidden || isBlocked
const [showHidden, setShowHidden] = useState(false)
return initiallyHidden && !showHidden ? (
<div
className="hover text-ink-600 text-sm font-thin italic hover:cursor-pointer"
onClick={() => {
setShowHidden(!showHidden)
}}
>
{isBlocked ? 'Commentor blocked' : 'Comment hidden'}
</div>
) : (
<Content size="sm" className="mt-1 grow" content={content || text} />
)
}
export const commenterAndBettorMatch = (c: ContractComment) =>
c.bettorUsername === c.userUsername || c.bettorId === c.userId