manifold/web/hooks/use-contract.ts

35 lines
985 B
TypeScript
Raw Permalink Normal View History

import { useEffect } from 'react'
import { useFirestoreDocumentData } from '@react-query-firebase/firestore'
import {
Contract,
contractDocRef,
listenForContract,
} from 'web/lib/firebase/contracts'
import { useStateCheckEquality } from './use-state-check-equality'
import { DocumentData } from 'firebase/firestore'
2021-12-09 22:05:55 +00:00
export const useContract = (contractId: string) => {
const result = useFirestoreDocumentData<DocumentData, Contract>(
['contracts', contractId],
contractDocRef(contractId),
{ subscribe: true, includeMetadataChanges: true }
2021-12-09 23:23:21 +00:00
)
2021-12-09 22:05:55 +00:00
return result.isLoading ? undefined : result.data
2021-12-09 23:23:21 +00:00
}
export const useContractWithPreload = (
initial: Contract | null | undefined
) => {
const [contract, setContract] = useStateCheckEquality<
Contract | null | undefined
>(initial)
2022-04-06 17:32:57 +00:00
const contractId = initial?.id
useEffect(() => {
if (contractId) return listenForContract(contractId, setContract)
}, [contractId, setContract])
return contract
}