manifold/web/hooks/use-contract.ts

28 lines
689 B
TypeScript
Raw Permalink Normal View History

2021-12-09 22:05:55 +00:00
import { useEffect, useState } from 'react'
import { Contract, listenForContract } from '../lib/firebase/contracts'
export const useContract = (contractId: string) => {
2021-12-09 23:23:21 +00:00
const [contract, setContract] = useState<Contract | null | 'loading'>(
'loading'
)
2021-12-09 22:05:55 +00:00
2021-12-09 23:23:21 +00:00
useEffect(() => {
if (contractId) return listenForContract(contractId, setContract)
}, [contractId])
2021-12-09 22:05:55 +00:00
2021-12-09 23:23:21 +00:00
return contract
}
export const useContractWithPreload = (
contractId: string,
initial: Contract | null
) => {
const [contract, setContract] = useState<Contract | null>(initial)
useEffect(() => {
if (contractId) return listenForContract(contractId, setContract)
}, [contractId])
return contract
}