import { FirebaseApp } from "firebase/app" ; import { FirebaseStorage , getDownloadURL , getStorage , ref , uploadBytes } from "firebase/storage" ; import { StorageSource , UploadFileProps } from "../../models" ; import { useEffect , useState } from "react" ; category export interface FirebaseStorageSourceProps { firebaseApp ? : FirebaseApp ; } /**
 * Use this hook to build an  StorageSource based on Firebase storage
 *  category export function useFirebaseStorageSource ( { firebaseApp } ) { const [ storage , setStorage ] = useState < FirebaseStorage > ( ) ; useEffect ( ( ) => { if ( ! firebaseApp ) return ; setStorage ( getStorage ( firebaseApp ) ) ; } , [ firebaseApp ] ) ; const urlsCache = { } ; return { uploadFile ( { file , fileName , path , metadata } ) { if ( ! storage ) throw Error ( "useFirebaseStorageSource Firebase not initialised" ) ; const usedFilename = fileName ?? file . name ; console . debug ( "Uploading file" , usedFilename , file , path , metadata ) ; return uploadBytes ( ref ( storage , `${ path }/${ usedFilename }` ) , file , metadata ) . then ( snapshot => ( { path : snapshot . ref . fullPath } ) ) ; } , getDownloadURL ( storagePath ) { if ( ! storage ) throw Error ( "useFirebaseStorageSource Firebase not initialised" ) ; if ( urlsCache [ storagePath ] ) return urlsCache [ storagePath ] ; const downloadURL = getDownloadURL ( ref ( storage , storagePath ) ) ; urlsCache [ storagePath ] = downloadURL ; return downloadURL ; } } ; }