manifold/web/hooks/use-contract.ts
Austin Chen 20d18f3588 Client-side render pages that need auth
These pages are now client-side rendered:
- /home
- /leaderboards
- /market/...
- /fold/...
2022-03-01 16:32:31 -08:00

38 lines
924 B
TypeScript

import { useEffect, useState } from 'react'
import {
Contract,
getContractFromSlug,
listenForContract,
} from '../lib/firebase/contracts'
export const useContract = (contractId: string) => {
const [contract, setContract] = useState<Contract | null | 'loading'>(
'loading'
)
useEffect(() => {
if (contractId) return listenForContract(contractId, setContract)
}, [contractId])
return contract
}
export const useContractWithPreload = (
slug: string,
initial: Contract | null
) => {
const [contract, setContract] = useState<Contract | null>(initial)
const [contractId, setContractId] = useState<string | undefined | null>(
initial?.id
)
useEffect(() => {
if (contractId) return listenForContract(contractId, setContract)
if (contractId !== null && slug)
getContractFromSlug(slug).then((c) => setContractId(c?.id || null))
}, [contractId, slug])
return contract
}