99 lines
3.3 KiB
JavaScript
99 lines
3.3 KiB
JavaScript
import AsyncStorage from '@react-native-async-storage/async-storage'
|
|
import { getReactNativePersistence, initializeAuth } from 'firebase/auth/react-native'
|
|
import firebase from 'firebase/compat/app'
|
|
import 'firebase/compat/auth'
|
|
import 'firebase/compat/firestore'
|
|
import 'firebase/compat/functions'
|
|
import 'firebase/compat/storage'
|
|
import { Platform } from 'react-native'
|
|
|
|
const functionsInstances = {}
|
|
const emulatorConfigured = {}
|
|
const emulatorHost = Platform.OS === 'web' ? 'localhost' : '192.168.1.60'
|
|
const USE_FUNCTIONS_EMULATOR = false // Toggle to route functions traffic to the local emulator.
|
|
|
|
const configureFunctionsEmulator = (instance, regionKey = 'us-central1') => {
|
|
const shouldUseEmulator = USE_FUNCTIONS_EMULATOR && instance?.useEmulator
|
|
|
|
if (!shouldUseEmulator || emulatorConfigured[regionKey]) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
instance.useEmulator(emulatorHost, 5001)
|
|
emulatorConfigured[regionKey] = true
|
|
} catch (error) {
|
|
console.warn(
|
|
`[firebase] Unable to set functions emulator for region ${regionKey}`,
|
|
error?.message
|
|
)
|
|
}
|
|
}
|
|
|
|
export const firebaseConfig = {
|
|
apiKey: 'AIzaSyCuHJHdwVN_F-VmUG4Hd7bGiMRqj6rPlLo',
|
|
authDomain: 'musicland-d33f9.firebaseapp.com',
|
|
projectId: 'musicland-d33f9',
|
|
storageBucket: 'musicland-d33f9.firebasestorage.app',
|
|
messagingSenderId: '305598753437',
|
|
appId: '1:305598753437:web:9930f10af59e5cf7f28274',
|
|
measurementId: 'G-RV4MS1SVTT',
|
|
}
|
|
|
|
if (!firebase?.apps?.filter(({ name_ }) => name_ === '[DEFAULT]').length) {
|
|
const defaultApp = firebase.initializeApp(firebaseConfig)
|
|
|
|
initializeAuth(defaultApp, {
|
|
persistence: getReactNativePersistence(AsyncStorage),
|
|
})
|
|
|
|
// if (__DEV__) {
|
|
// firebase.functions().useEmulator("localhost", 5001);
|
|
// }
|
|
const defaultFunctions = firebase.functions()
|
|
functionsInstances['us-central1'] = defaultFunctions
|
|
configureFunctionsEmulator(defaultFunctions, 'us-central1')
|
|
console.log('Firebase init')
|
|
}
|
|
|
|
// Firestore settings for React Native/iOS: avoid streaming transport issues
|
|
const firestore = firebase.firestore()
|
|
try {
|
|
// Prefer auto-detect; disable fetch streams for RN iOS stability
|
|
firebase.firestore().settings({
|
|
experimentalAutoDetectLongPolling: true,
|
|
useFetchStreams: false,
|
|
ignoreUndefinedProperties: true,
|
|
})
|
|
} catch (e) {
|
|
// Ignore if settings were already set elsewhere
|
|
}
|
|
|
|
export const usersRef = firestore.collection('users')
|
|
export const projectsRef = firestore.collection('projects')
|
|
export const tasksRef = firestore.collection('tasks')
|
|
export const playlistsRef = firestore.collection('playlists')
|
|
export const chatsRef = firestore.collection('chats')
|
|
export const videosRef = firestore.collection('videos')
|
|
export const notificationsRef = firestore.collection('notifications')
|
|
export const reportsRef = firestore.collection('reports')
|
|
|
|
export const { arrayUnion, arrayRemove, increment, serverTimestamp } = firebase.firestore.FieldValue
|
|
export const deleteField = firebase.firestore.FieldValue.delete
|
|
|
|
export const getFunctionsClient = (region = 'us-central1') => {
|
|
const regionKey = region || 'us-central1'
|
|
|
|
if (!functionsInstances[regionKey]) {
|
|
functionsInstances[regionKey] = firebase.app().functions(regionKey)
|
|
}
|
|
|
|
const instance = functionsInstances[regionKey]
|
|
|
|
configureFunctionsEmulator(instance, regionKey)
|
|
|
|
return instance
|
|
}
|
|
|
|
export default firebase
|