manifold/web/components/contract/contracts-grid.tsx

117 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Contract } from 'web/lib/firebase/contracts'
import { User } from 'web/lib/firebase/users'
import { Col } from '../layout/col'
import { SiteLink } from '../site-link'
import { ContractCard } from './contract-card'
import { ShowTime } from './contract-details'
import { ContractSearch } from '../contract-search'
import { useCallback } from 'react'
import clsx from 'clsx'
import { LoadingIndicator } from '../loading-indicator'
import { VisibilityObserver } from '../visibility-observer'
2021-12-14 10:27:22 +00:00
export type ContractHighlightOptions = {
contractIds?: string[]
highlightClassName?: string
}
export function ContractsGrid(props: {
contracts: Contract[] | undefined
loadMore?: () => void
showTime?: ShowTime
onContractClick?: (contract: Contract) => void
overrideGridClassName?: string
cardHideOptions?: {
hideQuickBet?: boolean
hideGroupLink?: boolean
}
highlightOptions?: ContractHighlightOptions
}) {
const {
contracts,
showTime,
loadMore,
onContractClick,
overrideGridClassName,
cardHideOptions,
highlightOptions,
} = props
const { hideQuickBet, hideGroupLink } = cardHideOptions || {}
const { contractIds, highlightClassName } = highlightOptions || {}
const onVisibilityUpdated = useCallback(
(visible) => {
if (visible && loadMore) {
loadMore()
}
},
[loadMore]
)
if (contracts === undefined) {
return <LoadingIndicator />
}
if (contracts.length === 0) {
return (
<p className="mx-2 text-gray-500">
2022-02-18 02:32:15 +00:00
No markets found. Why not{' '}
<SiteLink href="/create" className="font-bold text-gray-700">
2022-02-18 02:32:15 +00:00
create one?
</SiteLink>
</p>
)
}
2021-12-14 10:27:22 +00:00
return (
<Col className="gap-8">
<ul
className={clsx(
overrideGridClassName
? overrideGridClassName
: 'grid w-full grid-cols-1 gap-4 md:grid-cols-2'
)}
>
{contracts.map((contract) => (
<ContractCard
contract={contract}
key={contract.id}
showTime={showTime}
onClick={
onContractClick ? () => onContractClick(contract) : undefined
}
hideQuickBet={hideQuickBet}
hideGroupLink={hideGroupLink}
className={
contractIds?.includes(contract.id)
? highlightClassName
: undefined
}
/>
))}
</ul>
<VisibilityObserver
onVisibilityUpdated={onVisibilityUpdated}
className="relative -top-96 h-1"
/>
2021-12-30 20:03:32 +00:00
</Col>
)
}
export function CreatorContractsList(props: {
user: User | null | undefined
creator: User
}) {
const { user, creator } = props
2021-12-16 02:50:03 +00:00
return (
<ContractSearch
user={user}
defaultSort="newest"
defaultFilter="all"
additionalFilter={{
creatorId: creator.id,
}}
/>
)
}