import dayjs from 'dayjs' 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 { Spacer } from '../components/layout/spacer' import { Title } from '../components/title' import { listAllContracts } from '../lib/firebase/contracts' import { Contract } from '../lib/firebase/contracts' import { formatWithCommas } from '../lib/util/format' function ContractCard(props: { contract: Contract }) { const { contract } = props // Copied from contract-overview.tsx const { pot, seedAmounts, createdTime } = contract const volume = pot.YES + pot.NO - seedAmounts.YES - seedAmounts.NO const prob = pot.YES ** 2 / (pot.YES ** 2 + pot.NO ** 2) const probPercent = Math.round(prob * 100) + '%' return (
  • {/* Left side of card */}

    {contract.question}

    {/* Copied from contract-overview.tsx */}
    By {contract.creatorName}
    {dayjs(createdTime).format('MMM D')}
    {formatWithCommas(volume)} vol
    {/* Right side of card */} {contract.resolution || (
    {probPercent}
    chance
    )}
  • ) } export function ContractsGrid(props: { contracts: Contract[] }) { const { contracts } = props return ( ) } 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 (
    {/* Show a search input next to a sort dropdown */}
    setQuery(e.target.value)} placeholder="Search markets" className="input input-bordered w-full" />
    <ContractsGrid contracts={matches.filter((c) => !c.resolution)} /> <Title text="Resolved markets" className="mt-16" /> <ContractsGrid contracts={matches.filter((c) => c.resolution)} /> </div> </div> ) }