import * as React from 'react' ; import { getApps , initializeApp , registerVersion } from 'firebase/app' ; import type { FirebaseApp , FirebaseOptions } from 'firebase/app' ; const DEFAULT_APP_NAME = '[DEFAULT]' ; const FirebaseAppContext = React . createContext < FirebaseApp | undefined > ( undefined ) ; const SuspenseEnabledContext = React . createContext < boolean > ( false ) ; interface FirebaseAppProviderProps { firebaseApp ? : FirebaseApp ; firebaseConfig ? : FirebaseOptions ; appName ? : string ; suspense ? : boolean ; } export const version = __REACTFIRE_VERSION__ ; const shallowEq = ( a , b ) => a === b || [ ... Object . keys ( a ) , ... Object . keys ( b ) ] . every ( ( key ) => a [ key ] === b [ key ] ) ; export function FirebaseAppProvider ( props ) { const { firebaseConfig , appName , suspense } = props ; const firebaseApp = React . useMemo ( ( ) => { if ( props . firebaseApp ) { return props . firebaseApp ; } const existingApp = getApps ( ) . find ( ( app ) => app . name === ( appName || DEFAULT_APP_NAME ) ) ; if ( existingApp ) { if ( firebaseConfig && shallowEq ( existingApp . options , firebaseConfig ) ) { return existingApp ; } else { throw new Error ( `Does not match the options already provided to the ${ appName || 'default' } firebase app instance, give this new instance a different appName.` ) ; } } else { if ( ! firebaseConfig ) { throw new Error ( 'No firebaseConfig provided' ) ; } const reactVersion = React . version || 'unknown' ; registerVersion ( 'react' , reactVersion ) ; registerVersion ( 'reactfire' , version ) ; return initializeApp ( firebaseConfig , appName ) ; } } , [ props . firebaseApp , firebaseConfig , appName ] ) ; return ( < FirebaseAppContext . Provider value = { firebaseApp } > < SuspenseEnabledContext . Provider value = { suspense ?? false } { ... props } /> < /FirebaseAppContext.Provider>); } export function useIsSuspenseEnabled ( ) { const suspense = React . useContext ( SuspenseEnabledContext ) ; return suspense ?? false ; } export function useSuspenseEnabledFromConfigAndContext ( suspenseFromConfig ? ) { let suspenseFromContext = React . useContext ( SuspenseEnabledContext ) ; if ( suspenseFromConfig !== undefined ) { return suspenseFromConfig ; } return suspenseFromContext ; } export function useFirebaseApp ( ) { const firebaseApp = React . useContext ( FirebaseAppContext ) ; if ( ! firebaseApp ) { throw new Error ( 'Cannot call useFirebaseApp unless your component is within a FirebaseAppProvider' ) ; } return firebaseApp ; }