-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse-firestore.ts
More file actions
170 lines (153 loc) · 4.25 KB
/
use-firestore.ts
File metadata and controls
170 lines (153 loc) · 4.25 KB
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
160
161
162
163
164
165
166
167
168
169
170
"use client";
import { useState, useEffect } from "react";
import {
collection,
doc,
getDoc,
getDocs,
onSnapshot,
query,
type QueryConstraint,
} from "firebase/firestore";
import { db } from "@/lib/firebase/config";
export function useDocument<T>(
collectionName: string,
docId: string | undefined
) {
const [data, setData] = useState<(T & { id: string }) | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
if (!docId) {
setLoading(false);
return;
}
const docRef = doc(db, collectionName, docId);
const unsubscribe = onSnapshot(
docRef,
(snapshot) => {
if (snapshot.exists()) {
setData({ ...snapshot.data(), id: snapshot.id } as T & { id: string });
} else {
setData(null);
}
setLoading(false);
setError(null);
},
(err) => {
setError(err);
setLoading(false);
}
);
return () => unsubscribe();
}, [collectionName, docId]);
return { data, loading, error };
}
export function useCollection<T>(
collectionName: string,
constraints: QueryConstraint[] = []
) {
const [data, setData] = useState<(T & { id: string })[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
const ref = collection(db, collectionName);
const q = query(ref, ...constraints);
const unsubscribe = onSnapshot(
q,
(snapshot) => {
const docs = snapshot.docs.map(
(doc) => ({ ...doc.data(), id: doc.id }) as T & { id: string }
);
setData(docs);
setLoading(false);
setError(null);
},
(err) => {
setError(err);
setLoading(false);
}
);
return () => unsubscribe();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collectionName, JSON.stringify(constraints)]);
return { data, loading, error };
}
/**
* One-shot collection fetch — same shape as `useCollection` but uses `getDocs`
* instead of a live listener. Use for slow-changing data (members, chapters)
* to avoid the listener-overhead and redundant snapshot deliveries.
*/
export function useCollectionOnce<T>(
collectionName: string,
constraints: QueryConstraint[] = []
) {
const [data, setData] = useState<(T & { id: string })[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
let cancelled = false;
setLoading(true);
const ref = collection(db, collectionName);
const q = query(ref, ...constraints);
getDocs(q)
.then((snapshot) => {
if (cancelled) return;
setData(
snapshot.docs.map(
(doc) => ({ ...doc.data(), id: doc.id }) as T & { id: string }
)
);
setError(null);
})
.catch((err) => {
if (!cancelled) setError(err);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collectionName, JSON.stringify(constraints)]);
return { data, loading, error };
}
/** One-shot single-doc fetch. See `useCollectionOnce` for rationale. */
export function useDocumentOnce<T>(
collectionName: string,
docId: string | undefined
) {
const [data, setData] = useState<(T & { id: string }) | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
let cancelled = false;
if (!docId) {
setLoading(false);
setData(null);
return;
}
setLoading(true);
getDoc(doc(db, collectionName, docId))
.then((snap) => {
if (cancelled) return;
if (snap.exists()) {
setData({ ...(snap.data() as T), id: snap.id });
} else {
setData(null);
}
setError(null);
})
.catch((err) => {
if (!cancelled) setError(err);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [collectionName, docId]);
return { data, loading, error };
}