import { useCallback , useContext } from 'react' ; import useRxData from './useRxData' ; import { RxCollection , RxDocument } from 'rxdb' ; import Context from './context' ; import { Override } from './type.helpers' ; import { DeepReadonly } from 'rxdb/dist/types/types' ; export interface RxDocumentRet < T > { result ? : DeepReadonly < T > | RxDocument < T > ; isFetching : boolean ; } export interface RxDocumentJSON < T > extends RxDocumentRet < T > { result ? : DeepReadonly < T > ; } export interface RxDocumentDoc < T > extends RxDocumentRet < T > { result ? : RxDocument < T > ; } export interface UseRxDocumentOptions { idAttribute ? : string ; json ? : boolean ; } function useRxDocument < T > ( collectionName , id ? , options ? ) ; function useRxDocument < T > ( collectionName , id ? , options ? ) ; /**
 * Searches for a single document by an id attribute.
 */ function useRxDocument < T > ( collectionName , id ? , options = { } ) { const { idAttribute , json } = options ; const context = useContext ( Context ) ; const preferredIdAttribute = idAttribute || context . idAttribute ; /**
     * As a means of holding off data fetching when id is missing
     * don't return a valid RxQuery object
     */ const queryConstructor = useCallback ( ( c ) => id ? c . findOne ( ) . where ( preferredIdAttribute ) . equals ( id ) : undefined , [ id , preferredIdAttribute ] ) ; const { result , isFetching } = json ? useRxData < T > ( collectionName , queryConstructor , { json : true } ) : useRxData < T > ( collectionName , queryConstructor , { json : false } ) ; return { result : result [ 0 ] , isFetching } ; } export default useRxDocument ;