-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Existing hooks are deprecated and will be removed with the next major version update
- Loading branch information
1 parent
e3a832d
commit 47e58ef
Showing
10 changed files
with
277 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,5 @@ | ||
import { DocumentData, FirestoreError, onSnapshot, Query, QuerySnapshot, SnapshotListenOptions } from "firebase/firestore"; | ||
import { useCallback } from "react"; | ||
import { ValueHookResult } from "../common/types.js"; | ||
import { useListen, UseListenOnChange } from "../internal/useListen.js"; | ||
import { LoadingState } from "../internal/useLoadingValue.js"; | ||
import { isQueryEqual } from "./internal.js"; | ||
|
||
export type UseCollectionResult<Value extends DocumentData = DocumentData> = ValueHookResult< | ||
QuerySnapshot<Value>, | ||
FirestoreError | ||
>; | ||
|
||
/** | ||
* Options to configure the subscription | ||
*/ | ||
export interface UseCollectionOptions { | ||
snapshotListenOptions?: SnapshotListenOptions; | ||
} | ||
|
||
/** | ||
* Returns and updates a QuerySnapshot of a Firestore Query | ||
* | ||
* @template Value Type of the collection data | ||
* @param {Query<Value> | undefined | null} query Firestore query that will be subscribed to | ||
* @param {?UseCollectionOptions} options Options to configure the subscription | ||
* @returns {UseCollectionResult<Value>} QuerySnapshot, loading, and error | ||
* * value: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred | ||
* * loading: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred | ||
* * error: `undefined` if no error occurred | ||
*/ | ||
export function useCollection<Value extends DocumentData = DocumentData>( | ||
query: Query<Value> | undefined | null, | ||
options?: UseCollectionOptions | ||
): UseCollectionResult<Value> { | ||
const { snapshotListenOptions = {} } = options ?? {}; | ||
|
||
const onChange: UseListenOnChange<QuerySnapshot<Value>, FirestoreError, Query<Value>> = useCallback( | ||
(stableQuery, next, error) => | ||
onSnapshot<Value>(stableQuery, snapshotListenOptions, { | ||
next, | ||
error, | ||
}), | ||
[] | ||
); | ||
|
||
return useListen(query ?? undefined, onChange, isQueryEqual, LoadingState); | ||
} | ||
export { | ||
useQuery as useCollection, | ||
UseQueryOptions as UseCollectionOptions, | ||
UseQueryResult as UseCollectionResult, | ||
} from "./useQuery.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,5 @@ | ||
import { DocumentData, FirestoreError, onSnapshot, Query, SnapshotListenOptions, SnapshotOptions } from "firebase/firestore"; | ||
import { useCallback } from "react"; | ||
import { ValueHookResult } from "../common/types.js"; | ||
import { useListen, UseListenOnChange } from "../internal/useListen.js"; | ||
import { LoadingState } from "../internal/useLoadingValue.js"; | ||
import { isQueryEqual } from "./internal.js"; | ||
|
||
export type UseCollectionDataResult<Value extends DocumentData = DocumentData> = ValueHookResult<Value[], FirestoreError>; | ||
|
||
/** | ||
* Options to configure the subscription | ||
*/ | ||
export interface UseCollectionDataOptions<Value extends DocumentData = DocumentData> { | ||
snapshotListenOptions?: SnapshotListenOptions; | ||
snapshotOptions?: SnapshotOptions; | ||
initialValue?: Value[]; | ||
} | ||
|
||
/** | ||
* Returns and updates a the document data of a Firestore Query | ||
* | ||
* @template Value Type of the collection data | ||
* @param {Query<Value> | undefined | null} query Firestore query that will be subscribed to | ||
* @param {?UseCollectionDataOptions} options Options to configure the subscription | ||
* * `initialValue`: Value that is returned while the query is being fetched. | ||
* @returns {UseCollectionDataResult<Value>} Query data, loading state, and error | ||
* * value: Query data; `undefined` if query is currently being fetched, or an error occurred | ||
* * loading: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred | ||
* * error: `undefined` if no error occurred | ||
*/ | ||
export function useCollectionData<Value extends DocumentData = DocumentData>( | ||
query: Query<Value> | undefined | null, | ||
options?: UseCollectionDataOptions<Value> | ||
): UseCollectionDataResult<Value> { | ||
const { snapshotListenOptions = {}, snapshotOptions = {} } = options ?? {}; | ||
|
||
const onChange: UseListenOnChange<Value[], FirestoreError, Query<Value>> = useCallback( | ||
(stableQuery, next, error) => | ||
onSnapshot<Value>(stableQuery, snapshotListenOptions, { | ||
next: (snap) => next(snap.docs.map((doc) => doc.data(snapshotOptions))), | ||
error, | ||
}), | ||
[] | ||
); | ||
|
||
return useListen(query ?? undefined, onChange, isQueryEqual, options?.initialValue ?? LoadingState); | ||
} | ||
export { | ||
useQueryData as useCollectionData, | ||
UseQueryDataOptions as UseCollectionDataOptions, | ||
UseQueryDataResult as UseCollectionDataResult, | ||
} from "./useQueryData.js"; |
Oops, something went wrong.