manifold/web/components/contracts-list.tsx

172 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-12-14 10:27:22 +00:00
import Link from 'next/link'
import { Col } from '../components/layout/col'
import { Row } from '../components/layout/row'
import { useEffect, useState } from 'react'
import {
compute,
Contract,
listContracts,
path,
} from '../lib/firebase/contracts'
import { formatMoney } from '../lib/util/format'
import { User } from '../lib/firebase/users'
2021-12-16 02:11:29 +00:00
import { UserLink } from './user-page'
2021-12-14 10:27:22 +00:00
export function ContractDetails(props: { contract: Contract }) {
const { contract } = props
const { volume, createdDate, resolvedDate } = compute(contract)
2021-12-14 10:27:22 +00:00
return (
<Row className="flex-wrap text-sm text-gray-500">
2021-12-16 02:04:36 +00:00
<div className="whitespace-nowrap">
<UserLink displayName={contract.creatorName} />
</div>
2021-12-14 10:27:22 +00:00
<div className="mx-2"></div>
<div className="whitespace-nowrap">
{resolvedDate ? `${createdDate} - ${resolvedDate}` : createdDate}
</div>
2021-12-14 10:27:22 +00:00
<div className="mx-2"></div>
<div className="whitespace-nowrap">{formatMoney(volume)} bet</div>
2021-12-14 10:27:22 +00:00
</Row>
)
}
function ContractCard(props: { contract: Contract }) {
const { contract } = props
const { probPercent } = compute(contract)
2021-12-14 17:43:51 +00:00
const resolutionColor = {
YES: 'text-primary',
NO: 'text-red-400',
CANCEL: 'text-yellow-400',
'': '', // Empty if unresolved
}[contract.resolution || '']
const resolutionText = {
YES: 'YES',
NO: 'NO',
CANCEL: 'N/A',
'': '',
}[contract.resolution || '']
2021-12-14 10:27:22 +00:00
return (
<Link href={path(contract)}>
2021-12-14 10:27:22 +00:00
<a>
<li className="col-span-1 bg-white hover:bg-gray-100 shadow-xl rounded-lg divide-y divide-gray-200">
<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 mb-8">
{contract.question}
</p>
<ContractDetails contract={contract} />
</div>
{/* Right side of card */}
<Col>
<Col
className={'text-4xl mx-auto items-end ' + resolutionColor}
>
{resolutionText || (
2021-12-14 10:27:22 +00:00
<div className="text-primary">
{probPercent}
<div className="text-lg">chance</div>
</div>
)}
</Col>
</Col>
</div>
</div>
</div>
</li>
</a>
</Link>
)
}
2021-12-16 02:50:03 +00:00
function ContractsGrid(props: { contracts: Contract[] }) {
2021-12-14 10:27:22 +00:00
const { contracts } = props
return (
<ul role="list" className="grid grid-cols-1 gap-6 lg:grid-cols-2">
2021-12-14 10:27:22 +00:00
{contracts.map((contract) => (
<ContractCard contract={contract} key={contract.id} />
))}
{/* TODO: Show placeholder if empty */}
</ul>
)
}
2021-12-16 02:50:03 +00:00
type Sort = 'createdTime' | 'volume' | 'resolved' | 'all'
export function SearchableGrid(props: {
contracts: Contract[]
defaultSort?: Sort
}) {
const { contracts, defaultSort } = props
const [query, setQuery] = useState('')
const [sort, setSort] = useState(defaultSort || 'volume')
function check(corpus: String) {
return corpus.toLowerCase().includes(query.toLowerCase())
}
// TODO: Search through @username
let matches = contracts.filter(
(c) => check(c.question) || check(c.description) || check(c.creatorName)
)
if (sort === 'createdTime' || sort === 'resolved' || sort === 'all') {
matches.sort((a, b) => b.createdTime - a.createdTime)
} else if (sort === 'volume') {
matches.sort((a, b) => compute(b).volume - compute(a).volume)
}
if (sort !== 'all') {
// Filter for (or filter out) resolved contracts
matches = matches.filter((c) =>
sort === 'resolved' ? c.resolution : !c.resolution
)
}
return (
<div>
{/* Show a search input next to a sort dropdown */}
<div className="flex justify-between gap-2 my-8">
<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>
<option value="createdTime">Newest first</option>
<option value="resolved">Resolved</option>
<option value="all">All markets</option>
</select>
</div>
<ContractsGrid contracts={matches} />
</div>
)
}
export function ContractsList(props: { creator: User }) {
const { creator } = props
const [contracts, setContracts] = useState<Contract[]>([])
useEffect(() => {
if (creator?.id) {
2021-12-14 01:23:57 +00:00
// TODO: stream changes from firestore
listContracts(creator.id).then(setContracts)
}
}, [creator])
2021-12-16 02:50:03 +00:00
return <SearchableGrid contracts={contracts} defaultSort="all" />
}