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
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
import {
|
|
|
|
contractMetrics,
|
|
|
|
Contract,
|
|
|
|
listContracts,
|
2022-03-19 02:22:12 +00:00
|
|
|
getBinaryProb,
|
2022-04-07 21:15:51 +00:00
|
|
|
} from '../../lib/firebase/contracts'
|
|
|
|
import { User } from '../../lib/firebase/users'
|
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { SiteLink } from '../site-link'
|
2022-01-02 19:09:01 +00:00
|
|
|
import { ContractCard } from './contract-card'
|
2022-04-07 21:15:51 +00:00
|
|
|
import {
|
|
|
|
Sort,
|
|
|
|
useQueryAndSortParams,
|
|
|
|
} from '../../hooks/use-sort-and-query-params'
|
|
|
|
import { Answer } from '../../../common/answer'
|
2022-04-29 14:05:32 +00:00
|
|
|
import { LoadingIndicator } from '../loading-indicator'
|
2021-12-14 10:27:22 +00:00
|
|
|
|
2022-01-09 21:21:30 +00:00
|
|
|
export function ContractsGrid(props: {
|
|
|
|
contracts: Contract[]
|
|
|
|
showHotVolume?: boolean
|
2022-01-17 22:54:00 +00:00
|
|
|
showCloseTime?: boolean
|
2022-01-09 21:21:30 +00:00
|
|
|
}) {
|
2022-02-18 02:32:15 +00:00
|
|
|
const { showCloseTime } = props
|
2022-04-28 22:45:26 +00:00
|
|
|
const PAGE_SIZE = 100
|
|
|
|
const [page, setPage] = useState(1)
|
2022-01-09 21:21:30 +00:00
|
|
|
|
2021-12-16 21:20:49 +00:00
|
|
|
const [resolvedContracts, activeContracts] = _.partition(
|
|
|
|
props.contracts,
|
|
|
|
(c) => c.isResolved
|
|
|
|
)
|
2022-04-28 22:45:26 +00:00
|
|
|
const allContracts = [...activeContracts, ...resolvedContracts]
|
|
|
|
const showMore = allContracts.length > PAGE_SIZE * page
|
|
|
|
const contracts = allContracts.slice(0, PAGE_SIZE * page)
|
2021-12-16 21:20:49 +00:00
|
|
|
|
2021-12-18 10:28:01 +00:00
|
|
|
if (contracts.length === 0) {
|
|
|
|
return (
|
2022-02-11 18:40:22 +00:00
|
|
|
<p className="mx-2 text-gray-500">
|
2022-02-18 02:32:15 +00:00
|
|
|
No markets found. Why not{' '}
|
|
|
|
<SiteLink href="/home" className="font-bold text-gray-700">
|
|
|
|
create one?
|
|
|
|
</SiteLink>
|
2021-12-18 10:28:01 +00:00
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 10:27:22 +00:00
|
|
|
return (
|
2022-04-28 22:45:26 +00:00
|
|
|
<>
|
|
|
|
<ul className="grid w-full grid-cols-1 gap-6 md:grid-cols-2">
|
|
|
|
{contracts.map((contract) => (
|
|
|
|
<ContractCard
|
|
|
|
contract={contract}
|
|
|
|
key={contract.id}
|
|
|
|
// showHotVolume={showHotVolume}
|
|
|
|
showCloseTime={showCloseTime}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
{/* Show a link that increases the page num when clicked */}
|
|
|
|
{showMore && (
|
|
|
|
<button
|
|
|
|
className="btn btn-link float-right normal-case"
|
|
|
|
onClick={() => setPage(page + 1)}
|
|
|
|
>
|
|
|
|
Show more...
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</>
|
2021-12-14 10:27:22 +00:00
|
|
|
)
|
|
|
|
}
|
2021-12-10 17:54:16 +00:00
|
|
|
|
2021-12-30 22:31:04 +00:00
|
|
|
const MAX_GROUPED_CONTRACTS_DISPLAYED = 6
|
|
|
|
|
2022-01-02 19:09:01 +00:00
|
|
|
function CreatorContractsGrid(props: { contracts: Contract[] }) {
|
2021-12-30 20:03:32 +00:00
|
|
|
const { contracts } = props
|
|
|
|
|
|
|
|
const byCreator = _.groupBy(contracts, (contract) => contract.creatorId)
|
2022-01-09 21:55:10 +00:00
|
|
|
const creator7DayVol = _.mapValues(byCreator, (contracts) =>
|
|
|
|
_.sumBy(contracts, (contract) => contract.volume7Days)
|
|
|
|
)
|
|
|
|
const creatorIds = _.sortBy(
|
2022-01-15 23:56:02 +00:00
|
|
|
Object.keys(byCreator),
|
2022-01-09 21:55:10 +00:00
|
|
|
(creatorId) => -1 * creator7DayVol[creatorId]
|
2021-12-30 20:03:32 +00:00
|
|
|
)
|
|
|
|
|
2022-01-15 23:56:02 +00:00
|
|
|
let numContracts = 0
|
|
|
|
let maxIndex = 0
|
|
|
|
for (; maxIndex < creatorIds.length; maxIndex++) {
|
|
|
|
numContracts += Math.min(
|
|
|
|
MAX_GROUPED_CONTRACTS_DISPLAYED,
|
|
|
|
byCreator[creatorIds[maxIndex]].length
|
|
|
|
)
|
|
|
|
if (numContracts > MAX_CONTRACTS_DISPLAYED) break
|
|
|
|
}
|
|
|
|
|
|
|
|
const creatorIdsSubset = creatorIds.slice(0, maxIndex)
|
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
return (
|
|
|
|
<Col className="gap-6">
|
2022-01-15 23:56:02 +00:00
|
|
|
{creatorIdsSubset.map((creatorId) => {
|
2021-12-30 20:03:32 +00:00
|
|
|
const { creatorUsername, creatorName } = byCreator[creatorId][0]
|
|
|
|
|
|
|
|
return (
|
2022-01-02 22:46:04 +00:00
|
|
|
<Col className="gap-4" key={creatorUsername}>
|
2021-12-30 22:31:04 +00:00
|
|
|
<SiteLink className="text-lg" href={`/${creatorUsername}`}>
|
|
|
|
{creatorName}
|
|
|
|
</SiteLink>
|
|
|
|
|
|
|
|
<ul role="list" className="grid grid-cols-1 gap-4 lg:grid-cols-2">
|
|
|
|
{byCreator[creatorId]
|
|
|
|
.slice(0, MAX_GROUPED_CONTRACTS_DISPLAYED)
|
|
|
|
.map((contract) => (
|
|
|
|
<ContractCard contract={contract} key={contract.id} />
|
|
|
|
))}
|
2021-12-30 20:03:32 +00:00
|
|
|
</ul>
|
|
|
|
|
2021-12-30 22:31:04 +00:00
|
|
|
{byCreator[creatorId].length > MAX_GROUPED_CONTRACTS_DISPLAYED ? (
|
2021-12-30 20:03:32 +00:00
|
|
|
<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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:09:01 +00:00
|
|
|
function TagContractsGrid(props: { contracts: Contract[] }) {
|
2021-12-31 19:41:37 +00:00
|
|
|
const { contracts } = props
|
|
|
|
|
2022-01-30 21:30:49 +00:00
|
|
|
const contractTags = _.flatMap(contracts, (contract) => {
|
|
|
|
const { tags } = contract
|
|
|
|
return tags.map((tag) => ({
|
2021-12-31 19:41:37 +00:00
|
|
|
tag,
|
|
|
|
contract,
|
|
|
|
}))
|
2022-01-30 21:30:49 +00:00
|
|
|
})
|
2021-12-31 19:41:37 +00:00
|
|
|
const groupedByTag = _.groupBy(contractTags, ({ tag }) => tag)
|
|
|
|
const byTag = _.mapValues(groupedByTag, (contractTags) =>
|
|
|
|
contractTags.map(({ contract }) => contract)
|
|
|
|
)
|
2022-01-09 21:55:10 +00:00
|
|
|
const tag7DayVol = _.mapValues(byTag, (contracts) =>
|
|
|
|
_.sumBy(contracts, (contract) => contract.volume7Days)
|
|
|
|
)
|
|
|
|
const tags = _.sortBy(
|
2022-01-15 23:56:02 +00:00
|
|
|
Object.keys(byTag),
|
2022-01-09 21:55:10 +00:00
|
|
|
(creatorId) => -1 * tag7DayVol[creatorId]
|
2021-12-31 19:41:37 +00:00
|
|
|
)
|
|
|
|
|
2022-01-15 23:56:02 +00:00
|
|
|
let numContracts = 0
|
|
|
|
let maxIndex = 0
|
|
|
|
for (; maxIndex < tags.length; maxIndex++) {
|
|
|
|
numContracts += Math.min(
|
|
|
|
MAX_GROUPED_CONTRACTS_DISPLAYED,
|
|
|
|
byTag[tags[maxIndex]].length
|
|
|
|
)
|
|
|
|
if (numContracts > MAX_CONTRACTS_DISPLAYED) break
|
|
|
|
}
|
|
|
|
|
|
|
|
const tagsSubset = tags.slice(0, maxIndex)
|
|
|
|
|
2021-12-31 19:41:37 +00:00
|
|
|
return (
|
|
|
|
<Col className="gap-6">
|
2022-01-15 23:56:02 +00:00
|
|
|
{tagsSubset.map((tag) => {
|
2021-12-31 19:41:37 +00:00
|
|
|
return (
|
2022-01-02 22:46:04 +00:00
|
|
|
<Col className="gap-4" key={tag}>
|
2021-12-31 19:41:37 +00:00
|
|
|
<SiteLink className="text-lg" href={`/tag/${tag}`}>
|
|
|
|
#{tag}
|
|
|
|
</SiteLink>
|
|
|
|
|
|
|
|
<ul role="list" className="grid grid-cols-1 gap-4 lg:grid-cols-2">
|
|
|
|
{byTag[tag]
|
|
|
|
.slice(0, MAX_GROUPED_CONTRACTS_DISPLAYED)
|
|
|
|
.map((contract) => (
|
|
|
|
<ContractCard contract={contract} key={contract.id} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
{byTag[tag].length > MAX_GROUPED_CONTRACTS_DISPLAYED ? (
|
|
|
|
<Link href={`/tag/${tag}`}>
|
|
|
|
<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-16 02:50:03 +00:00
|
|
|
export function SearchableGrid(props: {
|
2022-04-29 14:05:32 +00:00
|
|
|
contracts: Contract[] | undefined
|
2021-12-30 20:03:32 +00:00
|
|
|
byOneCreator?: boolean
|
2022-03-29 15:09:46 +00:00
|
|
|
querySortOptions?: {
|
|
|
|
defaultSort: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}
|
2021-12-16 02:50:03 +00:00
|
|
|
}) {
|
2022-03-29 15:09:46 +00:00
|
|
|
const { contracts, byOneCreator, querySortOptions } = props
|
|
|
|
|
|
|
|
const { query, setQuery, sort, setSort } =
|
|
|
|
useQueryAndSortParams(querySortOptions)
|
2021-12-16 02:50:03 +00:00
|
|
|
|
2022-03-01 01:52:41 +00:00
|
|
|
const queryWords = query.toLowerCase().split(' ')
|
2021-12-16 02:50:03 +00:00
|
|
|
function check(corpus: String) {
|
2022-03-01 01:52:41 +00:00
|
|
|
return queryWords.every((word) => corpus.toLowerCase().includes(word))
|
2021-12-16 02:50:03 +00:00
|
|
|
}
|
2022-03-01 01:52:41 +00:00
|
|
|
|
2022-04-29 14:05:32 +00:00
|
|
|
let matches = (contracts ?? []).filter(
|
2021-12-18 07:29:42 +00:00
|
|
|
(c) =>
|
|
|
|
check(c.question) ||
|
|
|
|
check(c.description) ||
|
|
|
|
check(c.creatorName) ||
|
2022-01-30 21:30:49 +00:00
|
|
|
check(c.creatorUsername) ||
|
2022-02-24 23:35:30 +00:00
|
|
|
check(c.lowercaseTags.map((tag) => `#${tag}`).join(' ')) ||
|
2022-03-15 22:27:51 +00:00
|
|
|
check(
|
|
|
|
((c as any).answers ?? [])
|
|
|
|
.map((answer: Answer) => answer.text)
|
|
|
|
.join(' ')
|
|
|
|
)
|
2021-12-16 02:50:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-23 17:14:04 +00:00
|
|
|
if (sort === 'newest' || sort === 'all') {
|
2021-12-16 02:50:03 +00:00
|
|
|
matches.sort((a, b) => b.createdTime - a.createdTime)
|
2022-01-23 17:14:04 +00:00
|
|
|
} else if (sort === 'resolved') {
|
|
|
|
matches = _.sortBy(
|
|
|
|
matches,
|
|
|
|
(contract) => -1 * (contract.resolutionTime ?? 0)
|
|
|
|
)
|
2022-02-04 22:04:30 +00:00
|
|
|
} else if (sort === 'oldest') {
|
|
|
|
matches.sort((a, b) => a.createdTime - b.createdTime)
|
2022-03-02 02:00:14 +00:00
|
|
|
} else if (sort === 'close-date' || sort === 'closed') {
|
2022-01-24 06:31:52 +00:00
|
|
|
matches = _.sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours)
|
2022-03-19 02:15:27 +00:00
|
|
|
matches = _.sortBy(
|
|
|
|
matches,
|
2022-03-19 16:22:24 +00:00
|
|
|
(contract) =>
|
|
|
|
(sort === 'closed' ? -1 : 1) * (contract.closeTime ?? Infinity)
|
2022-03-19 02:15:27 +00:00
|
|
|
)
|
2022-03-02 02:00:14 +00:00
|
|
|
const hideClosed = sort === 'closed'
|
|
|
|
matches = matches.filter(
|
|
|
|
({ closeTime }) => closeTime && closeTime > Date.now() !== hideClosed
|
|
|
|
)
|
2022-01-09 21:55:10 +00:00
|
|
|
} else if (sort === 'most-traded') {
|
2022-03-23 05:27:22 +00:00
|
|
|
matches.sort((a, b) => b.volume - a.volume)
|
2022-01-24 06:31:52 +00:00
|
|
|
} else if (sort === '24-hour-vol') {
|
|
|
|
// Use lodash for stable sort, so previous sort breaks all ties.
|
|
|
|
matches = _.sortBy(matches, ({ volume7Days }) => -1 * volume7Days)
|
|
|
|
matches = _.sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours)
|
2022-01-09 21:55:10 +00:00
|
|
|
} else if (sort === 'creator' || sort === 'tag') {
|
|
|
|
matches.sort((a, b) => b.volume7Days - a.volume7Days)
|
2022-03-19 02:22:12 +00:00
|
|
|
} else if (sort === 'most-likely') {
|
|
|
|
matches = _.sortBy(matches, (contract) => -getBinaryProb(contract))
|
|
|
|
} else if (sort === 'least-likely') {
|
|
|
|
// Exclude non-binary contracts
|
|
|
|
matches = matches.filter((contract) => getBinaryProb(contract) !== 0)
|
|
|
|
matches = _.sortBy(matches, (contract) => getBinaryProb(contract))
|
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
|
|
|
|
)
|
2022-03-19 02:07:06 +00:00
|
|
|
|
|
|
|
// Filter out closed contracts.
|
|
|
|
if (sort !== 'closed' && sort !== 'resolved') {
|
|
|
|
matches = matches.filter((c) => !c.closeTime || c.closeTime > Date.now())
|
|
|
|
}
|
2021-12-16 02:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{/* Show a search input next to a sort dropdown */}
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mt-2 mb-8 flex justify-between gap-2">
|
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)}
|
|
|
|
>
|
2022-01-02 22:46:04 +00:00
|
|
|
<option value="most-traded">Most traded</option>
|
2022-02-03 09:48:44 +00:00
|
|
|
<option value="24-hour-vol">24h volume</option>
|
2022-01-24 06:31:52 +00:00
|
|
|
<option value="close-date">Closing soon</option>
|
2022-03-02 02:00:14 +00:00
|
|
|
<option value="closed">Closed</option>
|
2022-01-02 22:46:04 +00:00
|
|
|
<option value="newest">Newest</option>
|
2022-02-04 22:04:30 +00:00
|
|
|
<option value="oldest">Oldest</option>
|
2022-03-19 02:22:12 +00:00
|
|
|
<option value="most-likely">Most likely</option>
|
|
|
|
<option value="least-likely">Least likely</option>
|
2022-01-24 06:31:52 +00:00
|
|
|
|
|
|
|
<option value="tag">By tag</option>
|
|
|
|
{!byOneCreator && <option value="creator">By creator</option>}
|
2021-12-16 02:50:03 +00:00
|
|
|
<option value="resolved">Resolved</option>
|
2022-01-24 06:31:52 +00:00
|
|
|
{byOneCreator && <option value="all">All markets</option>}
|
2021-12-16 02:50:03 +00:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
2022-04-29 14:05:32 +00:00
|
|
|
{contracts === undefined ? (
|
|
|
|
<LoadingIndicator />
|
|
|
|
) : sort === 'tag' ? (
|
2021-12-31 19:41:37 +00:00
|
|
|
<TagContractsGrid contracts={matches} />
|
2022-01-23 17:14:04 +00:00
|
|
|
) : !byOneCreator && sort === 'creator' ? (
|
2021-12-30 20:03:32 +00:00
|
|
|
<CreatorContractsGrid contracts={matches} />
|
|
|
|
) : (
|
2022-01-24 07:38:29 +00:00
|
|
|
<ContractsGrid
|
|
|
|
contracts={matches}
|
2022-03-02 02:00:14 +00:00
|
|
|
showCloseTime={['close-date', 'closed'].includes(sort)}
|
2022-01-24 07:38:29 +00:00
|
|
|
/>
|
2021-12-30 20:03:32 +00:00
|
|
|
)}
|
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 <></>
|
|
|
|
|
2022-01-02 22:46:04 +00:00
|
|
|
return (
|
|
|
|
<SearchableGrid
|
|
|
|
contracts={contracts}
|
|
|
|
byOneCreator
|
2022-03-29 15:09:46 +00:00
|
|
|
querySortOptions={{
|
|
|
|
defaultSort: 'all',
|
|
|
|
shouldLoadFromStorage: false,
|
|
|
|
}}
|
2022-01-02 22:46:04 +00:00
|
|
|
/>
|
|
|
|
)
|
2021-12-10 17:54:16 +00:00
|
|
|
}
|