Simplify useContractWithPreload
This commit is contained in:
parent
dfa61be382
commit
a2344492a2
|
@ -1,9 +1,5 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
import {
|
||||
Contract,
|
||||
getContractFromSlug,
|
||||
listenForContract,
|
||||
} from '../lib/firebase/contracts'
|
||||
import { Contract, listenForContract } from '../lib/firebase/contracts'
|
||||
|
||||
export const useContract = (contractId: string) => {
|
||||
const [contract, setContract] = useState<Contract | null | 'loading'>(
|
||||
|
@ -17,21 +13,13 @@ export const useContract = (contractId: string) => {
|
|||
return contract
|
||||
}
|
||||
|
||||
export const useContractWithPreload = (
|
||||
slug: string,
|
||||
initial: Contract | null
|
||||
) => {
|
||||
export const useContractWithPreload = (initial: Contract | null) => {
|
||||
const [contract, setContract] = useState<Contract | null>(initial)
|
||||
const [contractId, setContractId] = useState<string | undefined | null>(
|
||||
initial?.id
|
||||
)
|
||||
const contractId = initial?.id
|
||||
|
||||
useEffect(() => {
|
||||
if (contractId) return listenForContract(contractId, setContract)
|
||||
|
||||
if (contractId !== null && slug)
|
||||
getContractFromSlug(slug).then((c) => setContractId(c?.id || null))
|
||||
}, [contractId, slug])
|
||||
}, [contractId])
|
||||
|
||||
return contract
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@ export default function ContractPage(props: {
|
|||
}
|
||||
const user = useUser()
|
||||
|
||||
const contract = useContractWithPreload(props.slug, props.contract)
|
||||
const contract = useContractWithPreload(props.contract)
|
||||
const { bets, comments } = props
|
||||
|
||||
// Sort for now to see if bug is fixed.
|
||||
|
|
|
@ -67,7 +67,7 @@ export default function ContractEmbedPage(props: {
|
|||
slug: '',
|
||||
}
|
||||
|
||||
const contract = useContractWithPreload(props.slug, props.contract)
|
||||
const contract = useContractWithPreload(props.contract)
|
||||
const { bets } = props
|
||||
|
||||
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
||||
|
@ -98,13 +98,13 @@ function ContractEmbed(props: { contract: Contract; bets: Bet[] }) {
|
|||
|
||||
return (
|
||||
<Col className="w-full flex-1 bg-white">
|
||||
<div className="flex flex-col relative pt-2" ref={setElem}>
|
||||
<div className="relative flex flex-col pt-2" ref={setElem}>
|
||||
<SiteLink
|
||||
className="absolute top-0 left-0 w-full h-full z-20"
|
||||
className="absolute top-0 left-0 z-20 h-full w-full"
|
||||
href={href}
|
||||
/>
|
||||
|
||||
<div className="px-3 text-xl md:text-2xl text-indigo-700">
|
||||
<div className="px-3 text-xl text-indigo-700 md:text-2xl">
|
||||
<Linkify text={question} />
|
||||
</div>
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user