import _ from 'lodash' import Link from 'next/link' import clsx from 'clsx' import { useEffect, useState } from 'react' import { Row } from '../components/layout/row' import { compute, Contract, listContracts, path, } from '../lib/firebase/contracts' import { formatMoney } from '../lib/util/format' import { User } from '../lib/firebase/users' import { UserLink } from './user-page' import { Linkify } from './linkify' import { Col } from './layout/col' import { SiteLink } from './link' export function ContractDetails(props: { contract: Contract }) { const { contract } = props const { truePool, createdDate, resolvedDate } = compute(contract) return (
{resolvedDate ? `${createdDate} - ${resolvedDate}` : createdDate}
{formatMoney(truePool)} pool
) } function ContractCard(props: { contract: Contract }) { const { contract } = props const { probPercent } = compute(contract) 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 || ''] return (
  • {resolutionText || (
    {probPercent}
    chance
    )}
  • ) } function ContractsGrid(props: { contracts: Contract[] }) { const [resolvedContracts, activeContracts] = _.partition( props.contracts, (c) => c.isResolved ) const contracts = [...activeContracts, ...resolvedContracts] if (contracts.length === 0) { return (

    No markets found. Would you like to{' '} create one ?

    ) } return ( ) } 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 ( {creatorIds.map((creatorId) => { const { creatorUsername, creatorName } = byCreator[creatorId][0] return ( {creatorName} {byCreator[creatorId].length > 6 ? ( e.stopPropagation()} > See all ) : (
    )} ) })} ) } const MAX_CONTRACTS_DISPLAYED = 99 type Sort = 'creator' | 'createdTime' | 'pool' | 'resolved' | 'all' export function SearchableGrid(props: { contracts: Contract[] defaultSort?: Sort byOneCreator?: boolean }) { const { contracts, defaultSort, byOneCreator } = props const [query, setQuery] = useState('') const [sort, setSort] = useState( defaultSort || (byOneCreator ? 'pool' : 'creator') ) function check(corpus: String) { return corpus.toLowerCase().includes(query.toLowerCase()) } let matches = contracts.filter( (c) => check(c.question) || check(c.description) || check(c.creatorName) || check(c.creatorUsername) ) if (sort === 'createdTime' || sort === 'resolved' || sort === 'all') { matches.sort((a, b) => b.createdTime - a.createdTime) } else if (sort === 'pool' || sort === 'creator') { matches.sort((a, b) => compute(b).truePool - compute(a).truePool) } 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) return (
    {/* Show a search input next to a sort dropdown */}
    setQuery(e.target.value)} placeholder="Search markets" className="input input-bordered w-full" />
    {!byOneCreator && (sort === 'creator' || sort === 'resolved') ? ( ) : ( )}
    ) } export function CreatorContractsList(props: { creator: User }) { const { creator } = props const [contracts, setContracts] = useState('loading') useEffect(() => { if (creator?.id) { // TODO: stream changes from firestore listContracts(creator.id).then(setContracts) } }, [creator]) if (contracts === 'loading') return <> return }