react-query-firebase 2.0.0-rc5 → 2.0.0-rc7
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.
- package/package.json +16 -13
- package/{dist/src → react-native}/firestore/useCollectionReference.d.ts +1 -1
- package/{src → react-native}/firestore/useCollectionReference.ts +1 -1
- package/{dist/src → react-native}/firestore/useDocReference.d.ts +1 -1
- package/{dist/src → react-native}/firestore/useDocReference.js +4 -9
- package/{src → react-native}/firestore/useDocReference.ts +5 -14
- package/web/analytics/index.d.ts +3 -0
- package/web/analytics/index.js +3 -0
- package/web/analytics/index.ts +3 -0
- package/web/analytics/useAnalytics.d.ts +5 -0
- package/web/analytics/useAnalytics.js +10 -0
- package/web/analytics/useAnalytics.ts +12 -0
- package/web/analytics/useLogEvent.d.ts +17 -0
- package/web/analytics/useLogEvent.js +21 -0
- package/web/analytics/useLogEvent.ts +34 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.d.ts +10 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.js +16 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.ts +22 -0
- package/web/auth/index.d.ts +13 -0
- package/web/auth/index.js +13 -0
- package/web/auth/index.ts +13 -0
- package/web/auth/mutation-keys.d.ts +7 -0
- package/web/auth/mutation-keys.js +19 -0
- package/web/auth/mutation-keys.ts +19 -0
- package/web/auth/useAuth.d.ts +5 -0
- package/web/auth/useAuth.js +10 -0
- package/web/auth/useAuth.ts +12 -0
- package/web/auth/useAuthStateReady.d.ts +6 -0
- package/web/auth/useAuthStateReady.js +19 -0
- package/web/auth/useAuthStateReady.ts +24 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +15 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.js +19 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.ts +33 -0
- package/web/auth/useCurrentUser.d.ts +6 -0
- package/web/auth/useCurrentUser.js +20 -0
- package/web/auth/useCurrentUser.ts +24 -0
- package/web/auth/useIdToken.d.ts +30 -0
- package/web/auth/useIdToken.js +66 -0
- package/web/auth/useIdToken.ts +83 -0
- package/web/auth/useReauthenticateWitCredentialMutation.d.ts +16 -0
- package/web/auth/useReauthenticateWitCredentialMutation.js +18 -0
- package/web/auth/useReauthenticateWitCredentialMutation.ts +31 -0
- package/web/auth/useReauthenticateWitRedirectMutation.d.ts +17 -0
- package/web/auth/useReauthenticateWitRedirectMutation.js +18 -0
- package/web/auth/useReauthenticateWitRedirectMutation.ts +37 -0
- package/web/auth/useSendEmailVerificationMutation.d.ts +13 -0
- package/web/auth/useSendEmailVerificationMutation.js +16 -0
- package/web/auth/useSendEmailVerificationMutation.ts +28 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.d.ts +15 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.js +19 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.ts +34 -0
- package/web/auth/useSignInWitRedirectMutation.d.ts +14 -0
- package/web/auth/useSignInWitRedirectMutation.js +18 -0
- package/web/auth/useSignInWitRedirectMutation.ts +33 -0
- package/web/auth/useSignOutMutation.d.ts +10 -0
- package/web/auth/useSignOutMutation.js +19 -0
- package/web/auth/useSignOutMutation.ts +25 -0
- package/web/auth/useUpdateProfileMutation.d.ts +15 -0
- package/web/auth/useUpdateProfileMutation.js +17 -0
- package/web/auth/useUpdateProfileMutation.ts +30 -0
- package/web/context/FirebaseContext.d.ts +19 -0
- package/web/context/FirebaseContext.js +8 -0
- package/web/context/FirebaseContext.ts +25 -0
- package/web/context/FirebaseContextProvider.d.ts +127 -0
- package/web/context/FirebaseContextProvider.js +100 -0
- package/web/context/FirebaseContextProvider.tsx +231 -0
- package/web/context/index.d.ts +1 -0
- package/web/context/index.js +1 -0
- package/web/context/index.ts +1 -0
- package/web/firestore/index.d.ts +16 -0
- package/web/firestore/index.js +16 -0
- package/web/firestore/index.ts +16 -0
- package/web/firestore/useAddDocMutation.d.ts +23 -0
- package/web/firestore/useAddDocMutation.js +26 -0
- package/web/firestore/useAddDocMutation.ts +62 -0
- package/web/firestore/useBatchWrite.d.ts +15 -0
- package/web/firestore/useBatchWrite.js +21 -0
- package/web/firestore/useBatchWrite.ts +31 -0
- package/web/firestore/useCollectionReference.d.ts +18 -0
- package/web/firestore/useCollectionReference.js +24 -0
- package/web/firestore/useCollectionReference.ts +37 -0
- package/web/firestore/useCompositeFilter.d.ts +30 -0
- package/web/firestore/useCompositeFilter.js +43 -0
- package/web/firestore/useCompositeFilter.ts +86 -0
- package/web/firestore/useCountQuery.d.ts +23 -0
- package/web/firestore/useCountQuery.js +30 -0
- package/web/firestore/useCountQuery.ts +65 -0
- package/web/firestore/useDeleteDocMutation.d.ts +18 -0
- package/web/firestore/useDeleteDocMutation.js +23 -0
- package/web/firestore/useDeleteDocMutation.ts +47 -0
- package/web/firestore/useDocReference.d.ts +19 -0
- package/web/firestore/useDocReference.js +45 -0
- package/web/firestore/useDocReference.ts +68 -0
- package/web/firestore/useDocReferences.d.ts +23 -0
- package/web/firestore/useDocReferences.js +31 -0
- package/web/firestore/useDocReferences.ts +44 -0
- package/web/firestore/useFirestore.d.ts +7 -0
- package/web/firestore/useFirestore.js +12 -0
- package/web/firestore/useFirestore.ts +13 -0
- package/web/firestore/useGetDocData.d.ts +18 -0
- package/web/firestore/useGetDocData.js +18 -0
- package/web/firestore/useGetDocData.ts +41 -0
- package/web/firestore/useGetRealtimeDocData.d.ts +57 -0
- package/web/firestore/useGetRealtimeDocData.js +56 -0
- package/web/firestore/useGetRealtimeDocData.ts +104 -0
- package/web/firestore/useInfiniteQuery.d.ts +18 -0
- package/web/firestore/useInfiniteQuery.js +28 -0
- package/web/firestore/useInfiniteQuery.ts +91 -0
- package/web/firestore/useQuery.d.ts +25 -0
- package/web/firestore/useQuery.js +34 -0
- package/web/firestore/useQuery.ts +72 -0
- package/web/firestore/useRunTransaction.d.ts +15 -0
- package/web/firestore/useRunTransaction.js +19 -0
- package/web/firestore/useRunTransaction.ts +31 -0
- package/web/firestore/useSetDocMutation.d.ts +20 -0
- package/web/firestore/useSetDocMutation.js +25 -0
- package/web/firestore/useSetDocMutation.ts +52 -0
- package/web/firestore/useUpdateDocMutation.d.ts +31 -0
- package/web/firestore/useUpdateDocMutation.js +37 -0
- package/web/firestore/useUpdateDocMutation.ts +74 -0
- package/web/firestore/utils/getDocData.d.ts +18 -0
- package/web/firestore/utils/getDocData.js +22 -0
- package/web/firestore/utils/getDocData.ts +39 -0
- package/web/firestore/utils/getDocRef.d.ts +19 -0
- package/web/firestore/utils/getDocRef.js +25 -0
- package/web/firestore/utils/getDocRef.ts +46 -0
- package/web/firestore/utils/getDocSnap.d.ts +20 -0
- package/web/firestore/utils/getDocSnap.js +25 -0
- package/web/firestore/utils/getDocSnap.ts +41 -0
- package/web/index.d.ts +5 -0
- package/web/index.js +5 -0
- package/web/index.ts +5 -0
- package/web/remoteConfig/index.d.ts +3 -0
- package/web/remoteConfig/index.js +3 -0
- package/web/remoteConfig/index.ts +3 -0
- package/web/remoteConfig/useFetchAndActivate.d.ts +11 -0
- package/web/remoteConfig/useFetchAndActivate.js +31 -0
- package/web/remoteConfig/useFetchAndActivate.ts +36 -0
- package/web/remoteConfig/useGetValue.d.ts +7 -0
- package/web/remoteConfig/useGetValue.js +15 -0
- package/web/remoteConfig/useGetValue.ts +16 -0
- package/web/remoteConfig/useRemoteConfig.d.ts +5 -0
- package/web/remoteConfig/useRemoteConfig.js +10 -0
- package/web/remoteConfig/useRemoteConfig.ts +11 -0
- /package/{dist/src → react-native}/analytics/index.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/index.js +0 -0
- /package/{src → react-native}/analytics/index.ts +0 -0
- /package/{dist/src → react-native}/analytics/useAnalytics.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/useAnalytics.js +0 -0
- /package/{src → react-native}/analytics/useAnalytics.ts +0 -0
- /package/{dist/src → react-native}/analytics/useLogEvent.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/useLogEvent.js +0 -0
- /package/{src → react-native}/analytics/useLogEvent.ts +0 -0
- /package/{dist/src → react-native}/analytics/useSetAnalyticsCollectionEnabled.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/useSetAnalyticsCollectionEnabled.js +0 -0
- /package/{src → react-native}/analytics/useSetAnalyticsCollectionEnabled.ts +0 -0
- /package/{dist/src → react-native}/auth/index.d.ts +0 -0
- /package/{dist/src → react-native}/auth/index.js +0 -0
- /package/{src → react-native}/auth/index.ts +0 -0
- /package/{dist/src → react-native}/auth/mutation-keys.d.ts +0 -0
- /package/{dist/src → react-native}/auth/mutation-keys.js +0 -0
- /package/{src → react-native}/auth/mutation-keys.ts +0 -0
- /package/{dist/src → react-native}/auth/useAuth.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useAuth.js +0 -0
- /package/{src → react-native}/auth/useAuth.ts +0 -0
- /package/{dist/src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.js +0 -0
- /package/{src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useCurrentUser.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useCurrentUser.js +0 -0
- /package/{src → react-native}/auth/useCurrentUser.ts +0 -0
- /package/{dist/src → react-native}/auth/useIdToken.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useIdToken.js +0 -0
- /package/{src → react-native}/auth/useIdToken.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitCredentialMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitCredentialMutation.js +0 -0
- /package/{src → react-native}/auth/useReauthenticateWitCredentialMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitRedirectMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitRedirectMutation.js +0 -0
- /package/{src → react-native}/auth/useReauthenticateWitRedirectMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSendEmailVerificationMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSendEmailVerificationMutation.js +0 -0
- /package/{src → react-native}/auth/useSendEmailVerificationMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitEmailAndPasswordMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitEmailAndPasswordMutation.js +0 -0
- /package/{src → react-native}/auth/useSignInWitEmailAndPasswordMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitRedirectMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitRedirectMutation.js +0 -0
- /package/{src → react-native}/auth/useSignInWitRedirectMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignOutMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignOutMutation.js +0 -0
- /package/{src → react-native}/auth/useSignOutMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useUpdateProfileMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useUpdateProfileMutation.js +0 -0
- /package/{src → react-native}/auth/useUpdateProfileMutation.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContext.d.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContext.js +0 -0
- /package/{src → react-native}/context/FirebaseContext.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContextProvider.d.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContextProvider.js +0 -0
- /package/{src → react-native}/context/FirebaseContextProvider.tsx +0 -0
- /package/{dist/src → react-native}/context/index.d.ts +0 -0
- /package/{dist/src → react-native}/context/index.js +0 -0
- /package/{src → react-native}/context/index.ts +0 -0
- /package/{dist/src → react-native}/firestore/index.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/index.js +0 -0
- /package/{src → react-native}/firestore/index.ts +0 -0
- /package/{dist/src → react-native}/firestore/useAddDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useAddDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useAddDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/useBatchWrite.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useBatchWrite.js +0 -0
- /package/{src → react-native}/firestore/useBatchWrite.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCollectionReference.js +0 -0
- /package/{dist/src → react-native}/firestore/useCompositeFilter.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCompositeFilter.js +0 -0
- /package/{src → react-native}/firestore/useCompositeFilter.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCountQuery.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCountQuery.js +0 -0
- /package/{src → react-native}/firestore/useCountQuery.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDeleteDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDeleteDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useDeleteDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDocReferences.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDocReferences.js +0 -0
- /package/{src → react-native}/firestore/useDocReferences.ts +0 -0
- /package/{dist/src → react-native}/firestore/useFirestore.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useFirestore.js +0 -0
- /package/{src → react-native}/firestore/useFirestore.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetDocData.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetDocData.js +0 -0
- /package/{src → react-native}/firestore/useGetDocData.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetRealtimeDocData.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetRealtimeDocData.js +0 -0
- /package/{src → react-native}/firestore/useGetRealtimeDocData.ts +0 -0
- /package/{dist/src → react-native}/firestore/useInfiniteQuery.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useInfiniteQuery.js +0 -0
- /package/{src → react-native}/firestore/useInfiniteQuery.ts +0 -0
- /package/{dist/src → react-native}/firestore/useQuery.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useQuery.js +0 -0
- /package/{src → react-native}/firestore/useQuery.ts +0 -0
- /package/{dist/src → react-native}/firestore/useRunTransaction.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useRunTransaction.js +0 -0
- /package/{src → react-native}/firestore/useRunTransaction.ts +0 -0
- /package/{dist/src → react-native}/firestore/useSetDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useSetDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useSetDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/useUpdateDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useUpdateDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useUpdateDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocData.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocData.js +0 -0
- /package/{src → react-native}/firestore/utils/getDocData.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocRef.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocRef.js +0 -0
- /package/{src → react-native}/firestore/utils/getDocRef.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocSnap.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocSnap.js +0 -0
- /package/{src → react-native}/firestore/utils/getDocSnap.ts +0 -0
- /package/{dist/src → react-native}/index.d.ts +0 -0
- /package/{dist/src → react-native}/index.js +0 -0
- /package/{src → react-native}/index.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/index.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/index.js +0 -0
- /package/{src → react-native}/remoteConfig/index.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useFetchAndActivate.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useFetchAndActivate.js +0 -0
- /package/{src → react-native}/remoteConfig/useFetchAndActivate.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useGetValue.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useGetValue.js +0 -0
- /package/{src → react-native}/remoteConfig/useGetValue.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useRemoteConfig.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useRemoteConfig.js +0 -0
- /package/{src → react-native}/remoteConfig/useRemoteConfig.ts +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { doc } from "firebase/firestore";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* Generates a document reference for a specified path or reference in Firestore.
|
|
6
|
+
* If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
|
|
7
|
+
*
|
|
8
|
+
* @param {Firestore} db - The Firestore database instance used to create the document reference.
|
|
9
|
+
* @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - An object containing the path, path segments, and optional reference.
|
|
10
|
+
* @param {string} options.path - The path to the document in the Firestore database.
|
|
11
|
+
* @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
|
|
12
|
+
* @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
|
|
13
|
+
*
|
|
14
|
+
* @returns {DocumentReference<AppModelType, DbModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
|
|
15
|
+
*/
|
|
16
|
+
const getDocReference = (db, { path, pathSegments, reference }) => {
|
|
17
|
+
if (!path) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return (!reference
|
|
21
|
+
? doc(db, path || "", ...(pathSegments || []))
|
|
22
|
+
: reference.type === "collection"
|
|
23
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
24
|
+
: doc(reference, path, ...(pathSegments || [])));
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Custom hook to generate and manage a Firestore document reference.
|
|
28
|
+
* This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
|
|
29
|
+
* The reference is updated whenever the associated path, reference, or path segments change.
|
|
30
|
+
*
|
|
31
|
+
* @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - Configuration options for setting up the document reference.
|
|
32
|
+
* @param {string} options.path - The path to the Firestore document.
|
|
33
|
+
* @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
|
|
34
|
+
* @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
|
|
35
|
+
*
|
|
36
|
+
* @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
|
|
37
|
+
*/
|
|
38
|
+
export const useDocReference = ({ path, reference, pathSegments }) => {
|
|
39
|
+
const db = useFirestore();
|
|
40
|
+
const ref = useRef(getDocReference(db, { path, pathSegments, reference }));
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
ref.current = getDocReference(db, { path, pathSegments, reference });
|
|
43
|
+
}, [path, reference, pathSegments]);
|
|
44
|
+
return ref.current;
|
|
45
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { doc, CollectionReference, DocumentData, DocumentReference, Firestore } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
import { useFirestore } from "./useFirestore";
|
|
5
|
+
|
|
6
|
+
export type UseDocReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
7
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
8
|
+
path?: string;
|
|
9
|
+
pathSegments?: string[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generates a document reference for a specified path or reference in Firestore.
|
|
14
|
+
* If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
|
|
15
|
+
*
|
|
16
|
+
* @param {Firestore} db - The Firestore database instance used to create the document reference.
|
|
17
|
+
* @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - An object containing the path, path segments, and optional reference.
|
|
18
|
+
* @param {string} options.path - The path to the document in the Firestore database.
|
|
19
|
+
* @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
|
|
20
|
+
* @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
|
|
21
|
+
*
|
|
22
|
+
* @returns {DocumentReference<AppModelType, DbModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
|
|
23
|
+
*/
|
|
24
|
+
const getDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>(
|
|
25
|
+
db: Firestore,
|
|
26
|
+
{ path, pathSegments, reference }: UseDocReferenceOptions<AppModelType, DbModelType>
|
|
27
|
+
) => {
|
|
28
|
+
if (!path) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
!reference
|
|
34
|
+
? doc(db, path || "", ...(pathSegments || []))
|
|
35
|
+
: reference.type === "collection"
|
|
36
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
37
|
+
: doc(reference, path, ...(pathSegments || []))
|
|
38
|
+
) as DocumentReference<AppModelType, DbModelType>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Custom hook to generate and manage a Firestore document reference.
|
|
43
|
+
* This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
|
|
44
|
+
* The reference is updated whenever the associated path, reference, or path segments change.
|
|
45
|
+
*
|
|
46
|
+
* @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - Configuration options for setting up the document reference.
|
|
47
|
+
* @param {string} options.path - The path to the Firestore document.
|
|
48
|
+
* @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
|
|
49
|
+
* @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
|
|
50
|
+
*
|
|
51
|
+
* @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
|
|
52
|
+
*/
|
|
53
|
+
export const useDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>({
|
|
54
|
+
path,
|
|
55
|
+
reference,
|
|
56
|
+
pathSegments
|
|
57
|
+
}: UseDocReferenceOptions<AppModelType, DbModelType>) => {
|
|
58
|
+
const db = useFirestore();
|
|
59
|
+
const ref = useRef<DocumentReference<AppModelType, DbModelType> | null>(
|
|
60
|
+
getDocReference(db, { path, pathSegments, reference })
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
ref.current = getDocReference(db, { path, pathSegments, reference });
|
|
65
|
+
}, [path, reference, pathSegments]);
|
|
66
|
+
|
|
67
|
+
return ref.current;
|
|
68
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
3
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
4
|
+
path: string;
|
|
5
|
+
pathSegments?: string[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* A custom hook to generate document references for Firebase Firestore documents using given
|
|
9
|
+
* reference options.
|
|
10
|
+
*
|
|
11
|
+
* The hook accepts an array of options, where each option may contain a path,
|
|
12
|
+
* an existing reference, or path segments, and returns an array of generated
|
|
13
|
+
* Firestore document references based on those options. The references can be used
|
|
14
|
+
* to interact with the Firestore database documents. It uses memoization to optimize
|
|
15
|
+
* reference generation.
|
|
16
|
+
*
|
|
17
|
+
* @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
|
|
18
|
+
* specifying how to generate document references. Each option may include a `path`
|
|
19
|
+
* as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
|
|
20
|
+
* @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
|
|
21
|
+
* generated from the options provided.
|
|
22
|
+
*/
|
|
23
|
+
export declare const useDocReferences: <AppModelType, DbModelType extends DocumentData = DocumentData>(references: UseDocReferencesOptions<AppModelType, DbModelType>[]) => DocumentReference<AppModelType, DbModelType>[];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { doc } from "firebase/firestore";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* A custom hook to generate document references for Firebase Firestore documents using given
|
|
6
|
+
* reference options.
|
|
7
|
+
*
|
|
8
|
+
* The hook accepts an array of options, where each option may contain a path,
|
|
9
|
+
* an existing reference, or path segments, and returns an array of generated
|
|
10
|
+
* Firestore document references based on those options. The references can be used
|
|
11
|
+
* to interact with the Firestore database documents. It uses memoization to optimize
|
|
12
|
+
* reference generation.
|
|
13
|
+
*
|
|
14
|
+
* @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
|
|
15
|
+
* specifying how to generate document references. Each option may include a `path`
|
|
16
|
+
* as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
|
|
17
|
+
* @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
|
|
18
|
+
* generated from the options provided.
|
|
19
|
+
*/
|
|
20
|
+
export const useDocReferences = (references) => {
|
|
21
|
+
const db = useFirestore();
|
|
22
|
+
return useMemo(() => {
|
|
23
|
+
return references.map(({ path, reference, pathSegments }) => {
|
|
24
|
+
return (!reference
|
|
25
|
+
? doc(db, path || "", ...(pathSegments || []))
|
|
26
|
+
: reference.type === "collection"
|
|
27
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
28
|
+
: doc(reference, path, ...(pathSegments || [])));
|
|
29
|
+
});
|
|
30
|
+
}, [references]);
|
|
31
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { CollectionReference, doc, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { useFirestore } from "./useFirestore";
|
|
5
|
+
|
|
6
|
+
export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
7
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
8
|
+
path: string;
|
|
9
|
+
pathSegments?: string[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A custom hook to generate document references for Firebase Firestore documents using given
|
|
14
|
+
* reference options.
|
|
15
|
+
*
|
|
16
|
+
* The hook accepts an array of options, where each option may contain a path,
|
|
17
|
+
* an existing reference, or path segments, and returns an array of generated
|
|
18
|
+
* Firestore document references based on those options. The references can be used
|
|
19
|
+
* to interact with the Firestore database documents. It uses memoization to optimize
|
|
20
|
+
* reference generation.
|
|
21
|
+
*
|
|
22
|
+
* @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
|
|
23
|
+
* specifying how to generate document references. Each option may include a `path`
|
|
24
|
+
* as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
|
|
25
|
+
* @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
|
|
26
|
+
* generated from the options provided.
|
|
27
|
+
*/
|
|
28
|
+
export const useDocReferences = <AppModelType, DbModelType extends DocumentData = DocumentData>(
|
|
29
|
+
references: UseDocReferencesOptions<AppModelType, DbModelType>[]
|
|
30
|
+
) => {
|
|
31
|
+
const db = useFirestore();
|
|
32
|
+
|
|
33
|
+
return useMemo(() => {
|
|
34
|
+
return references.map(({ path, reference, pathSegments }) => {
|
|
35
|
+
return (
|
|
36
|
+
!reference
|
|
37
|
+
? doc(db, path || "", ...(pathSegments || []))
|
|
38
|
+
: reference.type === "collection"
|
|
39
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
40
|
+
: doc(reference, path, ...(pathSegments || []))
|
|
41
|
+
) as DocumentReference<AppModelType, DbModelType>;
|
|
42
|
+
});
|
|
43
|
+
}, [references]);
|
|
44
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a hook to access the Firestore instance from the Firebase context.
|
|
3
|
+
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
4
|
+
*
|
|
5
|
+
* @returns {firestore.Firestore} The Firestore instance from the Firebase context.
|
|
6
|
+
*/
|
|
7
|
+
export declare const useFirestore: () => import("@firebase/firestore").Firestore;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { FirebaseContext } from "../context/FirebaseContext";
|
|
3
|
+
/**
|
|
4
|
+
* Provides a hook to access the Firestore instance from the Firebase context.
|
|
5
|
+
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
6
|
+
*
|
|
7
|
+
* @returns {firestore.Firestore} The Firestore instance from the Firebase context.
|
|
8
|
+
*/
|
|
9
|
+
export const useFirestore = () => {
|
|
10
|
+
const { firestore } = useContext(FirebaseContext);
|
|
11
|
+
return firestore;
|
|
12
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { FirebaseContext } from "../context/FirebaseContext";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Provides a hook to access the Firestore instance from the Firebase context.
|
|
6
|
+
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
7
|
+
*
|
|
8
|
+
* @returns {firestore.Firestore} The Firestore instance from the Firebase context.
|
|
9
|
+
*/
|
|
10
|
+
export const useFirestore = () => {
|
|
11
|
+
const { firestore } = useContext(FirebaseContext);
|
|
12
|
+
return firestore;
|
|
13
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
import { UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseGetDocOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
|
|
4
|
+
options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
5
|
+
path?: string;
|
|
6
|
+
pathSegments?: string[];
|
|
7
|
+
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Custom React Hook to retrieve document data from Firestore using specified parameters.
|
|
11
|
+
* @param {Object} options - The options for configuring the Firestore query.
|
|
12
|
+
* @param {string} reference - The reference to the document in Firestore.
|
|
13
|
+
* @param {string} path - The path to the document in Firestore.
|
|
14
|
+
* @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
|
|
15
|
+
* @returns {Object} Result of the query containing document data and query status.
|
|
16
|
+
*/
|
|
17
|
+
export declare const useGetDocData: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, reference, path, pathSegments }: UseGetDocOptions<AppModelType, DbModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useFirestore } from "./useFirestore";
|
|
2
|
+
import { getDocData } from "./utils/getDocData";
|
|
3
|
+
import { useQuery } from "@tanstack/react-query";
|
|
4
|
+
/**
|
|
5
|
+
* Custom React Hook to retrieve document data from Firestore using specified parameters.
|
|
6
|
+
* @param {Object} options - The options for configuring the Firestore query.
|
|
7
|
+
* @param {string} reference - The reference to the document in Firestore.
|
|
8
|
+
* @param {string} path - The path to the document in Firestore.
|
|
9
|
+
* @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
|
|
10
|
+
* @returns {Object} Result of the query containing document data and query status.
|
|
11
|
+
*/
|
|
12
|
+
export const useGetDocData = ({ options, reference, path, pathSegments }) => {
|
|
13
|
+
const db = useFirestore();
|
|
14
|
+
return useQuery({
|
|
15
|
+
...options,
|
|
16
|
+
queryFn: () => getDocData({ db, reference, path, pathSegments })
|
|
17
|
+
});
|
|
18
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
import { getDocData } from "./utils/getDocData";
|
|
5
|
+
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
|
|
6
|
+
|
|
7
|
+
type UseGetDocOptions<
|
|
8
|
+
AppModelType extends DocumentData = DocumentData,
|
|
9
|
+
DbModelType extends DocumentData = DocumentData
|
|
10
|
+
> = {
|
|
11
|
+
options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> &
|
|
12
|
+
Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
13
|
+
path?: string;
|
|
14
|
+
pathSegments?: string[];
|
|
15
|
+
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Custom React Hook to retrieve document data from Firestore using specified parameters.
|
|
20
|
+
* @param {Object} options - The options for configuring the Firestore query.
|
|
21
|
+
* @param {string} reference - The reference to the document in Firestore.
|
|
22
|
+
* @param {string} path - The path to the document in Firestore.
|
|
23
|
+
* @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
|
|
24
|
+
* @returns {Object} Result of the query containing document data and query status.
|
|
25
|
+
*/
|
|
26
|
+
export const useGetDocData = <
|
|
27
|
+
AppModelType extends DocumentData = DocumentData,
|
|
28
|
+
DbModelType extends DocumentData = DocumentData
|
|
29
|
+
>({
|
|
30
|
+
options,
|
|
31
|
+
reference,
|
|
32
|
+
path,
|
|
33
|
+
pathSegments
|
|
34
|
+
}: UseGetDocOptions<AppModelType, DbModelType>) => {
|
|
35
|
+
const db = useFirestore();
|
|
36
|
+
|
|
37
|
+
return useQuery({
|
|
38
|
+
...options,
|
|
39
|
+
queryFn: () => getDocData<AppModelType, DbModelType>({ db, reference, path, pathSegments })
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
import { FirebaseError } from "firebase/app";
|
|
3
|
+
/**
|
|
4
|
+
* @inline
|
|
5
|
+
*/
|
|
6
|
+
export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
7
|
+
/**
|
|
8
|
+
* A slash-separated path to a document. Has to be omitted to use
|
|
9
|
+
*/
|
|
10
|
+
path?: string;
|
|
11
|
+
/**
|
|
12
|
+
* A reference to a collection.
|
|
13
|
+
*/
|
|
14
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
15
|
+
/**
|
|
16
|
+
* Additional path segments that will be applied relative
|
|
17
|
+
* to the first argument.
|
|
18
|
+
*/
|
|
19
|
+
pathSegments?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* A callback to be called if the listen fails or is
|
|
22
|
+
* cancelled. No further callbacks will occur.
|
|
23
|
+
*/
|
|
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;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A hook to get realtime updates to a firestore document.
|
|
37
|
+
*
|
|
38
|
+
* @group Hook
|
|
39
|
+
*
|
|
40
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
41
|
+
*
|
|
42
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```jsx
|
|
46
|
+
* const firebaseConfig = {};
|
|
47
|
+
* export const MyComponent = () => {
|
|
48
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
49
|
+
* return (
|
|
50
|
+
* <div>
|
|
51
|
+
* {JSON.stringify(result)}
|
|
52
|
+
* </div>
|
|
53
|
+
* );
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => UseGetRealtimeDocDataResult<AppModelType>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { onSnapshot } from "firebase/firestore";
|
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
|
3
|
+
import { useDocReference } from "./useDocReference";
|
|
4
|
+
/**
|
|
5
|
+
* A hook to get realtime updates to a firestore document.
|
|
6
|
+
*
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
10
|
+
*
|
|
11
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```jsx
|
|
15
|
+
* const firebaseConfig = {};
|
|
16
|
+
* export const MyComponent = () => {
|
|
17
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
18
|
+
* return (
|
|
19
|
+
* <div>
|
|
20
|
+
* {JSON.stringify(result)}
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }) => {
|
|
27
|
+
const ref = useDocReference({ path, reference, pathSegments });
|
|
28
|
+
const [doc, setDoc] = useState(null);
|
|
29
|
+
const [isError, setIsError] = useState(false);
|
|
30
|
+
const [isFetching, setIsFetching] = useState(true);
|
|
31
|
+
const [error, setError] = useState(null);
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const unsubscribe = ref
|
|
34
|
+
? onSnapshot(ref, {
|
|
35
|
+
next: async (snapshot) => {
|
|
36
|
+
setIsFetching(false);
|
|
37
|
+
setDoc(snapshot.data() || null);
|
|
38
|
+
setError(null);
|
|
39
|
+
setIsError(false);
|
|
40
|
+
},
|
|
41
|
+
error: (e) => {
|
|
42
|
+
setIsError(true);
|
|
43
|
+
setError(e);
|
|
44
|
+
onError?.(e);
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
: () => { };
|
|
48
|
+
return () => unsubscribe();
|
|
49
|
+
}, [ref, doc, isError, onError, isFetching, error]);
|
|
50
|
+
return useMemo(() => ({
|
|
51
|
+
data: doc,
|
|
52
|
+
isError,
|
|
53
|
+
isFetching,
|
|
54
|
+
error
|
|
55
|
+
}), [doc, isError, error]);
|
|
56
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference, onSnapshot } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useState } from "react";
|
|
4
|
+
import { FirebaseError } from "firebase/app";
|
|
5
|
+
import { useDocReference } from "./useDocReference";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @inline
|
|
9
|
+
*/
|
|
10
|
+
export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
11
|
+
/**
|
|
12
|
+
* A slash-separated path to a document. Has to be omitted to use
|
|
13
|
+
*/
|
|
14
|
+
path?: string;
|
|
15
|
+
/**
|
|
16
|
+
* A reference to a collection.
|
|
17
|
+
*/
|
|
18
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
19
|
+
/**
|
|
20
|
+
* Additional path segments that will be applied relative
|
|
21
|
+
* to the first argument.
|
|
22
|
+
*/
|
|
23
|
+
pathSegments?: string[];
|
|
24
|
+
/**
|
|
25
|
+
* A callback to be called if the listen fails or is
|
|
26
|
+
* cancelled. No further callbacks will occur.
|
|
27
|
+
*/
|
|
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;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A hook to get realtime updates to a firestore document.
|
|
43
|
+
*
|
|
44
|
+
* @group Hook
|
|
45
|
+
*
|
|
46
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
47
|
+
*
|
|
48
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```jsx
|
|
52
|
+
* const firebaseConfig = {};
|
|
53
|
+
* export const MyComponent = () => {
|
|
54
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
55
|
+
* return (
|
|
56
|
+
* <div>
|
|
57
|
+
* {JSON.stringify(result)}
|
|
58
|
+
* </div>
|
|
59
|
+
* );
|
|
60
|
+
* };
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export const useGetRealtimeDocData = <AppModelType, DbModelType extends DocumentData = DocumentData>({
|
|
64
|
+
path,
|
|
65
|
+
pathSegments,
|
|
66
|
+
reference,
|
|
67
|
+
onError
|
|
68
|
+
}: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): UseGetRealtimeDocDataResult<AppModelType> => {
|
|
69
|
+
const ref = useDocReference({ path, reference, pathSegments });
|
|
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);
|
|
74
|
+
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
const unsubscribe = ref
|
|
77
|
+
? onSnapshot(ref, {
|
|
78
|
+
next: async (snapshot) => {
|
|
79
|
+
setIsFetching(false);
|
|
80
|
+
setDoc(snapshot.data() || null);
|
|
81
|
+
setError(null);
|
|
82
|
+
setIsError(false);
|
|
83
|
+
},
|
|
84
|
+
error: (e) => {
|
|
85
|
+
setIsError(true);
|
|
86
|
+
setError(e);
|
|
87
|
+
onError?.(e);
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
: () => {};
|
|
91
|
+
|
|
92
|
+
return () => unsubscribe();
|
|
93
|
+
}, [ref, doc, isError, onError, isFetching, error]);
|
|
94
|
+
|
|
95
|
+
return useMemo(
|
|
96
|
+
() => ({
|
|
97
|
+
data: doc,
|
|
98
|
+
isError,
|
|
99
|
+
isFetching,
|
|
100
|
+
error
|
|
101
|
+
}),
|
|
102
|
+
[doc, isError, error]
|
|
103
|
+
);
|
|
104
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DocumentData, FirestoreDataConverter, Query, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
|
+
import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
|
|
3
|
+
type UseInfiniteQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TQueryKey extends QueryKey = QueryKey> = {
|
|
4
|
+
options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryKey">>;
|
|
5
|
+
query: Query<AppModelType, DbModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: QueryCompositeFilterConstraint;
|
|
8
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
|
|
12
|
+
* It fetches data in pages and can load more as required.
|
|
13
|
+
*
|
|
14
|
+
* @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
|
|
15
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
16
|
+
*/
|
|
17
|
+
export declare const useInfiniteQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, query: queryReference, queryConstraints, compositeFilter, converter }: UseInfiniteQueryOptions<AppModelType, DbModelType>) => UseInfiniteQueryResult<InfiniteData<AppModelType[]>>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getDocs, query } from "firebase/firestore";
|
|
2
|
+
import { useInfiniteQuery as useInfiniteReactQuery } from "@tanstack/react-query";
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
|
|
5
|
+
* It fetches data in pages and can load more as required.
|
|
6
|
+
*
|
|
7
|
+
* @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
|
|
8
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
9
|
+
*/
|
|
10
|
+
export const useInfiniteQuery = ({ options, query: queryReference, queryConstraints = [], compositeFilter, converter }) => {
|
|
11
|
+
return useInfiniteReactQuery({
|
|
12
|
+
...options,
|
|
13
|
+
queryFn: async ({ pageParam }) => {
|
|
14
|
+
const allQueryConstraints = [...queryConstraints, ...(pageParam ? [pageParam] : [])];
|
|
15
|
+
const queryToExecute = compositeFilter
|
|
16
|
+
? query(queryReference, compositeFilter, ...allQueryConstraints)
|
|
17
|
+
: query(queryReference, ...allQueryConstraints);
|
|
18
|
+
const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
|
|
19
|
+
const docs = [];
|
|
20
|
+
if (querySnapshot) {
|
|
21
|
+
querySnapshot.forEach((doc) => {
|
|
22
|
+
docs.push(doc.data());
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return docs;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|