forked from CSFrequency/react-firebase-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDocument.ts
159 lines (148 loc) · 3.92 KB
/
useDocument.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
DocumentData,
DocumentReference,
DocumentSnapshot,
FirestoreError,
getDoc,
getDocFromCache,
getDocFromServer,
onSnapshot,
} from 'firebase/firestore';
import { useEffect, useMemo } from 'react';
import { useLoadingValue } from '../util';
import { snapshotToData, useIsFirestoreRefEqual } from './helpers';
import {
Data,
DataOptions,
DocumentDataHook,
DocumentHook,
GetOptions,
OnceDataOptions,
OnceOptions,
Options,
} from './types';
export const useDocument = <T = DocumentData>(
docRef?: DocumentReference<T> | null,
options?: Options
): DocumentHook<T> => {
return useDocumentInternal<T>(true, docRef, options);
};
export const useDocumentOnce = <T = DocumentData>(
docRef?: DocumentReference<T> | null,
options?: OnceOptions
): DocumentHook<T> => {
return useDocumentInternal<T>(false, docRef, options);
};
export const useDocumentData = <
T = DocumentData,
IDField extends string = '',
RefField extends string = ''
>(
docRef?: DocumentReference<T> | null,
options?: DataOptions<T>
): DocumentDataHook<T, IDField, RefField> => {
return useDocumentDataInternal<T, IDField, RefField>(true, docRef, options);
};
export const useDocumentDataOnce = <
T = DocumentData,
IDField extends string = '',
RefField extends string = ''
>(
docRef?: DocumentReference<T> | null,
options?: OnceDataOptions<T>
): DocumentDataHook<T, IDField, RefField> => {
return useDocumentDataInternal<T, IDField, RefField>(false, docRef, options);
};
const useDocumentInternal = <T = DocumentData>(
listen: boolean,
docRef?: DocumentReference<T> | null,
options?: Options & OnceOptions
): DocumentHook<T> => {
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
DocumentSnapshot<T>,
FirestoreError
>();
const ref = useIsFirestoreRefEqual<DocumentReference<T>>(docRef, reset);
useEffect(() => {
if (!ref.current) {
setValue(undefined);
return;
}
if (listen) {
const listener =
options && options.snapshotListenOptions
? onSnapshot(
ref.current,
options.snapshotListenOptions,
setValue,
setError
)
: onSnapshot(ref.current, setValue, setError);
return () => {
listener();
};
} else {
const get = getDocFnFromGetOptions(
options ? options.getOptions : undefined
);
get(ref.current).then(setValue).catch(setError);
}
}, [ref.current]);
const resArray: DocumentHook<T> = [
value as DocumentSnapshot<T>,
loading,
error,
];
return useMemo(() => resArray, resArray);
};
const useDocumentDataInternal = <
T = DocumentData,
IDField extends string = '',
RefField extends string = ''
>(
listen: boolean,
docRef?: DocumentReference<T> | null,
options?: DataOptions<T>
): DocumentDataHook<T, IDField, RefField> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
const snapshotOptions = options ? options.snapshotOptions : undefined;
const transform = options ? options.transform : undefined;
const [snapshot, loading, error] = useDocumentInternal<T>(
listen,
docRef,
options
);
const value = useMemo(
() =>
(snapshot
? snapshotToData<T>(
snapshot,
snapshotOptions,
idField,
refField,
transform
)
: undefined) as Data<T, IDField, RefField>,
[snapshot, snapshotOptions, idField, refField, transform]
);
const resArray: DocumentDataHook<T, IDField, RefField> = [
value,
loading,
error,
];
return useMemo(() => resArray, resArray);
};
function getDocFnFromGetOptions(
{ source }: GetOptions = { source: 'default' }
) {
switch (source) {
default:
case 'default':
return getDoc;
case 'cache':
return getDocFromCache;
case 'server':
return getDocFromServer;
}
}