import clsx from 'clsx'
import React, { useEffect, useState } from 'react'
import { ENV_CONFIG } from '../../common/envs/constants'
import { User } from '../../common/user'
import { formatMoney } from '../../common/util/format'
import { useUser } from '../hooks/use-user'
import { buyLeaderboardSlot } from '../lib/firebase/api-call'
import { Transaction, writeTransaction } from '../lib/firebase/transactions'
import { AmountInput } from './amount-input'
import { Avatar } from './avatar'
import { Col } from './layout/col'
import { Modal } from './layout/modal'
import { Row } from './layout/row'
import { SiteLink } from './site-link'
import { Title } from './title'
export function Manaboard(props: {
title: string
users: User[]
values: number[]
className?: string
}) {
// TODO: Ideally, highlight your own entry on the leaderboard
let { title, users, className, values } = props
const [expanded, setExpanded] = useState(false)
if (!expanded) {
users = users.slice(0, 25)
values = values.slice(0, 25)
}
return (
{users.length === 0 ? (
None yet
) : (
#
|
Name |
Value |
|
{users.map((user, index) => (
{index + 1}
|
{user.name}
|
{formatMoney(values[index])}
|
|
))}
)}
)
}
function Label(props: { children: React.ReactNode }) {
return
}
export function BuySlotModal(props: {
title: string
holder: User
slot: number
value: number
}) {
const { slot, title, holder, value } = props
const user = useUser()
const [open, setOpen] = useState(false)
const [newValue, setNewValue] = useState(value)
const [message, setMessage] = useState('')
useEffect(() => {
if (user?.name) {
setMessage(user.name)
}
}, [user])
const onBuy = async () => {
// Feel free to change this. - James
const slotId = `${title}-${slot}`
await buyLeaderboardSlot({ slotId, reassessValue: newValue })
}
return (
<>
{user && (
{slot}
{message}
)}
{
setMessage(e.target.value)
}}
value={message}
/>
setNewValue(amount ?? 0)}
error=""
label={ENV_CONFIG.moneyMoniker}
/>
Additional fees: {formatMoney(newValue / 10)} per hour
>
)
}
async function buySlot(options: {
holder: User
buyer: User
amount: number
slot: number
message: string
newValue: number
}) {
const { holder, buyer, amount, slot, message, newValue } = options
const createdTime = Date.now()
const buyTransaction: Transaction = {
id: '',
createdTime,
fromId: buyer.id,
fromName: buyer.name,
fromUsername: buyer.username,
fromAvatarUrl: buyer.avatarUrl,
toId: holder.id,
toName: holder.name,
toUsername: holder.username,
toAvatarUrl: holder.avatarUrl,
amount: amount,
category: 'BUY_LEADERBOARD_SLOT',
description: `${buyer.name} bought a slot from ${holder.name}`,
data: {
slot,
message,
newValue,
},
}
const feeTransaction: Transaction = {
id: '',
createdTime,
fromId: holder.id,
fromName: holder.name,
fromUsername: holder.username,
fromAvatarUrl: holder.avatarUrl,
// Send fee to Manifold Markets official account
toId: 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2',
toName: 'Manifold Markets',
toUsername: 'ManifoldMarkets',
toAvatarUrl: 'https://manifold.markets/logo-bg-white.png',
amount: 10, // TODO: Calculate fee
category: 'LEADERBOARD_TAX',
description: `${holder.name} paid M$ 10 in fees`,
data: {
slot,
},
}
await Promise.all([
writeTransaction(buyTransaction),
writeTransaction(feeTransaction),
])
}