manifold/web/components/contracts-list.tsx

274 lines
7.8 KiB
TypeScript
Raw Normal View History

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'
import { useEffect, useState } from 'react'
2021-12-16 21:20:49 +00:00
import { compute, Contract, listContracts } from '../lib/firebase/contracts'
import { User } from '../lib/firebase/users'
2021-12-30 20:03:32 +00:00
import { Col } from './layout/col'
import { SiteLink } from './site-link'
2021-12-31 19:41:37 +00:00
import { parseTags } from '../lib/util/parse'
import { ContractCard } from './contract-card'
import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params'
2021-12-14 10:27:22 +00:00
export function ContractsGrid(props: {
contracts: Contract[]
showHotVolume?: boolean
}) {
const { showHotVolume } = props
2021-12-16 21:20:49 +00:00
const [resolvedContracts, activeContracts] = _.partition(
props.contracts,
(c) => c.isResolved
)
const contracts = [...activeContracts, ...resolvedContracts]
if (contracts.length === 0) {
return (
2021-12-18 23:40:39 +00:00
<p className="mx-4">
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 (
<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}
showHotVolume={showHotVolume}
/>
2021-12-14 10:27:22 +00:00
))}
</ul>
)
}
const MAX_GROUPED_CONTRACTS_DISPLAYED = 6
function CreatorContractsGrid(props: { contracts: Contract[] }) {
2021-12-30 20:03:32 +00:00
const { contracts } = props
const byCreator = _.groupBy(contracts, (contract) => contract.creatorId)
const creator7DayVol = _.mapValues(byCreator, (contracts) =>
_.sumBy(contracts, (contract) => contract.volume7Days)
)
const creatorIds = _.sortBy(
Object.keys(byCreator).filter((creatorId) => creator7DayVol[creatorId] > 0),
(creatorId) => -1 * creator7DayVol[creatorId]
2021-12-30 20:03:32 +00:00
)
return (
<Col className="gap-6">
{creatorIds.map((creatorId) => {
const { creatorUsername, creatorName } = byCreator[creatorId][0]
return (
<Col className="gap-4" key={creatorUsername}>
<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>
{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>
)
}
function TagContractsGrid(props: { contracts: Contract[] }) {
2021-12-31 19:41:37 +00:00
const { contracts } = props
const contractTags = _.flatMap(contracts, (contract) =>
parseTags(contract.question + ' ' + contract.description).map((tag) => ({
tag,
contract,
}))
)
const groupedByTag = _.groupBy(contractTags, ({ tag }) => tag)
const byTag = _.mapValues(groupedByTag, (contractTags) =>
contractTags.map(({ contract }) => contract)
)
const tag7DayVol = _.mapValues(byTag, (contracts) =>
_.sumBy(contracts, (contract) => contract.volume7Days)
)
const tags = _.sortBy(
Object.keys(byTag).filter((tag) => tag7DayVol[tag] > 0),
(creatorId) => -1 * tag7DayVol[creatorId]
2021-12-31 19:41:37 +00:00
)
return (
<Col className="gap-6">
{tags.map((tag) => {
return (
<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>
)
}
const MAX_CONTRACTS_DISPLAYED = 99
2021-12-16 02:50:03 +00:00
export function SearchableGrid(props: {
contracts: Contract[]
query: string
setQuery: (query: string) => void
sort: Sort
setSort: (sort: Sort) => void
2021-12-30 20:03:32 +00:00
byOneCreator?: boolean
2021-12-16 02:50:03 +00:00
}) {
const { contracts, query, setQuery, sort, setSort, byOneCreator } = props
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 === 'newest' || sort === 'resolved' || sort === 'all') {
2021-12-16 02:50:03 +00:00
matches.sort((a, b) => b.createdTime - a.createdTime)
} else if (sort === 'most-traded') {
matches.sort((a, b) => compute(b).truePool - compute(a).truePool)
} else if (sort === 'creator' || sort === 'tag') {
matches.sort((a, b) => b.volume7Days - a.volume7Days)
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
)
}
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-31 19:41:37 +00:00
<option value="tag">By tag</option>
<option value="most-traded">Most traded</option>
<option value="newest">Newest</option>
2021-12-16 02:50:03 +00:00
<option value="resolved">Resolved</option>
</select>
</div>
2021-12-31 19:41:37 +00:00
{sort === 'tag' ? (
<TagContractsGrid contracts={matches} />
) : !byOneCreator && (sort === 'creator' || sort === 'resolved') ? (
2021-12-30 20:03:32 +00:00
<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 }) {
const { creator } = props
const [contracts, setContracts] = useState<Contract[] | 'loading'>('loading')
const { query, setQuery, sort, setSort } = useQueryAndSortParams({
defaultSort: 'all',
})
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-19 21:05:39 +00:00
if (contracts === 'loading') return <></>
return (
<SearchableGrid
contracts={contracts}
byOneCreator
query={query}
setQuery={setQuery}
sort={sort}
setSort={setSort}
/>
)
}