react-query-firebase 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import React, { PropsWithChildren } from "react";
2
2
  import { ConsentSettings } from "firebase/analytics";
3
3
  import { RemoteConfigSettings } from "firebase/remote-config";
4
+ import { FirestoreSettings } from "firebase/firestore";
4
5
  import { FirebaseOptions } from "firebase/app";
5
6
  /**
6
7
  * @inline
@@ -71,6 +72,13 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
71
72
  * }
72
73
  */
73
74
  consentSettings?: ConsentSettings;
75
+ /**
76
+ * Specifies custom configurations for your Cloud Firestore instance.
77
+ * You must set these before invoking any other methods.
78
+ * {@link https://firebase.google.com/docs/reference/js/firestore_.firestoresettings}
79
+ * @defaultValue {}
80
+ */
81
+ firestoreSettings?: FirestoreSettings;
74
82
  /**
75
83
  * Flag indicating whether Firebase Firestore should be enabled.
76
84
  * @defaultValue `true`
@@ -3,7 +3,7 @@ import { FirebaseContext } from "./FirebaseContext";
3
3
  import { connectAuthEmulator, getAuth } from "firebase/auth";
4
4
  import { getAnalytics, setAnalyticsCollectionEnabled, setConsent } from "firebase/analytics";
5
5
  import { getRemoteConfig } from "firebase/remote-config";
6
- import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
6
+ import { connectFirestoreEmulator, initializeFirestore } from "firebase/firestore";
7
7
  import { initializeApp } from "firebase/app";
8
8
  /**
9
9
  * FirebaseContextProvider component configures and provides Firebase services to its children.
@@ -28,7 +28,7 @@ import { initializeApp } from "firebase/app";
28
28
  * };
29
29
  * ```
30
30
  */
31
- export const FirebaseContextProvider = ({ emulators, options, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true, consentSettings = {}, remoteConfigEnabled = true, remoteConfigSettings, remoteConfigDefaults = {} }) => {
31
+ export const FirebaseContextProvider = ({ emulators, options, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true, consentSettings = {}, remoteConfigEnabled = true, remoteConfigSettings, remoteConfigDefaults = {}, firestoreSettings }) => {
32
32
  const firebase = useMemo(() => {
33
33
  return initializeApp(options);
34
34
  }, [options]);
@@ -44,39 +44,53 @@ export const FirebaseContextProvider = ({ emulators, options, children, authEnab
44
44
  ...consentSettings
45
45
  });
46
46
  }, [consentSettings]);
47
- const contextValue = useMemo(() => {
48
- const value = {};
47
+ const firestore = useMemo(() => {
49
48
  if (firestoreEnabled) {
50
- const firestore = getFirestore(firebase);
49
+ const localFirestore = initializeFirestore(firebase, firestoreSettings || {});
51
50
  if (emulators?.firestore?.host && emulators?.firestore?.port) {
52
- connectFirestoreEmulator(firestore, emulators.firestore.host, emulators.firestore.port);
51
+ connectFirestoreEmulator(localFirestore, emulators.firestore.host, emulators.firestore.port);
53
52
  }
54
- value.firestore = firestore;
53
+ return localFirestore;
55
54
  }
55
+ return null;
56
+ }, [firestoreSettings, emulators?.firestore, firestoreEnabled]);
57
+ const auth = useMemo(() => {
56
58
  if (authEnabled) {
57
- const auth = getAuth(firebase);
59
+ const localAuth = getAuth(firebase);
58
60
  if (emulators?.auth?.host) {
59
- connectAuthEmulator(auth, emulators?.auth?.host, {
61
+ connectAuthEmulator(localAuth, emulators?.auth?.host, {
60
62
  disableWarnings: true
61
63
  });
62
64
  }
63
- value.auth = auth;
65
+ return localAuth;
64
66
  }
67
+ return null;
68
+ }, [emulators?.auth, authEnabled]);
69
+ const analytics = useMemo(() => {
65
70
  if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
66
- const analytics = getAnalytics(firebase);
67
- value.analytics = analytics;
71
+ return getAnalytics(firebase);
68
72
  }
73
+ return null;
74
+ }, [analyticsEnabled, options.measurementId]);
75
+ const remoteConfig = useMemo(() => {
69
76
  if (remoteConfigEnabled && typeof window !== "undefined") {
70
- const remoteConfig = getRemoteConfig(firebase);
77
+ const localRemoteConfig = getRemoteConfig(firebase);
71
78
  if (remoteConfigSettings) {
72
- remoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
73
- remoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
74
- remoteConfig.defaultConfig = remoteConfigDefaults;
79
+ localRemoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
80
+ localRemoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
81
+ localRemoteConfig.defaultConfig = remoteConfigDefaults;
75
82
  }
76
- value.remoteConfig = remoteConfig;
83
+ return localRemoteConfig;
77
84
  }
78
- return { firebase, ...value };
79
- }, [firebase]);
85
+ return null;
86
+ }, [remoteConfigEnabled]);
87
+ const contextValue = useMemo(() => ({
88
+ firebase,
89
+ auth,
90
+ analytics,
91
+ firestore,
92
+ remoteConfig
93
+ }), [firebase, auth, analytics, firestore, remoteConfig]);
80
94
  useEffect(() => {
81
95
  if (contextValue.analytics) {
82
96
  setAnalyticsCollectionEnabled(contextValue.analytics, consentSettings?.analytics_storage === "granted");
@@ -11,7 +11,7 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
11
11
  /**
12
12
  * A reference to a collection.
13
13
  */
14
- reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
14
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
15
15
  /**
16
16
  * Additional path segments that will be applied relative
17
17
  * to the first argument.
@@ -21,7 +21,16 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
21
21
  * A callback to be called if the listen fails or is
22
22
  * cancelled. No further callbacks will occur.
23
23
  */
24
- onError: (error: FirebaseError) => unknown;
24
+ onError?: (error: FirebaseError) => unknown;
25
+ };
26
+ /**
27
+ * @inline
28
+ */
29
+ export type UseGetRealtimeDocDataResult<AppModelType> = {
30
+ data: AppModelType | null;
31
+ isError: boolean;
32
+ error: FirebaseError | null;
33
+ isFetching: boolean;
25
34
  };
26
35
  /**
27
36
  * A hook to get realtime updates to a firestore document.
@@ -30,19 +39,19 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
30
39
  *
31
40
  * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
32
41
  *
33
- * @returns {AppModelType | null}
42
+ * @returns {UseGetRealtimeDocDataResult<AppModelType>}
34
43
  *
35
44
  * @example
36
45
  * ```jsx
37
46
  * const firebaseConfig = {};
38
47
  * export const MyComponent = () => {
39
- * const doc = useGetRealtimeDocData('collection/documentId');
48
+ * const result = useGetRealtimeDocData('collection/documentId');
40
49
  * return (
41
50
  * <div>
42
- * {JSON.stringify(doc)}
51
+ * {JSON.stringify(result)}
43
52
  * </div>
44
53
  * );
45
54
  * };
46
55
  * ```
47
56
  */
48
- export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => AppModelType | null;
57
+ export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => UseGetRealtimeDocDataResult<AppModelType>;
@@ -1,5 +1,5 @@
1
1
  import { onSnapshot } from "firebase/firestore";
2
- import { useEffect, useState } from "react";
2
+ import { useEffect, useMemo, useState } from "react";
3
3
  import { useDocReference } from "./useDocReference";
4
4
  /**
5
5
  * A hook to get realtime updates to a firestore document.
@@ -8,16 +8,16 @@ import { useDocReference } from "./useDocReference";
8
8
  *
9
9
  * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
10
10
  *
11
- * @returns {AppModelType | null}
11
+ * @returns {UseGetRealtimeDocDataResult<AppModelType>}
12
12
  *
13
13
  * @example
14
14
  * ```jsx
15
15
  * const firebaseConfig = {};
16
16
  * export const MyComponent = () => {
17
- * const doc = useGetRealtimeDocData('collection/documentId');
17
+ * const result = useGetRealtimeDocData('collection/documentId');
18
18
  * return (
19
19
  * <div>
20
- * {JSON.stringify(doc)}
20
+ * {JSON.stringify(result)}
21
21
  * </div>
22
22
  * );
23
23
  * };
@@ -26,16 +26,31 @@ import { useDocReference } from "./useDocReference";
26
26
  export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }) => {
27
27
  const ref = useDocReference({ path, reference, pathSegments });
28
28
  const [doc, setDoc] = useState(null);
29
+ const [isError, setIsError] = useState(false);
30
+ const [isFetching, setIsFetching] = useState(true);
31
+ const [error, setError] = useState(null);
29
32
  useEffect(() => {
30
33
  const unsubscribe = ref
31
34
  ? onSnapshot(ref, {
32
35
  next: async (snapshot) => {
36
+ setIsFetching(false);
33
37
  setDoc(snapshot.data() || null);
38
+ setError(null);
39
+ setIsError(false);
34
40
  },
35
- error: onError
41
+ error: (e) => {
42
+ setIsError(true);
43
+ setError(e);
44
+ onError?.(e);
45
+ }
36
46
  })
37
47
  : () => { };
38
48
  return () => unsubscribe();
39
- }, [ref, doc]);
40
- return doc;
49
+ }, [ref, doc, isError, onError, isFetching, error]);
50
+ return useMemo(() => ({
51
+ data: doc,
52
+ isError,
53
+ isFetching,
54
+ error
55
+ }), [doc, isError, error]);
41
56
  };
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "docs:build": "vitepress build docs",
61
61
  "docs:preview": "vitepress preview docs"
62
62
  },
63
- "version": "1.3.0"
63
+ "version": "1.3.2"
64
64
  }
@@ -3,7 +3,7 @@ import { FirebaseContext } from "./FirebaseContext";
3
3
  import { connectAuthEmulator, getAuth } from "firebase/auth";
4
4
  import { ConsentSettings, getAnalytics, setAnalyticsCollectionEnabled, setConsent } from "firebase/analytics";
5
5
  import { getRemoteConfig, RemoteConfigSettings } from "firebase/remote-config";
6
- import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
6
+ import { connectFirestoreEmulator, FirestoreSettings, initializeFirestore } from "firebase/firestore";
7
7
  import { FirebaseOptions, initializeApp } from "firebase/app";
8
8
 
9
9
  /**
@@ -78,6 +78,13 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
78
78
  * }
79
79
  */
80
80
  consentSettings?: ConsentSettings;
81
+ /**
82
+ * Specifies custom configurations for your Cloud Firestore instance.
83
+ * You must set these before invoking any other methods.
84
+ * {@link https://firebase.google.com/docs/reference/js/firestore_.firestoresettings}
85
+ * @defaultValue {}
86
+ */
87
+ firestoreSettings?: FirestoreSettings;
81
88
  /**
82
89
  * Flag indicating whether Firebase Firestore should be enabled.
83
90
  * @defaultValue `true`
@@ -132,7 +139,8 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
132
139
  consentSettings = {},
133
140
  remoteConfigEnabled = true,
134
141
  remoteConfigSettings,
135
- remoteConfigDefaults = {}
142
+ remoteConfigDefaults = {},
143
+ firestoreSettings
136
144
  }) => {
137
145
  const firebase = useMemo(() => {
138
146
  return initializeApp(options);
@@ -151,46 +159,63 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
151
159
  });
152
160
  }, [consentSettings]);
153
161
 
154
- const contextValue = useMemo(() => {
155
- const value: Partial<React.ContextType<typeof FirebaseContext>> = {};
156
-
162
+ const firestore = useMemo(() => {
157
163
  if (firestoreEnabled) {
158
- const firestore = getFirestore(firebase);
164
+ const localFirestore = initializeFirestore(firebase, firestoreSettings || {});
159
165
 
160
166
  if (emulators?.firestore?.host && emulators?.firestore?.port) {
161
- connectFirestoreEmulator(firestore, emulators.firestore.host, emulators.firestore.port);
167
+ connectFirestoreEmulator(localFirestore, emulators.firestore.host, emulators.firestore.port);
162
168
  }
163
169
 
164
- value.firestore = firestore;
170
+ return localFirestore;
165
171
  }
166
172
 
173
+ return null;
174
+ }, [firestoreSettings, emulators?.firestore, firestoreEnabled]);
175
+
176
+ const auth = useMemo(() => {
167
177
  if (authEnabled) {
168
- const auth = getAuth(firebase);
178
+ const localAuth = getAuth(firebase);
169
179
  if (emulators?.auth?.host) {
170
- connectAuthEmulator(auth, emulators?.auth?.host, {
180
+ connectAuthEmulator(localAuth, emulators?.auth?.host, {
171
181
  disableWarnings: true
172
182
  });
173
183
  }
174
- value.auth = auth;
184
+ return localAuth;
175
185
  }
186
+ return null;
187
+ }, [emulators?.auth, authEnabled]);
176
188
 
189
+ const analytics = useMemo(() => {
177
190
  if (analyticsEnabled && options.measurementId && typeof window !== "undefined") {
178
- const analytics = getAnalytics(firebase);
179
- value.analytics = analytics;
191
+ return getAnalytics(firebase);
180
192
  }
193
+ return null;
194
+ }, [analyticsEnabled, options.measurementId]);
181
195
 
196
+ const remoteConfig = useMemo(() => {
182
197
  if (remoteConfigEnabled && typeof window !== "undefined") {
183
- const remoteConfig = getRemoteConfig(firebase);
198
+ const localRemoteConfig = getRemoteConfig(firebase);
184
199
  if (remoteConfigSettings) {
185
- remoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
186
- remoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
187
- remoteConfig.defaultConfig = remoteConfigDefaults;
200
+ localRemoteConfig.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeoutMillis;
201
+ localRemoteConfig.settings.minimumFetchIntervalMillis = remoteConfigSettings.minimumFetchIntervalMillis;
202
+ localRemoteConfig.defaultConfig = remoteConfigDefaults;
188
203
  }
189
- value.remoteConfig = remoteConfig;
204
+ return localRemoteConfig;
190
205
  }
191
-
192
- return { firebase, ...value };
193
- }, [firebase]);
206
+ return null;
207
+ }, [remoteConfigEnabled]);
208
+
209
+ const contextValue = useMemo(
210
+ () => ({
211
+ firebase,
212
+ auth,
213
+ analytics,
214
+ firestore,
215
+ remoteConfig
216
+ }),
217
+ [firebase, auth, analytics, firestore, remoteConfig]
218
+ );
194
219
 
195
220
  useEffect(() => {
196
221
  if (contextValue.analytics) {
@@ -1,6 +1,6 @@
1
1
  import { CollectionReference, DocumentData, DocumentReference, onSnapshot } from "firebase/firestore";
2
2
 
3
- import { useEffect, useState } from "react";
3
+ import { useEffect, useMemo, useState } from "react";
4
4
  import { FirebaseError } from "firebase/app";
5
5
  import { useDocReference } from "./useDocReference";
6
6
 
@@ -15,7 +15,7 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
15
15
  /**
16
16
  * A reference to a collection.
17
17
  */
18
- reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
18
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
19
19
  /**
20
20
  * Additional path segments that will be applied relative
21
21
  * to the first argument.
@@ -25,7 +25,17 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
25
25
  * A callback to be called if the listen fails or is
26
26
  * cancelled. No further callbacks will occur.
27
27
  */
28
- onError: (error: FirebaseError) => unknown;
28
+ onError?: (error: FirebaseError) => unknown;
29
+ };
30
+
31
+ /**
32
+ * @inline
33
+ */
34
+ export type UseGetRealtimeDocDataResult<AppModelType> = {
35
+ data: AppModelType | null;
36
+ isError: boolean;
37
+ error: FirebaseError | null;
38
+ isFetching: boolean;
29
39
  };
30
40
 
31
41
  /**
@@ -35,16 +45,16 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
35
45
  *
36
46
  * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
37
47
  *
38
- * @returns {AppModelType | null}
48
+ * @returns {UseGetRealtimeDocDataResult<AppModelType>}
39
49
  *
40
50
  * @example
41
51
  * ```jsx
42
52
  * const firebaseConfig = {};
43
53
  * export const MyComponent = () => {
44
- * const doc = useGetRealtimeDocData('collection/documentId');
54
+ * const result = useGetRealtimeDocData('collection/documentId');
45
55
  * return (
46
56
  * <div>
47
- * {JSON.stringify(doc)}
57
+ * {JSON.stringify(result)}
48
58
  * </div>
49
59
  * );
50
60
  * };
@@ -55,22 +65,40 @@ export const useGetRealtimeDocData = <AppModelType, DbModelType extends Document
55
65
  pathSegments,
56
66
  reference,
57
67
  onError
58
- }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): AppModelType | null => {
68
+ }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): UseGetRealtimeDocDataResult<AppModelType> => {
59
69
  const ref = useDocReference({ path, reference, pathSegments });
60
70
  const [doc, setDoc] = useState<AppModelType | null>(null);
71
+ const [isError, setIsError] = useState(false);
72
+ const [isFetching, setIsFetching] = useState(true);
73
+ const [error, setError] = useState<FirebaseError | null>(null);
61
74
 
62
75
  useEffect(() => {
63
76
  const unsubscribe = ref
64
77
  ? onSnapshot(ref, {
65
78
  next: async (snapshot) => {
79
+ setIsFetching(false);
66
80
  setDoc(snapshot.data() || null);
81
+ setError(null);
82
+ setIsError(false);
67
83
  },
68
- error: onError
84
+ error: (e) => {
85
+ setIsError(true);
86
+ setError(e);
87
+ onError?.(e);
88
+ }
69
89
  })
70
90
  : () => {};
71
91
 
72
92
  return () => unsubscribe();
73
- }, [ref, doc]);
93
+ }, [ref, doc, isError, onError, isFetching, error]);
74
94
 
75
- return doc;
95
+ return useMemo(
96
+ () => ({
97
+ data: doc,
98
+ isError,
99
+ isFetching,
100
+ error
101
+ }),
102
+ [doc, isError, error]
103
+ );
76
104
  };