diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index f99d9806..b5b5ea4e 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -11,6 +11,7 @@ import { onSnapshot, orderBy, getDoc, + limit, } from 'firebase/firestore' export type Contract = { @@ -47,9 +48,7 @@ export async function getContract(contractId: string) { const docRef = doc(db, 'contracts', contractId) const result = await getDoc(docRef) - return result.exists() - ? result.data() as Contract - : undefined + return result.exists() ? (result.data() as Contract) : undefined } export async function deleteContract(contractId: string) { @@ -64,9 +63,13 @@ export async function listContracts(creatorId: string): Promise { orderBy('createdTime', 'desc') ) const snapshot = await getDocs(q) - const contracts: Contract[] = [] - snapshot.forEach((doc) => contracts.push(doc.data() as Contract)) - return contracts + return snapshot.docs.map((doc) => doc.data() as Contract) +} + +export async function listAllContracts(): Promise { + const q = query(contractCollection, orderBy('createdTime', 'desc'), limit(25)) + const snapshot = await getDocs(q) + return snapshot.docs.map((doc) => doc.data() as Contract) } export function listenForContract( diff --git a/web/pages/markets.tsx b/web/pages/markets.tsx new file mode 100644 index 00000000..6544a1c3 --- /dev/null +++ b/web/pages/markets.tsx @@ -0,0 +1,89 @@ +import dayjs from 'dayjs' +import React, { useEffect, useState } from 'react' +import { Header } from '../components/header' +import { Col } from '../components/layout/col' +import { Row } from '../components/layout/row' +import { Spacer } from '../components/layout/spacer' +import { Title } from '../components/title' +import { listAllContracts } from '../lib/firebase/contracts' +import { Contract } from '../lib/firebase/contracts' +import { formatWithCommas } from '../lib/util/format' + +function ContractCard(props: { contract: Contract }) { + const { contract } = props + + // Copied from contract-overview.tsx + const { pot, seedAmounts, createdTime } = contract + const volume = pot.YES + pot.NO - seedAmounts.YES - seedAmounts.NO + const prob = pot.YES ** 2 / (pot.YES ** 2 + pot.NO ** 2) + const probPercent = Math.round(prob * 100) + '%' + + return ( +
  • +
    +
    +
    + {/* Left side of card */} +
    +

    {contract.question}

    + + {/* Copied from contract-overview.tsx */} + +
    + By {contract.creatorName} +
    +
    +
    + {dayjs(createdTime).format('MMM D')} +
    +
    +
    + {formatWithCommas(volume)} vol +
    +
    +
    + {/* Right side of card */} + + + {probPercent} + + {/* Show a yes and a no button side-by-side */} +
    + + +
    + +
    +
    +
    +
  • + ) +} + +export default function Markets() { + const [contracts, setContracts] = useState([]) + useEffect(() => { + listAllContracts().then(setContracts) + }, []) + + return ( +
    +
    +
    + + <ul + role="list" + className="grid grid-cols-1 gap-6 sm:grid-cols-1 lg:grid-cols-2" + > + {contracts.map((contract) => ( + <ContractCard contract={contract} key={contract.id} /> + ))} + </ul> + </div> + </div> + ) +}