2021-12-14 01:09:16 +00:00
|
|
|
import dayjs from 'dayjs'
|
2021-12-14 07:59:04 +00:00
|
|
|
import Link from 'next/link'
|
2021-12-14 01:09:16 +00:00
|
|
|
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 (
|
2021-12-14 07:59:04 +00:00
|
|
|
<Link href={`/contract/${contract.id}`}>
|
|
|
|
<a>
|
2021-12-14 08:35:20 +00:00
|
|
|
<li className="col-span-1 bg-white hover:bg-gray-100 shadow-xl rounded-lg divide-y divide-gray-200">
|
2021-12-14 07:59:04 +00:00
|
|
|
<div className="card">
|
|
|
|
<div className="card-body p-6">
|
|
|
|
<div className="flex justify-between gap-2">
|
|
|
|
{/* Left side of card */}
|
|
|
|
<div>
|
|
|
|
<p className="font-medium text-indigo-700">
|
|
|
|
{contract.question}
|
|
|
|
</p>
|
|
|
|
<Spacer h={8} />
|
|
|
|
{/* Copied from contract-overview.tsx */}
|
|
|
|
<Row className="flex-wrap text-sm text-gray-500">
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
By {contract.creatorName}
|
|
|
|
</div>
|
|
|
|
<div className="mx-2">•</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{dayjs(createdTime).format('MMM D')}
|
|
|
|
</div>
|
|
|
|
<div className="mx-2">•</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatWithCommas(volume)} vol
|
|
|
|
</div>
|
|
|
|
</Row>
|
2021-12-14 01:09:16 +00:00
|
|
|
</div>
|
2021-12-14 07:59:04 +00:00
|
|
|
{/* Right side of card */}
|
|
|
|
<Col>
|
|
|
|
<Col className="text-4xl mx-auto items-end">
|
|
|
|
{contract.resolution || (
|
|
|
|
<div className="text-primary">
|
|
|
|
{probPercent}
|
|
|
|
<div className="text-lg">chance</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</Col>
|
2021-12-14 01:09:16 +00:00
|
|
|
</div>
|
2021-12-14 07:59:04 +00:00
|
|
|
</div>
|
2021-12-14 01:09:16 +00:00
|
|
|
</div>
|
2021-12-14 07:59:04 +00:00
|
|
|
</li>
|
|
|
|
</a>
|
|
|
|
</Link>
|
2021-12-14 01:09:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 08:35:20 +00:00
|
|
|
export function ContractsGrid(props: { contracts: Contract[] }) {
|
|
|
|
const { contracts } = props
|
|
|
|
return (
|
|
|
|
<ul
|
|
|
|
role="list"
|
|
|
|
className="grid grid-cols-1 gap-6 sm:grid-cols-1 lg:grid-cols-2"
|
|
|
|
>
|
|
|
|
{contracts.map((contract) => (
|
|
|
|
<ContractCard contract={contract} key={contract.id} />
|
|
|
|
))}
|
|
|
|
{/* TODO: Show placeholder if empty */}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 01:09:16 +00:00
|
|
|
export default function Markets() {
|
|
|
|
const [contracts, setContracts] = useState<Contract[]>([])
|
|
|
|
useEffect(() => {
|
|
|
|
listAllContracts().then(setContracts)
|
|
|
|
}, [])
|
|
|
|
|
2021-12-14 08:57:27 +00:00
|
|
|
const [query, setQuery] = useState('')
|
|
|
|
type Sort = 'createdTime' | 'volume'
|
2021-12-14 09:03:16 +00:00
|
|
|
const [sort, setSort] = useState('volume')
|
2021-12-14 08:57:27 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-12-14 01:09:16 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Header />
|
|
|
|
<div className="max-w-4xl py-8 mx-auto">
|
2021-12-14 08:57:27 +00:00
|
|
|
{/* Show a search input next to a sort dropdown */}
|
|
|
|
<div className="flex justify-between gap-2">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value={query}
|
|
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
|
|
placeholder="Search markets"
|
|
|
|
className="input input-bordered w-full"
|
|
|
|
/>
|
|
|
|
<select
|
|
|
|
className="select select-bordered"
|
|
|
|
value={sort}
|
|
|
|
onChange={(e) => setSort(e.target.value as Sort)}
|
|
|
|
>
|
|
|
|
<option value="volume">Most traded</option>
|
2021-12-14 09:03:16 +00:00
|
|
|
<option value="createdTime">Newest first</option>
|
2021-12-14 08:57:27 +00:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Title text="Open markets" className="mt-16" />
|
|
|
|
<ContractsGrid contracts={matches.filter((c) => !c.resolution)} />
|
2021-12-14 07:59:04 +00:00
|
|
|
|
2021-12-14 08:57:27 +00:00
|
|
|
<Title text="Resolved markets" className="mt-16" />
|
|
|
|
<ContractsGrid contracts={matches.filter((c) => c.resolution)} />
|
2021-12-14 01:09:16 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|