2021-12-16 21:20:49 +00:00
|
|
|
import _ from 'lodash'
|
2021-12-14 10:27:22 +00:00
|
|
|
import Link from 'next/link'
|
2021-12-16 06:11:14 +00:00
|
|
|
import clsx from 'clsx'
|
2021-12-14 01:07:28 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-12-16 21:20:49 +00:00
|
|
|
|
|
|
|
import { Row } from '../components/layout/row'
|
2021-12-16 00:52:19 +00:00
|
|
|
import {
|
|
|
|
compute,
|
|
|
|
Contract,
|
|
|
|
listContracts,
|
|
|
|
path,
|
|
|
|
} from '../lib/firebase/contracts'
|
2021-12-14 18:51:30 +00:00
|
|
|
import { formatMoney } from '../lib/util/format'
|
2021-12-16 01:34:36 +00:00
|
|
|
import { User } from '../lib/firebase/users'
|
2021-12-16 02:11:29 +00:00
|
|
|
import { UserLink } from './user-page'
|
2021-12-19 09:17:25 +00:00
|
|
|
import { Linkify } from './linkify'
|
2021-12-30 20:03:32 +00:00
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { SiteLink } from './link'
|
2021-12-14 10:27:22 +00:00
|
|
|
|
|
|
|
export function ContractDetails(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
2021-12-24 21:06:01 +00:00
|
|
|
const { truePool, 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">
|
2021-12-18 07:27:29 +00:00
|
|
|
<UserLink username={contract.creatorUsername} />
|
2021-12-16 02:04:36 +00:00
|
|
|
</div>
|
2021-12-14 10:27:22 +00:00
|
|
|
<div className="mx-2">•</div>
|
2021-12-14 18:51:30 +00:00
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{resolvedDate ? `${createdDate} - ${resolvedDate}` : createdDate}
|
|
|
|
</div>
|
2021-12-14 10:27:22 +00:00
|
|
|
<div className="mx-2">•</div>
|
2021-12-24 21:06:01 +00:00
|
|
|
<div className="whitespace-nowrap">{formatMoney(truePool)} pool</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
|
|
|
|
2021-12-15 07:43:04 +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 (
|
2021-12-16 00:52:19 +00:00
|
|
|
<Link href={path(contract)}>
|
2021-12-14 10:27:22 +00:00
|
|
|
<a>
|
2021-12-30 18:52:05 +00:00
|
|
|
<li className="col-span-1 bg-white hover:bg-gray-100 shadow-md rounded-lg divide-y divide-gray-200">
|
2021-12-14 10:27:22 +00:00
|
|
|
<div className="card">
|
|
|
|
<div className="card-body p-6">
|
2021-12-18 23:40:39 +00:00
|
|
|
<Row className="justify-between gap-4 mb-2">
|
2021-12-16 06:11:14 +00:00
|
|
|
<p className="font-medium text-indigo-700">
|
2021-12-19 09:08:12 +00:00
|
|
|
<Linkify text={contract.question} />
|
2021-12-16 06:11:14 +00:00
|
|
|
</p>
|
|
|
|
<div className={clsx('text-4xl', resolutionColor)}>
|
|
|
|
{resolutionText || (
|
|
|
|
<div className="text-primary">
|
|
|
|
{probPercent}
|
|
|
|
<div className="text-lg">chance</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-12-14 10:27:22 +00:00
|
|
|
</div>
|
2021-12-16 06:11:14 +00:00
|
|
|
</Row>
|
|
|
|
<ContractDetails contract={contract} />
|
2021-12-14 10:27:22 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-16 02:50:03 +00:00
|
|
|
function ContractsGrid(props: { contracts: Contract[] }) {
|
2021-12-16 21:20:49 +00:00
|
|
|
const [resolvedContracts, activeContracts] = _.partition(
|
|
|
|
props.contracts,
|
|
|
|
(c) => c.isResolved
|
|
|
|
)
|
|
|
|
const contracts = [...activeContracts, ...resolvedContracts]
|
|
|
|
|
2021-12-18 10:28:01 +00:00
|
|
|
if (contracts.length === 0) {
|
|
|
|
return (
|
2021-12-18 23:40:39 +00:00
|
|
|
<p className="mx-4">
|
2021-12-18 10:28:01 +00:00
|
|
|
No markets found. Would you like to{' '}
|
|
|
|
<Link href="/create">
|
|
|
|
<a className="text-green-500 hover:underline hover:decoration-2">
|
|
|
|
create one
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
?
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 10:27:22 +00:00
|
|
|
return (
|
2021-12-16 00:52:19 +00:00
|
|
|
<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} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|
2021-12-10 17:54:16 +00:00
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
export function CreatorContractsGrid(props: { contracts: Contract[] }) {
|
|
|
|
const { contracts } = props
|
|
|
|
|
|
|
|
const byCreator = _.groupBy(contracts, (contract) => contract.creatorId)
|
|
|
|
const creatorIds = _.sortBy(Object.keys(byCreator), (creatorId) =>
|
|
|
|
_.sumBy(byCreator[creatorId], (contract) => -1 * compute(contract).truePool)
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className="gap-6">
|
|
|
|
{creatorIds.map((creatorId) => {
|
|
|
|
const { creatorUsername, creatorName } = byCreator[creatorId][0]
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className="gap-6">
|
|
|
|
<SiteLink href={`/${creatorUsername}`}>{creatorName}</SiteLink>
|
|
|
|
|
|
|
|
<ul role="list" className="grid grid-cols-1 gap-6 lg:grid-cols-2">
|
|
|
|
{byCreator[creatorId].slice(0, 6).map((contract) => (
|
|
|
|
<ContractCard contract={contract} key={contract.id} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
{byCreator[creatorId].length > 6 ? (
|
|
|
|
<Link href={`/${creatorUsername}`}>
|
|
|
|
<a
|
|
|
|
className={clsx(
|
|
|
|
'self-end hover:underline hover:decoration-indigo-400 hover:decoration-2'
|
|
|
|
)}
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
|
|
|
See all
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:35:29 +00:00
|
|
|
const MAX_CONTRACTS_DISPLAYED = 99
|
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
type Sort = 'creator' | 'createdTime' | 'pool' | 'resolved' | 'all'
|
2021-12-16 02:50:03 +00:00
|
|
|
export function SearchableGrid(props: {
|
|
|
|
contracts: Contract[]
|
|
|
|
defaultSort?: Sort
|
2021-12-30 20:03:32 +00:00
|
|
|
byOneCreator?: boolean
|
2021-12-16 02:50:03 +00:00
|
|
|
}) {
|
2021-12-30 20:03:32 +00:00
|
|
|
const { contracts, defaultSort, byOneCreator } = props
|
2021-12-16 02:50:03 +00:00
|
|
|
const [query, setQuery] = useState('')
|
2021-12-30 20:03:32 +00:00
|
|
|
const [sort, setSort] = useState(
|
|
|
|
defaultSort || (byOneCreator ? 'pool' : 'creator')
|
|
|
|
)
|
2021-12-16 02:50:03 +00:00
|
|
|
|
|
|
|
function check(corpus: String) {
|
|
|
|
return corpus.toLowerCase().includes(query.toLowerCase())
|
|
|
|
}
|
|
|
|
let matches = contracts.filter(
|
2021-12-18 07:29:42 +00:00
|
|
|
(c) =>
|
|
|
|
check(c.question) ||
|
|
|
|
check(c.description) ||
|
|
|
|
check(c.creatorName) ||
|
|
|
|
check(c.creatorUsername)
|
2021-12-16 02:50:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if (sort === 'createdTime' || sort === 'resolved' || sort === 'all') {
|
|
|
|
matches.sort((a, b) => b.createdTime - a.createdTime)
|
2021-12-30 20:03:32 +00:00
|
|
|
} else if (sort === 'pool' || sort === 'creator') {
|
2021-12-24 21:06:01 +00:00
|
|
|
matches.sort((a, b) => compute(b).truePool - compute(a).truePool)
|
2021-12-16 02:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sort !== 'all') {
|
|
|
|
// Filter for (or filter out) resolved contracts
|
|
|
|
matches = matches.filter((c) =>
|
|
|
|
sort === 'resolved' ? c.resolution : !c.resolution
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:35:29 +00:00
|
|
|
if (matches.length > MAX_CONTRACTS_DISPLAYED)
|
|
|
|
matches = _.slice(matches, 0, MAX_CONTRACTS_DISPLAYED)
|
|
|
|
|
2021-12-16 02:50:03 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{/* Show a search input next to a sort dropdown */}
|
2021-12-18 12:06:59 +00:00
|
|
|
<div className="flex justify-between gap-2 mt-2 mb-8">
|
2021-12-16 02:50:03 +00:00
|
|
|
<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)}
|
|
|
|
>
|
2021-12-30 20:03:32 +00:00
|
|
|
{byOneCreator ? (
|
|
|
|
<option value="all">All markets</option>
|
|
|
|
) : (
|
|
|
|
<option value="creator">By creator</option>
|
|
|
|
)}
|
2021-12-24 21:06:01 +00:00
|
|
|
<option value="pool">Most traded</option>
|
2021-12-16 02:50:03 +00:00
|
|
|
<option value="createdTime">Newest first</option>
|
|
|
|
<option value="resolved">Resolved</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
{!byOneCreator && (sort === 'creator' || sort === 'resolved') ? (
|
|
|
|
<CreatorContractsGrid contracts={matches} />
|
|
|
|
) : (
|
|
|
|
<ContractsGrid contracts={matches} />
|
|
|
|
)}
|
2021-12-16 02:50:03 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
export function CreatorContractsList(props: { creator: User }) {
|
2021-12-16 01:34:36 +00:00
|
|
|
const { creator } = props
|
2021-12-19 20:37:31 +00:00
|
|
|
const [contracts, setContracts] = useState<Contract[] | 'loading'>('loading')
|
2021-12-14 01:07:28 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (creator?.id) {
|
2021-12-14 01:23:57 +00:00
|
|
|
// TODO: stream changes from firestore
|
2021-12-14 01:07:28 +00:00
|
|
|
listContracts(creator.id).then(setContracts)
|
|
|
|
}
|
|
|
|
}, [creator])
|
|
|
|
|
2021-12-19 21:05:39 +00:00
|
|
|
if (contracts === 'loading') return <></>
|
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
return <SearchableGrid contracts={contracts} byOneCreator defaultSort="all" />
|
2021-12-10 17:54:16 +00:00
|
|
|
}
|