@ttt-productions/query-core 0.14.21 → 0.14.23
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/README.md +6 -6
- package/dist/defaults.d.ts +0 -22
- package/dist/defaults.d.ts.map +1 -1
- package/dist/defaults.js +0 -35
- package/dist/defaults.js.map +1 -1
- package/dist/firestore/types.d.ts +1 -191
- package/dist/firestore/types.d.ts.map +1 -1
- package/dist/firestore/types.js +1 -6
- package/dist/firestore/types.js.map +1 -1
- package/dist/index.d.ts +2 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -8
- package/dist/index.js.map +1 -1
- package/dist/keys.d.ts +0 -22
- package/dist/keys.d.ts.map +1 -1
- package/dist/keys.js +0 -4
- package/dist/keys.js.map +1 -1
- package/dist/react/cache-helpers.d.ts +16 -0
- package/dist/react/cache-helpers.d.ts.map +1 -0
- package/dist/{cache-helpers.js → react/cache-helpers.js} +0 -28
- package/dist/react/cache-helpers.js.map +1 -0
- package/dist/react/defaults.d.ts +4 -0
- package/dist/react/defaults.d.ts.map +1 -0
- package/dist/react/defaults.js +17 -0
- package/dist/react/defaults.js.map +1 -0
- package/dist/react/domain-events/cache-invalidation.d.ts +33 -0
- package/dist/react/domain-events/cache-invalidation.d.ts.map +1 -0
- package/dist/react/domain-events/cache-invalidation.js +74 -0
- package/dist/react/domain-events/cache-invalidation.js.map +1 -0
- package/dist/react/domain-events/create-domain-event-invalidator.d.ts +28 -0
- package/dist/react/domain-events/create-domain-event-invalidator.d.ts.map +1 -0
- package/dist/{domain-events → react/domain-events}/create-domain-event-invalidator.js +1 -6
- package/dist/react/domain-events/create-domain-event-invalidator.js.map +1 -0
- package/dist/react/firestore/absence-scheduler.d.ts +0 -28
- package/dist/react/firestore/absence-scheduler.d.ts.map +1 -1
- package/dist/react/firestore/absence-scheduler.js +1 -39
- package/dist/react/firestore/absence-scheduler.js.map +1 -1
- package/dist/react/firestore/batch-doc-loader.d.ts +0 -30
- package/dist/react/firestore/batch-doc-loader.d.ts.map +1 -1
- package/dist/react/firestore/batch-doc-loader.js +0 -34
- package/dist/react/firestore/batch-doc-loader.js.map +1 -1
- package/dist/react/firestore/context.d.ts +0 -19
- package/dist/react/firestore/context.d.ts.map +1 -1
- package/dist/react/firestore/context.js +1 -20
- package/dist/react/firestore/context.js.map +1 -1
- package/dist/react/firestore/doc-subscription-registry.d.ts +0 -23
- package/dist/react/firestore/doc-subscription-registry.d.ts.map +1 -1
- package/dist/react/firestore/doc-subscription-registry.js +5 -26
- package/dist/react/firestore/doc-subscription-registry.js.map +1 -1
- package/dist/react/firestore/infinite-helpers.d.ts +9 -0
- package/dist/react/firestore/infinite-helpers.d.ts.map +1 -0
- package/dist/react/firestore/infinite-helpers.js +11 -0
- package/dist/react/firestore/infinite-helpers.js.map +1 -0
- package/dist/react/firestore/resubscribe.d.ts +0 -20
- package/dist/react/firestore/resubscribe.d.ts.map +1 -1
- package/dist/react/firestore/resubscribe.js +0 -20
- package/dist/react/firestore/resubscribe.js.map +1 -1
- package/dist/react/firestore/subscription-status.d.ts +0 -29
- package/dist/react/firestore/subscription-status.d.ts.map +1 -1
- package/dist/react/firestore/subscription-status.js +1 -34
- package/dist/react/firestore/subscription-status.js.map +1 -1
- package/dist/react/firestore/types.d.ts +80 -0
- package/dist/react/firestore/types.d.ts.map +1 -0
- package/dist/react/firestore/types.js +4 -0
- package/dist/react/firestore/types.js.map +1 -0
- package/dist/react/firestore/use-firestore-collection.d.ts +2 -21
- package/dist/react/firestore/use-firestore-collection.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-collection.js +23 -44
- package/dist/react/firestore/use-firestore-collection.js.map +1 -1
- package/dist/react/firestore/use-firestore-count.d.ts +1 -16
- package/dist/react/firestore/use-firestore-count.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-count.js +0 -15
- package/dist/react/firestore/use-firestore-count.js.map +1 -1
- package/dist/react/firestore/use-firestore-doc.d.ts +2 -24
- package/dist/react/firestore/use-firestore-doc.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-doc.js +21 -42
- package/dist/react/firestore/use-firestore-doc.js.map +1 -1
- package/dist/react/firestore/use-firestore-infinite.d.ts +1 -30
- package/dist/react/firestore/use-firestore-infinite.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-infinite.js +0 -32
- package/dist/react/firestore/use-firestore-infinite.js.map +1 -1
- package/dist/react/firestore/use-firestore-live-infinite.d.ts +2 -35
- package/dist/react/firestore/use-firestore-live-infinite.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-live-infinite.js +10 -36
- package/dist/react/firestore/use-firestore-live-infinite.js.map +1 -1
- package/dist/react/firestore/use-firestore-mutations.d.ts +2 -1
- package/dist/react/firestore/use-firestore-mutations.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-mutations.js +3 -3
- package/dist/react/firestore/use-firestore-mutations.js.map +1 -1
- package/dist/react/firestore/use-firestore-paginated.d.ts +2 -31
- package/dist/react/firestore/use-firestore-paginated.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-paginated.js +8 -36
- package/dist/react/firestore/use-firestore-paginated.js.map +1 -1
- package/dist/react/firestore/useBatchFirestoreDocs.d.ts +1 -123
- package/dist/react/firestore/useBatchFirestoreDocs.d.ts.map +1 -1
- package/dist/react/firestore/useBatchFirestoreDocs.js +12 -61
- package/dist/react/firestore/useBatchFirestoreDocs.js.map +1 -1
- package/dist/react/index.d.ts +13 -4
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +8 -13
- package/dist/react/index.js.map +1 -1
- package/dist/react/pending-mutations.d.ts +2 -22
- package/dist/react/pending-mutations.d.ts.map +1 -1
- package/dist/react/pending-mutations.js +7 -38
- package/dist/react/pending-mutations.js.map +1 -1
- package/dist/react/provider.d.ts +1 -6
- package/dist/react/provider.d.ts.map +1 -1
- package/dist/react/provider.js +1 -5
- package/dist/react/provider.js.map +1 -1
- package/dist/{query-client.d.ts → react/query-client.d.ts} +0 -4
- package/dist/react/query-client.d.ts.map +1 -0
- package/dist/{query-client.js → react/query-client.js} +0 -4
- package/dist/react/query-client.js.map +1 -0
- package/dist/react/search/types.d.ts +16 -0
- package/dist/react/search/types.d.ts.map +1 -0
- package/dist/react/search/types.js +2 -0
- package/dist/react/search/types.js.map +1 -0
- package/dist/react/search/use-firestore-search.d.ts +1 -30
- package/dist/react/search/use-firestore-search.d.ts.map +1 -1
- package/dist/react/search/use-firestore-search.js +3 -41
- package/dist/react/search/use-firestore-search.js.map +1 -1
- package/dist/react/use-read-retry-state.d.ts +1 -21
- package/dist/react/use-read-retry-state.d.ts.map +1 -1
- package/dist/react/use-read-retry-state.js +3 -22
- package/dist/react/use-read-retry-state.js.map +1 -1
- package/dist/search/rule.d.ts +0 -6
- package/dist/search/rule.d.ts.map +1 -1
- package/dist/search/rule.js +0 -6
- package/dist/search/rule.js.map +1 -1
- package/dist/search/types.d.ts +1 -39
- package/dist/search/types.d.ts.map +1 -1
- package/dist/search/types.js.map +1 -1
- package/package.json +2 -2
- package/dist/cache-helpers.d.ts +0 -48
- package/dist/cache-helpers.d.ts.map +0 -1
- package/dist/cache-helpers.js.map +0 -1
- package/dist/domain-events/cache-invalidation.d.ts +0 -70
- package/dist/domain-events/cache-invalidation.d.ts.map +0 -1
- package/dist/domain-events/cache-invalidation.js +0 -123
- package/dist/domain-events/cache-invalidation.js.map +0 -1
- package/dist/domain-events/create-domain-event-invalidator.d.ts +0 -46
- package/dist/domain-events/create-domain-event-invalidator.d.ts.map +0 -1
- package/dist/domain-events/create-domain-event-invalidator.js.map +0 -1
- package/dist/domain-events/index.d.ts +0 -5
- package/dist/domain-events/index.d.ts.map +0 -1
- package/dist/domain-events/index.js +0 -3
- package/dist/domain-events/index.js.map +0 -1
- package/dist/firestore/infinite-helpers.d.ts +0 -20
- package/dist/firestore/infinite-helpers.d.ts.map +0 -1
- package/dist/firestore/infinite-helpers.js +0 -23
- package/dist/firestore/infinite-helpers.js.map +0 -1
- package/dist/query-client.d.ts.map +0 -1
- package/dist/query-client.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,qBAAqB,EACrB,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAE7E,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,EAAE,IAAI,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAE3F;AAED,MAAM,WAAW,mBAAmB,CAAC,CAAC,CAAE,SAAQ,oBAAoB;IAClE,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,GAAG;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,KAAK,CAAC,CAAC;CACrD;AAED,MAAM,WAAW,0BAA0B,CAAC,CAAC,CAAE,SAAQ,oBAAoB;IACzE,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,eAAe,EAAE,CAAC;IAChC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,GAAG;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,KAAK,CAAC,CAAC;CACrD;AAED,MAAM,WAAW,wBAAwB,CAAC,CAAC,CAAE,SAAQ,oBAAoB;IACvE,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,eAAe,EAAE,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,GAAG;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,KAAK,CAAC,CAAC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AC;IACX,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,MAAM,MAAM,iBAAiB,GACzB;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,YAAY,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,GACrE;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GACvD;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/react/firestore/types.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,SAAS,CAAyB,IAA8B;IAC9E,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;AACzC,CAAC","sourcesContent":["import type {\n DocumentData,\n DocumentSnapshot,\n QueryDocumentSnapshot,\n QueryConstraint,\n} from 'firebase/firestore';\nimport type { WithId, FirestoreBaseOptions } from '../../firestore/types.js';\n\nexport function docWithId<T extends DocumentData>(snap: QueryDocumentSnapshot<T>): WithId<T> {\n return { id: snap.id, ...snap.data() };\n}\n\nexport interface FirestoreDocOptions<T> extends FirestoreBaseOptions {\n docPath: string;\n subscribe?: boolean;\n select?: (data: DocumentData & { id: string }) => T;\n}\n\nexport interface FirestoreCollectionOptions<T> extends FirestoreBaseOptions {\n collectionPath: string;\n constraints?: QueryConstraint[];\n subscribe?: boolean;\n select?: (data: DocumentData & { id: string }) => T;\n}\n\nexport interface FirestoreInfiniteOptions<T> extends FirestoreBaseOptions {\n collectionPath: string;\n constraints?: QueryConstraint[];\n pageSize?: number;\n select?: (data: DocumentData & { id: string }) => T;\n}\n\nexport interface FirestorePaginatedOptions<T> extends FirestoreBaseOptions {\n collectionPath: string;\n constraints?: QueryConstraint[];\n pageSize?: number;\n initialPage?: number;\n refetchInterval?: number;\n select?: (data: DocumentData & { id: string }) => T;\n}\n\nexport interface FirestoreCountOptions extends FirestoreBaseOptions {\n collectionPath: string;\n constraints?: QueryConstraint[];\n refetchInterval?: number;\n}\n\nexport interface FirestoreLiveInfiniteOptions<T = DocumentData & { id: string }> {\n collectionPath: string;\n queryKey: readonly unknown[];\n orderByField: string;\n constraints?: QueryConstraint[];\n pageSize?: number;\n enabled?: boolean;\n select?: (data: DocumentData & { id: string }) => T;\n sort?: 'asc' | 'desc';\n}\n\nexport interface InfinitePage<T> {\n items: WithId<T>[];\n lastDoc: DocumentSnapshot | null;\n size: number;\n}\n\nexport interface PaginatedPage<T> {\n items: T[];\n cursor: DocumentSnapshot | null;\n hasMore: boolean;\n after: string | null;\n}\n\nexport type MutationOperation =\n | { type: 'set'; docPath: string; data: DocumentData; merge?: boolean }\n | { type: 'update'; docPath: string; data: DocumentData }\n | { type: 'delete'; docPath: string };\n"]}
|
|
@@ -1,25 +1,6 @@
|
|
|
1
1
|
import { type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import { type DocumentData } from 'firebase/firestore';
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
* Fetch all documents from a Firestore collection with optional realtime updates.
|
|
6
|
-
* Use this for small collections where you need all documents at once.
|
|
7
|
-
* For large collections, use useFirestoreInfinite or useFirestorePaginated.
|
|
8
|
-
* * @example
|
|
9
|
-
* ```tsx
|
|
10
|
-
* // Fetch all channels in an entity
|
|
11
|
-
* const { data: channels } = useFirestoreCollection<Channel>({
|
|
12
|
-
* collectionPath: `entities/${entityId}/channels`,
|
|
13
|
-
* queryKey: ['channels', entityId],
|
|
14
|
-
* constraints: [orderBy('createdAt', 'asc')],
|
|
15
|
-
* });
|
|
16
|
-
* * // With realtime updates
|
|
17
|
-
* const { data: members } = useFirestoreCollection<Member>({
|
|
18
|
-
* collectionPath: `entities/${entityId}/members`,
|
|
19
|
-
* queryKey: ['members', entityId],
|
|
20
|
-
* subscribe: true,
|
|
21
|
-
* });
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
3
|
+
import type { WithId, WithSourceState } from '../../firestore/types.js';
|
|
4
|
+
import type { FirestoreCollectionOptions } from './types.js';
|
|
24
5
|
export declare function useFirestoreCollection<T extends DocumentData = DocumentData>({ collectionPath, queryKey, constraints, enabled, subscribe, staleTime, gcTime, select, }: FirestoreCollectionOptions<T>): WithSourceState<UseQueryResult<WithId<T>[], Error>>;
|
|
25
6
|
//# sourceMappingURL=use-firestore-collection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-collection.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,
|
|
1
|
+
{"version":3,"file":"use-firestore-collection.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAA0C,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAI/F,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAE7D,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,EAC5E,cAAc,EACd,QAAQ,EACR,WAAgB,EAChB,OAAc,EACd,SAAiB,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACP,EAAE,0BAA0B,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CA2GrF"}
|
|
@@ -1,49 +1,21 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { useEffect } from 'react';
|
|
3
3
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
4
|
-
import { collection, query, getDocs, onSnapshot
|
|
4
|
+
import { collection, query, getDocs, onSnapshot } from 'firebase/firestore';
|
|
5
5
|
import { useFirestoreDb } from './context.js';
|
|
6
6
|
import { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';
|
|
7
7
|
import { useSubscribedResult, useSubscriptionStatus } from './subscription-status.js';
|
|
8
|
-
/**
|
|
9
|
-
* Fetch all documents from a Firestore collection with optional realtime updates.
|
|
10
|
-
* Use this for small collections where you need all documents at once.
|
|
11
|
-
* For large collections, use useFirestoreInfinite or useFirestorePaginated.
|
|
12
|
-
* * @example
|
|
13
|
-
* ```tsx
|
|
14
|
-
* // Fetch all channels in an entity
|
|
15
|
-
* const { data: channels } = useFirestoreCollection<Channel>({
|
|
16
|
-
* collectionPath: `entities/${entityId}/channels`,
|
|
17
|
-
* queryKey: ['channels', entityId],
|
|
18
|
-
* constraints: [orderBy('createdAt', 'asc')],
|
|
19
|
-
* });
|
|
20
|
-
* * // With realtime updates
|
|
21
|
-
* const { data: members } = useFirestoreCollection<Member>({
|
|
22
|
-
* collectionPath: `entities/${entityId}/members`,
|
|
23
|
-
* queryKey: ['members', entityId],
|
|
24
|
-
* subscribe: true,
|
|
25
|
-
* });
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
8
|
export function useFirestoreCollection({ collectionPath, queryKey, constraints = [], enabled = true, subscribe = false, staleTime, gcTime, select, }) {
|
|
29
9
|
const db = useFirestoreDb();
|
|
30
10
|
const queryClient = useQueryClient();
|
|
31
|
-
// STABILIZE DEPENDENCIES:
|
|
32
|
-
// We stringify these because [where('a', '==', 'b')] !== [where('a', '==', 'b')] in JS.
|
|
33
11
|
const constraintsMemo = JSON.stringify(constraints);
|
|
34
12
|
const queryKeyMemo = JSON.stringify(queryKey);
|
|
35
|
-
// Listener errors can't surface through useQuery (the queryFn is disabled while
|
|
36
|
-
// subscribed), so they're tracked per subscription identity and merged into the result.
|
|
37
13
|
const subscription = useSubscriptionStatus(JSON.stringify([collectionPath, queryKeyMemo, constraintsMemo, enabled, subscribe]));
|
|
38
14
|
const { reset: resetStatus, update: updateStatus, restarts } = subscription;
|
|
39
15
|
useEffect(() => {
|
|
40
16
|
if (!subscribe || !enabled)
|
|
41
17
|
return;
|
|
42
18
|
resetStatus();
|
|
43
|
-
// Bounded resubscribe: a transient `permission-denied` (e.g. a failed mid-session
|
|
44
|
-
// App Check token refresh) is retried on the RESUBSCRIBE_DELAYS_MS ladder before the
|
|
45
|
-
// error is surfaced. `attempt` resets on any healthy snapshot; `disposed`/timers are
|
|
46
|
-
// cleaned up on unmount so a pending retry never fires after teardown.
|
|
47
19
|
let currentUnsubscribe = null;
|
|
48
20
|
let retryTimer = null;
|
|
49
21
|
let attempt = 0;
|
|
@@ -52,9 +24,7 @@ export function useFirestoreCollection({ collectionPath, queryKey, constraints =
|
|
|
52
24
|
if (disposed)
|
|
53
25
|
return;
|
|
54
26
|
const collectionRef = collection(db, collectionPath);
|
|
55
|
-
const q = constraints.length > 0
|
|
56
|
-
? query(collectionRef, ...constraints)
|
|
57
|
-
: collectionRef;
|
|
27
|
+
const q = constraints.length > 0 ? query(collectionRef, ...constraints) : collectionRef;
|
|
58
28
|
currentUnsubscribe = onSnapshot(q, { includeMetadataChanges: true }, (snapshot) => {
|
|
59
29
|
attempt = 0;
|
|
60
30
|
updateStatus({ error: null, restart: null });
|
|
@@ -66,11 +36,14 @@ export function useFirestoreCollection({ collectionPath, queryKey, constraints =
|
|
|
66
36
|
});
|
|
67
37
|
queryClient.setQueryData(queryKey, items);
|
|
68
38
|
const fromCache = snapshot.metadata?.fromCache ?? false;
|
|
69
|
-
updateStatus((prev) => ({
|
|
39
|
+
updateStatus((prev) => ({
|
|
40
|
+
sourceState: fromCache
|
|
41
|
+
? prev.sourceState === 'connecting'
|
|
42
|
+
? 'connecting'
|
|
43
|
+
: 'offline'
|
|
44
|
+
: 'live',
|
|
45
|
+
}));
|
|
70
46
|
}, (error) => {
|
|
71
|
-
// Heal a transient permission-denied by resubscribing on the backoff ladder,
|
|
72
|
-
// keeping the last-known data visible. Only surface the error once the ladder
|
|
73
|
-
// is exhausted; any other error surfaces immediately, as before.
|
|
74
47
|
if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {
|
|
75
48
|
currentUnsubscribe?.();
|
|
76
49
|
currentUnsubscribe = null;
|
|
@@ -92,21 +65,27 @@ export function useFirestoreCollection({ collectionPath, queryKey, constraints =
|
|
|
92
65
|
clearTimeout(retryTimer);
|
|
93
66
|
currentUnsubscribe?.();
|
|
94
67
|
};
|
|
95
|
-
// queryKey
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- queryKey and constraints are tracked by their serialized forms; an inline select must not re-subscribe every render
|
|
69
|
+
}, [
|
|
70
|
+
db,
|
|
71
|
+
collectionPath,
|
|
72
|
+
queryKeyMemo,
|
|
73
|
+
constraintsMemo,
|
|
74
|
+
enabled,
|
|
75
|
+
subscribe,
|
|
76
|
+
restarts,
|
|
77
|
+
queryClient,
|
|
78
|
+
resetStatus,
|
|
79
|
+
updateStatus,
|
|
80
|
+
]);
|
|
99
81
|
const queryResult = useQuery({
|
|
100
82
|
queryKey,
|
|
101
83
|
queryFn: async () => {
|
|
102
84
|
const collectionRef = collection(db, collectionPath);
|
|
103
|
-
const q = constraints.length > 0
|
|
104
|
-
? query(collectionRef, ...constraints)
|
|
105
|
-
: collectionRef;
|
|
85
|
+
const q = constraints.length > 0 ? query(collectionRef, ...constraints) : collectionRef;
|
|
106
86
|
const snapshot = await getDocs(q);
|
|
107
87
|
return snapshot.docs.map((docSnap) => {
|
|
108
88
|
const rawData = docSnap.data();
|
|
109
|
-
// Include id in data passed to select, so it can be renamed/transformed
|
|
110
89
|
const dataWithId = { id: docSnap.id, ...rawData };
|
|
111
90
|
const data = select ? select(dataWithId) : dataWithId;
|
|
112
91
|
return data;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-collection.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EACL,UAAU,EACV,KAAK,EACL,OAAO,EACP,UAAU,GAEX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAOtF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,sBAAsB,CAAwC,EAC5E,cAAc,EACd,QAAQ,EACR,WAAW,GAAG,EAAE,EAChB,OAAO,GAAG,IAAI,EACd,SAAS,GAAG,KAAK,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACwB;IAC9B,MAAM,EAAE,GAAG,cAAc,EAAE,CAAC;IAC5B,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IAErC,0BAA0B;IAC1B,wFAAwF;IACxF,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IACpD,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IAE9C,gFAAgF;IAChF,wFAAwF;IACxF,MAAM,YAAY,GAAG,qBAAqB,CACxC,IAAI,CAAC,SAAS,CAAC,CAAC,cAAc,E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,SAAS;QAC9B,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;QAC3C,MAAM;KACP,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;AAC5E,CAAC","sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { useQuery, useQueryClient, type UseQueryResult } from '@tanstack/react-query';\nimport {\n collection,\n query,\n getDocs,\n onSnapshot,\n type DocumentData,\n} from 'firebase/firestore';\nimport { useFirestoreDb } from './context.js';\nimport { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';\nimport { useSubscribedResult, useSubscriptionStatus } from './subscription-status.js';\nimport type {\n FirestoreCollectionOptions,\n WithId,\n WithSourceState,\n} from '../../firestore/types.js';\n\n/**\n * Fetch all documents from a Firestore collection with optional realtime updates.\n * Use this for small collections where you need all documents at once.\n * For large collections, use useFirestoreInfinite or useFirestorePaginated.\n * * @example\n * ```tsx\n * // Fetch all channels in an entity\n * const { data: channels } = useFirestoreCollection<Channel>({\n * collectionPath: `entities/${entityId}/channels`,\n * queryKey: ['channels', entityId],\n * constraints: [orderBy('createdAt', 'asc')],\n * });\n * * // With realtime updates\n * const { data: members } = useFirestoreCollection<Member>({\n * collectionPath: `entities/${entityId}/members`,\n * queryKey: ['members', entityId],\n * subscribe: true,\n * });\n * ```\n */\nexport function useFirestoreCollection<T extends DocumentData = DocumentData>({\n collectionPath,\n queryKey,\n constraints = [],\n enabled = true,\n subscribe = false,\n staleTime,\n gcTime,\n select,\n}: FirestoreCollectionOptions<T>): WithSourceState<UseQueryResult<WithId<T>[], Error>> {\n const db = useFirestoreDb();\n const queryClient = useQueryClient();\n\n // STABILIZE DEPENDENCIES:\n // We stringify these because [where('a', '==', 'b')] !== [where('a', '==', 'b')] in JS.\n const constraintsMemo = JSON.stringify(constraints);\n const queryKeyMemo = JSON.stringify(queryKey);\n\n // Listener errors can't surface through useQuery (the queryFn is disabled while\n // subscribed), so they're tracked per subscription identity and merged into the result.\n const subscription = useSubscriptionStatus(\n JSON.stringify([collectionPath, queryKeyMemo, constraintsMemo, enabled, subscribe]),\n );\n const { reset: resetStatus, update: updateStatus, restarts } = subscription;\n\n useEffect(() => {\n if (!subscribe || !enabled) return;\n\n resetStatus();\n\n // Bounded resubscribe: a transient `permission-denied` (e.g. a failed mid-session\n // App Check token refresh) is retried on the RESUBSCRIBE_DELAYS_MS ladder before the\n // error is surfaced. `attempt` resets on any healthy snapshot; `disposed`/timers are\n // cleaned up on unmount so a pending retry never fires after teardown.\n let currentUnsubscribe: (() => void) | null = null;\n let retryTimer: ReturnType<typeof setTimeout> | null = null;\n let attempt = 0;\n let disposed = false;\n\n const start = () => {\n if (disposed) return;\n const collectionRef = collection(db, collectionPath);\n const q = constraints.length > 0\n ? query(collectionRef, ...constraints)\n : collectionRef;\n\n currentUnsubscribe = onSnapshot(\n q,\n { includeMetadataChanges: true },\n (snapshot) => {\n attempt = 0;\n updateStatus({ error: null, restart: null });\n const items = snapshot.docs.map((docSnap) => {\n const rawData = docSnap.data();\n const dataWithId = { id: docSnap.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n });\n queryClient.setQueryData(queryKey, items);\n const fromCache = snapshot.metadata?.fromCache ?? false;\n updateStatus((prev) => ({ sourceState: fromCache ? (prev.sourceState === 'connecting' ? 'connecting' : 'offline') : 'live' }));\n },\n (error) => {\n // Heal a transient permission-denied by resubscribing on the backoff ladder,\n // keeping the last-known data visible. Only surface the error once the ladder\n // is exhausted; any other error surfaces immediately, as before.\n if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {\n currentUnsubscribe?.();\n currentUnsubscribe = null;\n const delay = RESUBSCRIBE_DELAYS_MS[attempt];\n attempt += 1;\n updateStatus({ sourceState: 'connecting' });\n retryTimer = setTimeout(start, delay);\n return;\n }\n console.error('[useFirestoreCollection] Subscription error:', error);\n updateStatus({ error, sourceState: 'error', restart: null });\n queryClient.setQueryData(queryKey, undefined);\n }\n );\n };\n\n start();\n\n return () => {\n disposed = true;\n if (retryTimer) clearTimeout(retryTimer);\n currentUnsubscribe?.();\n };\n // queryKey & constraints are tracked via their stringified forms above; select is intentionally\n // excluded so an inline caller function doesn't force a re-subscribe on every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [db, collectionPath, queryKeyMemo, constraintsMemo, enabled, subscribe, restarts, queryClient, resetStatus, updateStatus]);\n\n const queryResult = useQuery({\n queryKey,\n queryFn: async (): Promise<WithId<T>[]> => {\n const collectionRef = collection(db, collectionPath);\n const q = constraints.length > 0\n ? query(collectionRef, ...constraints)\n : collectionRef;\n\n const snapshot = await getDocs(q);\n return snapshot.docs.map((docSnap) => {\n const rawData = docSnap.data();\n // Include id in data passed to select, so it can be renamed/transformed\n const dataWithId = { id: docSnap.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n });\n },\n enabled: enabled && !subscribe,\n staleTime: subscribe ? Infinity : staleTime,\n gcTime,\n });\n\n return useSubscribedResult(queryResult, subscribe, enabled, subscription);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"use-firestore-collection.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAqB,MAAM,oBAAoB,CAAC;AAC/F,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CA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/B,MAAM,UAAU,GAAG,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,OAAO,EAAE,CAAC;gBAClD,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;gBACtD,OAAO,IAAiB,CAAC;YAC3B,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,EAAE,OAAO,IAAI,CAAC,SAAS;QAC9B,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;QAC3C,MAAM;KACP,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;AAC5E,CAAC","sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { useQuery, useQueryClient, type UseQueryResult } from '@tanstack/react-query';\nimport { collection, query, getDocs, onSnapshot, type DocumentData } from 'firebase/firestore';\nimport { useFirestoreDb } from './context.js';\nimport { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';\nimport { useSubscribedResult, useSubscriptionStatus } from './subscription-status.js';\nimport type { WithId, WithSourceState } from '../../firestore/types.js';\nimport type { FirestoreCollectionOptions } from './types.js';\n\nexport function useFirestoreCollection<T extends DocumentData = DocumentData>({\n collectionPath,\n queryKey,\n constraints = [],\n enabled = true,\n subscribe = false,\n staleTime,\n gcTime,\n select,\n}: FirestoreCollectionOptions<T>): WithSourceState<UseQueryResult<WithId<T>[], Error>> {\n const db = useFirestoreDb();\n const queryClient = useQueryClient();\n\n const constraintsMemo = JSON.stringify(constraints);\n const queryKeyMemo = JSON.stringify(queryKey);\n\n const subscription = useSubscriptionStatus(\n JSON.stringify([collectionPath, queryKeyMemo, constraintsMemo, enabled, subscribe]),\n );\n const { reset: resetStatus, update: updateStatus, restarts } = subscription;\n\n useEffect(() => {\n if (!subscribe || !enabled) return;\n\n resetStatus();\n\n let currentUnsubscribe: (() => void) | null = null;\n let retryTimer: ReturnType<typeof setTimeout> | null = null;\n let attempt = 0;\n let disposed = false;\n\n const start = () => {\n if (disposed) return;\n const collectionRef = collection(db, collectionPath);\n const q = constraints.length > 0 ? query(collectionRef, ...constraints) : collectionRef;\n\n currentUnsubscribe = onSnapshot(\n q,\n { includeMetadataChanges: true },\n (snapshot) => {\n attempt = 0;\n updateStatus({ error: null, restart: null });\n const items = snapshot.docs.map((docSnap) => {\n const rawData = docSnap.data();\n const dataWithId = { id: docSnap.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n });\n queryClient.setQueryData(queryKey, items);\n const fromCache = snapshot.metadata?.fromCache ?? false;\n updateStatus((prev) => ({\n sourceState: fromCache\n ? prev.sourceState === 'connecting'\n ? 'connecting'\n : 'offline'\n : 'live',\n }));\n },\n (error) => {\n if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {\n currentUnsubscribe?.();\n currentUnsubscribe = null;\n const delay = RESUBSCRIBE_DELAYS_MS[attempt];\n attempt += 1;\n updateStatus({ sourceState: 'connecting' });\n retryTimer = setTimeout(start, delay);\n return;\n }\n console.error('[useFirestoreCollection] Subscription error:', error);\n updateStatus({ error, sourceState: 'error', restart: null });\n queryClient.setQueryData(queryKey, undefined);\n },\n );\n };\n\n start();\n\n return () => {\n disposed = true;\n if (retryTimer) clearTimeout(retryTimer);\n currentUnsubscribe?.();\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps -- queryKey and constraints are tracked by their serialized forms; an inline select must not re-subscribe every render\n }, [\n db,\n collectionPath,\n queryKeyMemo,\n constraintsMemo,\n enabled,\n subscribe,\n restarts,\n queryClient,\n resetStatus,\n updateStatus,\n ]);\n\n const queryResult = useQuery({\n queryKey,\n queryFn: async (): Promise<WithId<T>[]> => {\n const collectionRef = collection(db, collectionPath);\n const q = constraints.length > 0 ? query(collectionRef, ...constraints) : collectionRef;\n\n const snapshot = await getDocs(q);\n return snapshot.docs.map((docSnap) => {\n const rawData = docSnap.data();\n const dataWithId = { id: docSnap.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n });\n },\n enabled: enabled && !subscribe,\n staleTime: subscribe ? Infinity : staleTime,\n gcTime,\n });\n\n return useSubscribedResult(queryResult, subscribe, enabled, subscription);\n}\n"]}
|
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
import { type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
-
import type { FirestoreCountOptions } from '
|
|
3
|
-
/**
|
|
4
|
-
* Count documents in a Firestore collection via a server-side `count()`
|
|
5
|
-
* aggregation (cheap — it does not read the matching documents). Optional
|
|
6
|
-
* polling via `refetchInterval`.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```tsx
|
|
10
|
-
* const { data: count } = useFirestoreCount({
|
|
11
|
-
* collectionPath: 'activeUserNotifications',
|
|
12
|
-
* queryKey: ['notifications', 'unread-count', userId],
|
|
13
|
-
* constraints: [where('targetUserId', '==', userId), where('seenAt', '==', 0)],
|
|
14
|
-
* refetchInterval: 30_000,
|
|
15
|
-
* });
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
2
|
+
import type { FirestoreCountOptions } from './types.js';
|
|
18
3
|
export declare function useFirestoreCount({ collectionPath, queryKey, constraints, enabled, staleTime, gcTime, refetchInterval, }: FirestoreCountOptions): UseQueryResult<number, Error>;
|
|
19
4
|
//# sourceMappingURL=use-firestore-count.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-count.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-count.ts"],"names":[],"mappings":"AAEA,OAAO,EAAY,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAGtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"use-firestore-count.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-count.ts"],"names":[],"mappings":"AAEA,OAAO,EAAY,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAGtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAExD,wBAAgB,iBAAiB,CAAC,EAChC,cAAc,EACd,QAAQ,EACR,WAAgB,EAChB,OAAc,EACd,SAAS,EACT,MAAM,EACN,eAAe,GAChB,EAAE,qBAAqB,GAAG,cAAc,CAAC,MAAM,EAAE,KAAK,CAAC,CAgBvD"}
|
|
@@ -2,21 +2,6 @@
|
|
|
2
2
|
import { useQuery } from '@tanstack/react-query';
|
|
3
3
|
import { collection, query, getCountFromServer } from 'firebase/firestore';
|
|
4
4
|
import { useFirestoreDb } from './context.js';
|
|
5
|
-
/**
|
|
6
|
-
* Count documents in a Firestore collection via a server-side `count()`
|
|
7
|
-
* aggregation (cheap — it does not read the matching documents). Optional
|
|
8
|
-
* polling via `refetchInterval`.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```tsx
|
|
12
|
-
* const { data: count } = useFirestoreCount({
|
|
13
|
-
* collectionPath: 'activeUserNotifications',
|
|
14
|
-
* queryKey: ['notifications', 'unread-count', userId],
|
|
15
|
-
* constraints: [where('targetUserId', '==', userId), where('seenAt', '==', 0)],
|
|
16
|
-
* refetchInterval: 30_000,
|
|
17
|
-
* });
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
5
|
export function useFirestoreCount({ collectionPath, queryKey, constraints = [], enabled = true, staleTime, gcTime, refetchInterval, }) {
|
|
21
6
|
const db = useFirestoreDb();
|
|
22
7
|
return useQuery({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-count.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-count.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,QAAQ,EAAuB,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG9C
|
|
1
|
+
{"version":3,"file":"use-firestore-count.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-count.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,QAAQ,EAAuB,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG9C,MAAM,UAAU,iBAAiB,CAAC,EAChC,cAAc,EACd,QAAQ,EACR,WAAW,GAAG,EAAE,EAChB,OAAO,GAAG,IAAI,EACd,SAAS,EACT,MAAM,EACN,eAAe,GACO;IACtB,MAAM,EAAE,GAAG,cAAc,EAAE,CAAC;IAE5B,OAAO,QAAQ,CAAC;QACd,QAAQ;QACR,OAAO,EAAE,KAAK,IAAqB,EAAE;YACnC,MAAM,aAAa,GAAG,UAAU,CAAC,EAAE,EAAE,cAAc,CAAC,CAAC;YACrD,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,aAAa,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC;YACxF,MAAM,QAAQ,GAAG,MAAM,kBAAkB,CAAC,CAAC,CAAC,CAAC;YAC7C,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;QAC/B,CAAC;QACD,OAAO;QACP,SAAS;QACT,MAAM;QACN,eAAe;KAChB,CAAC,CAAC;AACL,CAAC","sourcesContent":["'use client';\n\nimport { useQuery, type UseQueryResult } from '@tanstack/react-query';\nimport { collection, query, getCountFromServer } from 'firebase/firestore';\nimport { useFirestoreDb } from './context.js';\nimport type { FirestoreCountOptions } from './types.js';\n\nexport function useFirestoreCount({\n collectionPath,\n queryKey,\n constraints = [],\n enabled = true,\n staleTime,\n gcTime,\n refetchInterval,\n}: FirestoreCountOptions): UseQueryResult<number, Error> {\n const db = useFirestoreDb();\n\n return useQuery({\n queryKey,\n queryFn: async (): Promise<number> => {\n const collectionRef = collection(db, collectionPath);\n const q = constraints.length > 0 ? query(collectionRef, ...constraints) : collectionRef;\n const snapshot = await getCountFromServer(q);\n return snapshot.data().count;\n },\n enabled,\n staleTime,\n gcTime,\n refetchInterval,\n });\n}\n"]}
|
|
@@ -1,28 +1,6 @@
|
|
|
1
1
|
import { type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import { type DocumentData } from 'firebase/firestore';
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
* Fetch a single Firestore document with optional realtime updates.
|
|
6
|
-
* * @example
|
|
7
|
-
* ```tsx
|
|
8
|
-
* // Simple fetch
|
|
9
|
-
* const { data: user } = useFirestoreDoc<User>({
|
|
10
|
-
* docPath: `users/${userId}`,
|
|
11
|
-
* queryKey: ['user', userId],
|
|
12
|
-
* });
|
|
13
|
-
* * // With realtime updates
|
|
14
|
-
* const { data: entity } = useFirestoreDoc<Entity>({
|
|
15
|
-
* docPath: `entities/${entityId}`,
|
|
16
|
-
* queryKey: ['entity', entityId],
|
|
17
|
-
* subscribe: true,
|
|
18
|
-
* });
|
|
19
|
-
* * // Conditional fetch
|
|
20
|
-
* const { data: profile } = useFirestoreDoc<Profile>({
|
|
21
|
-
* docPath: `profiles/${userId}`,
|
|
22
|
-
* queryKey: ['profile', userId],
|
|
23
|
-
* enabled: !!userId,
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
3
|
+
import type { WithId, WithSourceState } from '../../firestore/types.js';
|
|
4
|
+
import type { FirestoreDocOptions } from './types.js';
|
|
27
5
|
export declare function useFirestoreDoc<T extends DocumentData = DocumentData>({ docPath, queryKey, enabled, subscribe, staleTime, gcTime, select, }: FirestoreDocOptions<T>): WithSourceState<UseQueryResult<WithId<T> | null, Error>>;
|
|
28
6
|
//# sourceMappingURL=use-firestore-doc.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-doc.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAIhF,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"use-firestore-doc.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAIhF,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtD,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,EACrE,OAAO,EACP,QAAQ,EACR,OAAc,EACd,SAAiB,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACP,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAwGnF"}
|
|
@@ -5,46 +5,16 @@ import { doc, getDoc, onSnapshot } from 'firebase/firestore';
|
|
|
5
5
|
import { useFirestoreDb } from './context.js';
|
|
6
6
|
import { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';
|
|
7
7
|
import { useSubscribedResult, useSubscriptionStatus } from './subscription-status.js';
|
|
8
|
-
/**
|
|
9
|
-
* Fetch a single Firestore document with optional realtime updates.
|
|
10
|
-
* * @example
|
|
11
|
-
* ```tsx
|
|
12
|
-
* // Simple fetch
|
|
13
|
-
* const { data: user } = useFirestoreDoc<User>({
|
|
14
|
-
* docPath: `users/${userId}`,
|
|
15
|
-
* queryKey: ['user', userId],
|
|
16
|
-
* });
|
|
17
|
-
* * // With realtime updates
|
|
18
|
-
* const { data: entity } = useFirestoreDoc<Entity>({
|
|
19
|
-
* docPath: `entities/${entityId}`,
|
|
20
|
-
* queryKey: ['entity', entityId],
|
|
21
|
-
* subscribe: true,
|
|
22
|
-
* });
|
|
23
|
-
* * // Conditional fetch
|
|
24
|
-
* const { data: profile } = useFirestoreDoc<Profile>({
|
|
25
|
-
* docPath: `profiles/${userId}`,
|
|
26
|
-
* queryKey: ['profile', userId],
|
|
27
|
-
* enabled: !!userId,
|
|
28
|
-
* });
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
8
|
export function useFirestoreDoc({ docPath, queryKey, enabled = true, subscribe = false, staleTime, gcTime, select, }) {
|
|
32
9
|
const db = useFirestoreDb();
|
|
33
10
|
const queryClient = useQueryClient();
|
|
34
11
|
const queryKeyMemo = JSON.stringify(queryKey);
|
|
35
|
-
// Listener errors can't surface through useQuery (the queryFn is disabled while
|
|
36
|
-
// subscribed), so they're tracked per subscription identity and merged into the result.
|
|
37
12
|
const subscription = useSubscriptionStatus(JSON.stringify([docPath, queryKeyMemo, enabled, subscribe]));
|
|
38
13
|
const { reset: resetStatus, update: updateStatus, restarts } = subscription;
|
|
39
|
-
// Set up realtime subscription
|
|
40
14
|
useEffect(() => {
|
|
41
15
|
if (!subscribe || !enabled)
|
|
42
16
|
return;
|
|
43
17
|
resetStatus();
|
|
44
|
-
// Bounded resubscribe: a transient `permission-denied` (e.g. a failed mid-session
|
|
45
|
-
// App Check token refresh) is retried on the RESUBSCRIBE_DELAYS_MS ladder before the
|
|
46
|
-
// error is surfaced. `attempt` resets on any healthy snapshot; `disposed`/timers are
|
|
47
|
-
// cleaned up on unmount so a pending retry never fires after teardown.
|
|
48
18
|
let currentUnsubscribe = null;
|
|
49
19
|
let retryTimer = null;
|
|
50
20
|
let attempt = 0;
|
|
@@ -58,7 +28,6 @@ export function useFirestoreDoc({ docPath, queryKey, enabled = true, subscribe =
|
|
|
58
28
|
updateStatus({ error: null, restart: null });
|
|
59
29
|
if (snapshot.exists()) {
|
|
60
30
|
const rawData = snapshot.data();
|
|
61
|
-
// Include id in data passed to select, so it can be renamed/transformed
|
|
62
31
|
const dataWithId = { id: snapshot.id, ...rawData };
|
|
63
32
|
const data = select ? select(dataWithId) : dataWithId;
|
|
64
33
|
queryClient.setQueryData(queryKey, data);
|
|
@@ -67,11 +36,14 @@ export function useFirestoreDoc({ docPath, queryKey, enabled = true, subscribe =
|
|
|
67
36
|
queryClient.setQueryData(queryKey, null);
|
|
68
37
|
}
|
|
69
38
|
const fromCache = snapshot.metadata?.fromCache ?? false;
|
|
70
|
-
updateStatus((prev) => ({
|
|
39
|
+
updateStatus((prev) => ({
|
|
40
|
+
sourceState: fromCache
|
|
41
|
+
? prev.sourceState === 'connecting'
|
|
42
|
+
? 'connecting'
|
|
43
|
+
: 'offline'
|
|
44
|
+
: 'live',
|
|
45
|
+
}));
|
|
71
46
|
}, (error) => {
|
|
72
|
-
// Heal a transient permission-denied by resubscribing on the backoff ladder,
|
|
73
|
-
// keeping the last-known data visible. Only surface the error once the ladder
|
|
74
|
-
// is exhausted; any other error surfaces immediately, as before.
|
|
75
47
|
if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {
|
|
76
48
|
currentUnsubscribe?.();
|
|
77
49
|
currentUnsubscribe = null;
|
|
@@ -93,10 +65,18 @@ export function useFirestoreDoc({ docPath, queryKey, enabled = true, subscribe =
|
|
|
93
65
|
clearTimeout(retryTimer);
|
|
94
66
|
currentUnsubscribe?.();
|
|
95
67
|
};
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- queryKey is tracked by queryKeyMemo; an inline select must not re-subscribe every render
|
|
69
|
+
}, [
|
|
70
|
+
db,
|
|
71
|
+
docPath,
|
|
72
|
+
queryKeyMemo,
|
|
73
|
+
enabled,
|
|
74
|
+
subscribe,
|
|
75
|
+
restarts,
|
|
76
|
+
queryClient,
|
|
77
|
+
resetStatus,
|
|
78
|
+
updateStatus,
|
|
79
|
+
]);
|
|
100
80
|
const query = useQuery({
|
|
101
81
|
queryKey,
|
|
102
82
|
queryFn: async () => {
|
|
@@ -106,13 +86,12 @@ export function useFirestoreDoc({ docPath, queryKey, enabled = true, subscribe =
|
|
|
106
86
|
return null;
|
|
107
87
|
}
|
|
108
88
|
const rawData = snapshot.data();
|
|
109
|
-
// Include id in data passed to select, so it can be renamed/transformed
|
|
110
89
|
const dataWithId = { id: snapshot.id, ...rawData };
|
|
111
90
|
const data = select ? select(dataWithId) : dataWithId;
|
|
112
91
|
return data;
|
|
113
92
|
},
|
|
114
|
-
enabled: enabled && !subscribe,
|
|
115
|
-
staleTime: subscribe ? Infinity : staleTime,
|
|
93
|
+
enabled: enabled && !subscribe,
|
|
94
|
+
staleTime: subscribe ? Infinity : staleTime,
|
|
116
95
|
gcTime,
|
|
117
96
|
});
|
|
118
97
|
return useSubscribedResult(query, subscribe, enabled, subscription);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-doc.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAqB,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAClF,OA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C,MAAM,CAAC,CAAC;YAEtC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;gBACvB,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC;YAChC,MAAM,UAAU,GAAG,EAAE,EAAE,EAAE,QAAQ,CAAC,EAAE,EAAE,GAAG,OAAO,EAAE,CAAC;YACnD,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;YACtD,OAAO,IAAiB,CAAC;QAC3B,CAAC;QACD,OAAO,EAAE,OAAO,IAAI,CAAC,SAAS;QAC9B,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;QAC3C,MAAM;KACP,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;AACtE,CAAC","sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { useQuery, useQueryClient, type UseQueryResult } from '@tanstack/react-query';\nimport { doc, getDoc, onSnapshot, type DocumentData } from 'firebase/firestore';\nimport { useFirestoreDb } from './context.js';\nimport { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';\nimport { useSubscribedResult, useSubscriptionStatus } from './subscription-status.js';\nimport type { WithId, WithSourceState } from '../../firestore/types.js';\nimport type { FirestoreDocOptions } from './types.js';\n\nexport function useFirestoreDoc<T extends DocumentData = DocumentData>({\n docPath,\n queryKey,\n enabled = true,\n subscribe = false,\n staleTime,\n gcTime,\n select,\n}: FirestoreDocOptions<T>): WithSourceState<UseQueryResult<WithId<T> | null, Error>> {\n const db = useFirestoreDb();\n const queryClient = useQueryClient();\n const queryKeyMemo = JSON.stringify(queryKey);\n\n const subscription = useSubscriptionStatus(\n JSON.stringify([docPath, queryKeyMemo, enabled, subscribe]),\n );\n const { reset: resetStatus, update: updateStatus, restarts } = subscription;\n\n useEffect(() => {\n if (!subscribe || !enabled) return;\n\n resetStatus();\n\n let currentUnsubscribe: (() => void) | null = null;\n let retryTimer: ReturnType<typeof setTimeout> | null = null;\n let attempt = 0;\n let disposed = false;\n\n const start = () => {\n if (disposed) return;\n const docRef = doc(db, docPath);\n currentUnsubscribe = onSnapshot(\n docRef,\n { includeMetadataChanges: true },\n (snapshot) => {\n attempt = 0;\n updateStatus({ error: null, restart: null });\n if (snapshot.exists()) {\n const rawData = snapshot.data();\n const dataWithId = { id: snapshot.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n queryClient.setQueryData(queryKey, data as WithId<T>);\n } else {\n queryClient.setQueryData(queryKey, null);\n }\n const fromCache = snapshot.metadata?.fromCache ?? false;\n updateStatus((prev) => ({\n sourceState: fromCache\n ? prev.sourceState === 'connecting'\n ? 'connecting'\n : 'offline'\n : 'live',\n }));\n },\n (error) => {\n if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {\n currentUnsubscribe?.();\n currentUnsubscribe = null;\n const delay = RESUBSCRIBE_DELAYS_MS[attempt];\n attempt += 1;\n updateStatus({ sourceState: 'connecting' });\n retryTimer = setTimeout(start, delay);\n return;\n }\n console.error('[useFirestoreDoc] Subscription error:', error);\n updateStatus({ error, sourceState: 'error', restart: null });\n queryClient.setQueryData(queryKey, undefined);\n },\n );\n };\n\n start();\n\n return () => {\n disposed = true;\n if (retryTimer) clearTimeout(retryTimer);\n currentUnsubscribe?.();\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps -- queryKey is tracked by queryKeyMemo; an inline select must not re-subscribe every render\n }, [\n db,\n docPath,\n queryKeyMemo,\n enabled,\n subscribe,\n restarts,\n queryClient,\n resetStatus,\n updateStatus,\n ]);\n\n const query = useQuery({\n queryKey,\n queryFn: async (): Promise<WithId<T> | null> => {\n const docRef = doc(db, docPath);\n const snapshot = await getDoc(docRef);\n\n if (!snapshot.exists()) {\n return null;\n }\n\n const rawData = snapshot.data();\n const dataWithId = { id: snapshot.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n },\n enabled: enabled && !subscribe,\n staleTime: subscribe ? Infinity : staleTime,\n gcTime,\n });\n\n return useSubscribedResult(query, subscribe, enabled, subscription);\n}\n"]}
|
|
@@ -1,35 +1,6 @@
|
|
|
1
1
|
import { type UseInfiniteQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import { type DocumentData, type DocumentSnapshot } from 'firebase/firestore';
|
|
3
|
-
import type { FirestoreInfiniteOptions, InfinitePage } from '
|
|
4
|
-
/**
|
|
5
|
-
* Infinite scroll pagination for Firestore collections.
|
|
6
|
-
* Automatically handles cursor-based pagination with React Query.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```tsx
|
|
10
|
-
* const {
|
|
11
|
-
* data,
|
|
12
|
-
* fetchNextPage,
|
|
13
|
-
* hasNextPage,
|
|
14
|
-
* isFetchingNextPage,
|
|
15
|
-
* isLoading,
|
|
16
|
-
* } = useFirestoreInfinite<Post>({
|
|
17
|
-
* collectionPath: 'posts',
|
|
18
|
-
* queryKey: ['posts', 'feed'],
|
|
19
|
-
* constraints: [
|
|
20
|
-
* where('status', '==', 'published'),
|
|
21
|
-
* orderBy('createdAt', 'desc'),
|
|
22
|
-
* ],
|
|
23
|
-
* pageSize: 20,
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* // Flatten pages for rendering
|
|
27
|
-
* const posts = data?.pages.flatMap(page => page.items) ?? [];
|
|
28
|
-
*
|
|
29
|
-
* // Load more on scroll
|
|
30
|
-
* <InfiniteScroll onLoadMore={fetchNextPage} hasMore={hasNextPage} />
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
3
|
+
import type { FirestoreInfiniteOptions, InfinitePage } from './types.js';
|
|
33
4
|
export declare function useFirestoreInfinite<T extends DocumentData = DocumentData>({ collectionPath, queryKey, constraints, pageSize, enabled, staleTime, gcTime, select, }: FirestoreInfiniteOptions<T>): UseInfiniteQueryResult<{
|
|
34
5
|
pages: InfinitePage<T>[];
|
|
35
6
|
pageParams: (DocumentSnapshot | null)[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-infinite.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-infinite.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,KAAK,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAML,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACtB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-firestore-infinite.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-infinite.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,KAAK,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAML,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACtB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,wBAAwB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAIzE,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,EAC1E,cAAc,EACd,QAAQ,EACR,WAAgB,EAChB,QAA4B,EAC5B,OAAc,EACd,SAAS,EACT,MAAM,EACN,MAAM,GACP,EAAE,wBAAwB,CAAC,CAAC,CAAC,GAAG,sBAAsB,CACrD;IAAE,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC;IAAC,UAAU,EAAE,CAAC,gBAAgB,GAAG,IAAI,CAAC,EAAE,CAAA;CAAE,EACrE,KAAK,CACN,CAuCA"}
|
|
@@ -3,42 +3,12 @@ import { useInfiniteQuery } from '@tanstack/react-query';
|
|
|
3
3
|
import { collection, query, getDocs, limit, startAfter, } from 'firebase/firestore';
|
|
4
4
|
import { useFirestoreDb } from './context.js';
|
|
5
5
|
const DEFAULT_PAGE_SIZE = 20;
|
|
6
|
-
/**
|
|
7
|
-
* Infinite scroll pagination for Firestore collections.
|
|
8
|
-
* Automatically handles cursor-based pagination with React Query.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```tsx
|
|
12
|
-
* const {
|
|
13
|
-
* data,
|
|
14
|
-
* fetchNextPage,
|
|
15
|
-
* hasNextPage,
|
|
16
|
-
* isFetchingNextPage,
|
|
17
|
-
* isLoading,
|
|
18
|
-
* } = useFirestoreInfinite<Post>({
|
|
19
|
-
* collectionPath: 'posts',
|
|
20
|
-
* queryKey: ['posts', 'feed'],
|
|
21
|
-
* constraints: [
|
|
22
|
-
* where('status', '==', 'published'),
|
|
23
|
-
* orderBy('createdAt', 'desc'),
|
|
24
|
-
* ],
|
|
25
|
-
* pageSize: 20,
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* // Flatten pages for rendering
|
|
29
|
-
* const posts = data?.pages.flatMap(page => page.items) ?? [];
|
|
30
|
-
*
|
|
31
|
-
* // Load more on scroll
|
|
32
|
-
* <InfiniteScroll onLoadMore={fetchNextPage} hasMore={hasNextPage} />
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
6
|
export function useFirestoreInfinite({ collectionPath, queryKey, constraints = [], pageSize = DEFAULT_PAGE_SIZE, enabled = true, staleTime, gcTime, select, }) {
|
|
36
7
|
const db = useFirestoreDb();
|
|
37
8
|
return useInfiniteQuery({
|
|
38
9
|
queryKey,
|
|
39
10
|
queryFn: async ({ pageParam }) => {
|
|
40
11
|
const collectionRef = collection(db, collectionPath);
|
|
41
|
-
// Build query with constraints
|
|
42
12
|
const queryConstraints = [
|
|
43
13
|
...constraints,
|
|
44
14
|
...(pageParam ? [startAfter(pageParam)] : []),
|
|
@@ -48,7 +18,6 @@ export function useFirestoreInfinite({ collectionPath, queryKey, constraints = [
|
|
|
48
18
|
const snapshot = await getDocs(q);
|
|
49
19
|
const items = snapshot.docs.map((docSnap) => {
|
|
50
20
|
const rawData = docSnap.data();
|
|
51
|
-
// Include id in data passed to select, so it can be renamed/transformed
|
|
52
21
|
const dataWithId = { id: docSnap.id, ...rawData };
|
|
53
22
|
const data = select ? select(dataWithId) : dataWithId;
|
|
54
23
|
return data;
|
|
@@ -61,7 +30,6 @@ export function useFirestoreInfinite({ collectionPath, queryKey, constraints = [
|
|
|
61
30
|
},
|
|
62
31
|
initialPageParam: null,
|
|
63
32
|
getNextPageParam: (lastPage) => {
|
|
64
|
-
// If we got fewer items than requested, there are no more pages
|
|
65
33
|
if (lastPage.size < pageSize)
|
|
66
34
|
return undefined;
|
|
67
35
|
return lastPage.lastDoc;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-infinite.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-infinite.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,gBAAgB,EAA+B,MAAM,uBAAuB,CAAC;AACtF,OAAO,EACL,UAAU,EACV,KAAK,EACL,OAAO,EACP,KAAK,EACL,UAAU,GAGX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"use-firestore-infinite.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-infinite.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,gBAAgB,EAA+B,MAAM,uBAAuB,CAAC;AACtF,OAAO,EACL,UAAU,EACV,KAAK,EACL,OAAO,EACP,KAAK,EACL,UAAU,GAGX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAI9C,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,MAAM,UAAU,oBAAoB,CAAwC,EAC1E,cAAc,EACd,QAAQ,EACR,WAAW,GAAG,EAAE,EAChB,QAAQ,GAAG,iBAAiB,EAC5B,OAAO,GAAG,IAAI,EACd,SAAS,EACT,MA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;YAC7B,IAAI,QAAQ,CAAC,IAAI,GAAG,QAAQ;gBAAE,OAAO,SAAS,CAAC;YAC/C,OAAO,QAAQ,CAAC,OAAO,CAAC;QAC1B,CAAC;QACD,OAAO;QACP,SAAS;QACT,MAAM;KACP,CAAC,CAAC;AACL,CAAC","sourcesContent":["'use client';\n\nimport { useInfiniteQuery, type UseInfiniteQueryResult } from '@tanstack/react-query';\nimport {\n collection,\n query,\n getDocs,\n limit,\n startAfter,\n type DocumentData,\n type DocumentSnapshot,\n} from 'firebase/firestore';\nimport { useFirestoreDb } from './context.js';\nimport type { WithId } from '../../firestore/types.js';\nimport type { FirestoreInfiniteOptions, InfinitePage } from './types.js';\n\nconst DEFAULT_PAGE_SIZE = 20;\n\nexport function useFirestoreInfinite<T extends DocumentData = DocumentData>({\n collectionPath,\n queryKey,\n constraints = [],\n pageSize = DEFAULT_PAGE_SIZE,\n enabled = true,\n staleTime,\n gcTime,\n select,\n}: FirestoreInfiniteOptions<T>): UseInfiniteQueryResult<\n { pages: InfinitePage<T>[]; pageParams: (DocumentSnapshot | null)[] },\n Error\n> {\n const db = useFirestoreDb();\n\n return useInfiniteQuery({\n queryKey,\n queryFn: async ({ pageParam }): Promise<InfinitePage<T>> => {\n const collectionRef = collection(db, collectionPath);\n\n const queryConstraints = [\n ...constraints,\n ...(pageParam ? [startAfter(pageParam as DocumentSnapshot)] : []),\n limit(pageSize),\n ];\n\n const q = query(collectionRef, ...queryConstraints);\n const snapshot = await getDocs(q);\n\n const items = snapshot.docs.map((docSnap) => {\n const rawData = docSnap.data();\n const dataWithId = { id: docSnap.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n });\n\n return {\n items,\n lastDoc: snapshot.docs[snapshot.docs.length - 1] ?? null,\n size: snapshot.docs.length,\n };\n },\n initialPageParam: null as DocumentSnapshot | null,\n getNextPageParam: (lastPage) => {\n if (lastPage.size < pageSize) return undefined;\n return lastPage.lastDoc;\n },\n enabled,\n staleTime,\n gcTime,\n });\n}\n"]}
|