import { MachineDatastore } from "./MachineDatastores" ; import { useFirestore } from "../../../apps/repository/js/FirestoreProvider" ; import { MachineIDs } from "polar-shared/src/util/MachineIDs" ; import firebase from 'firebase/app' ; import { OnErrorCallback } from "polar-shared/src/util/Snapshots" ; import DocumentSnapshot = firebase . firestore . DocumentSnapshot ; import { useSnapshots } from "../ui/data_loader/UseSnapshotSubscriber" ; import { IDocumentSnapshot } from "polar-snapshot-cache/src/store/IDocumentSnapshot" ; export namespace MachineDatastoreHooks { const COLLECTION_NAME = "machine_datastore" ; function useCollectionRef ( ) { const { firestore } = useFirestore ( ) ; const id = MachineIDs . get ( ) ; return firestore . collection ( COLLECTION_NAME ) . doc ( id ) ; } function toDoc ( snapshot ) { if ( ! snapshot . exists ) { return ; } return snapshot . data ( ) as MachineDatastore ; } /**
     * Callback for when we have new data for the account.
     */ function useMachineDatastore ( ) { const ref = useCollectionRef ( ) ; return ( onNext , onError = ERR_HANDLER ) => { return ref . onSnapshot ( ( snapshot ) => { const doc = toDoc ( snapshot ) ; if ( ! doc ) { return ; } onNext ( doc ) ; } , onError ) ; } ; } export function useMachineDatastoreSnapshots ( ) { const snapshotProvider = useMachineDatastore ( ) ; return useSnapshots ( snapshotProvider ) ; } const ERR_HANDLER = ( err ) => console . error ( "Error during snapshot: " , err ) ; }