import Link from 'next/link'
import React, { useEffect, useState } from 'react'
import { Header } from '../components/header'
import { Col } from '../components/layout/col'
import { Row } from '../components/layout/row'
import { Title } from '../components/title'
import { compute, listAllContracts } from '../lib/firebase/contracts'
import { Contract } from '../lib/firebase/contracts'
import { formatWithCommas } from '../lib/util/format'
export function ContractDetails(props: { contract: Contract }) {
const { contract } = props
const { volume, createdDate } = compute(contract)
return (
By {contract.creatorName}
•
{createdDate}
•
{formatWithCommas(volume)} vol
)
}
function ContractCard(props: { contract: Contract }) {
const { contract } = props
const { probPercent } = compute(contract)
return (
{/* Left side of card */}
{/* Right side of card */}
{contract.resolution || (
)}
)
}
export function ContractsGrid(props: { contracts: Contract[] }) {
const { contracts } = props
return (
{contracts.map((contract) => (
))}
{/* TODO: Show placeholder if empty */}
)
}
export default function Markets() {
const [contracts, setContracts] = useState([])
useEffect(() => {
listAllContracts().then(setContracts)
}, [])
const [query, setQuery] = useState('')
type Sort = 'createdTime' | 'volume'
const [sort, setSort] = useState('volume')
function check(corpus: String) {
return corpus.toLowerCase().includes(query.toLowerCase())
}
const matches = contracts.filter(
(c) => check(c.question) || check(c.description) || check(c.creatorName)
)
function volume(contract: Contract) {
return (
contract.pot.YES +
contract.pot.NO -
contract.seedAmounts.YES -
contract.seedAmounts.NO
)
}
if (sort === 'createdTime') {
matches.sort((a, b) => b.createdTime - a.createdTime)
} else if (sort === 'volume') {
matches.sort((a, b) => volume(b) - volume(a))
}
return (
)
}