@marcwelti/mw-core 0.2.0
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 +141 -0
- package/dist/context/index.d.mts +95 -0
- package/dist/context/index.d.ts +95 -0
- package/dist/context/index.js +280 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/index.mjs +276 -0
- package/dist/context/index.mjs.map +1 -0
- package/dist/firebase/index.d.mts +176 -0
- package/dist/firebase/index.d.ts +176 -0
- package/dist/firebase/index.js +393 -0
- package/dist/firebase/index.js.map +1 -0
- package/dist/firebase/index.mjs +318 -0
- package/dist/firebase/index.mjs.map +1 -0
- package/dist/hooks/index.d.mts +97 -0
- package/dist/hooks/index.d.ts +97 -0
- package/dist/hooks/index.js +618 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +611 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +922 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +843 -0
- package/dist/index.mjs.map +1 -0
- package/dist/server/index.d.mts +216 -0
- package/dist/server/index.d.ts +216 -0
- package/dist/server/index.js +309 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +276 -0
- package/dist/server/index.mjs.map +1 -0
- package/dist/storage-BU_rfYCi.d.mts +43 -0
- package/dist/storage-BU_rfYCi.d.ts +43 -0
- package/dist/types/index.d.mts +108 -0
- package/dist/types/index.d.ts +108 -0
- package/dist/types/index.js +12 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +3 -0
- package/dist/types/index.mjs.map +1 -0
- package/package.json +91 -0
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import { getAuth, onAuthStateChanged, signInWithEmailAndPassword, createUserWithEmailAndPassword, updateProfile, signOut as signOut$1, GoogleAuthProvider, signInWithPopup, sendPasswordResetEmail, sendEmailVerification } from 'firebase/auth';
|
|
3
|
+
import { getApps, getApp, initializeApp } from 'firebase/app';
|
|
4
|
+
import { getFirestore, onSnapshot, getDoc, query, getDocs, addDoc, serverTimestamp, setDoc, updateDoc, deleteDoc, doc, collection } from 'firebase/firestore';
|
|
5
|
+
import { getStorage, uploadBytes, getDownloadURL, uploadBytesResumable, deleteObject, ref } from 'firebase/storage';
|
|
6
|
+
|
|
7
|
+
// src/hooks/useAuth.ts
|
|
8
|
+
function getFirebaseConfig() {
|
|
9
|
+
const config = {
|
|
10
|
+
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY || "",
|
|
11
|
+
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN || "",
|
|
12
|
+
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID || "",
|
|
13
|
+
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET || "",
|
|
14
|
+
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID || "",
|
|
15
|
+
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID || "",
|
|
16
|
+
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID
|
|
17
|
+
};
|
|
18
|
+
const requiredFields = [
|
|
19
|
+
"apiKey",
|
|
20
|
+
"authDomain",
|
|
21
|
+
"projectId",
|
|
22
|
+
"storageBucket",
|
|
23
|
+
"messagingSenderId",
|
|
24
|
+
"appId"
|
|
25
|
+
];
|
|
26
|
+
const missingFields = requiredFields.filter((field) => !config[field]);
|
|
27
|
+
if (missingFields.length > 0) {
|
|
28
|
+
console.warn(
|
|
29
|
+
`[mw-core] Missing Firebase config fields: ${missingFields.join(", ")}. Make sure to set NEXT_PUBLIC_FIREBASE_* environment variables.`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return config;
|
|
33
|
+
}
|
|
34
|
+
function initializeFirebase(config) {
|
|
35
|
+
if (getApps().length > 0) {
|
|
36
|
+
return getApp();
|
|
37
|
+
}
|
|
38
|
+
const firebaseConfig = getFirebaseConfig();
|
|
39
|
+
return initializeApp(firebaseConfig);
|
|
40
|
+
}
|
|
41
|
+
var _app = null;
|
|
42
|
+
var _auth = null;
|
|
43
|
+
var _db = null;
|
|
44
|
+
var _storage = null;
|
|
45
|
+
function getFirebaseApp() {
|
|
46
|
+
if (!_app) {
|
|
47
|
+
_app = initializeFirebase();
|
|
48
|
+
}
|
|
49
|
+
return _app;
|
|
50
|
+
}
|
|
51
|
+
function getFirebaseAuth() {
|
|
52
|
+
if (!_auth) {
|
|
53
|
+
_auth = getAuth(getFirebaseApp());
|
|
54
|
+
}
|
|
55
|
+
return _auth;
|
|
56
|
+
}
|
|
57
|
+
function getFirebaseFirestore() {
|
|
58
|
+
if (!_db) {
|
|
59
|
+
_db = getFirestore(getFirebaseApp());
|
|
60
|
+
}
|
|
61
|
+
return _db;
|
|
62
|
+
}
|
|
63
|
+
function getFirebaseStorage() {
|
|
64
|
+
if (!_storage) {
|
|
65
|
+
_storage = getStorage(getFirebaseApp());
|
|
66
|
+
}
|
|
67
|
+
return _storage;
|
|
68
|
+
}
|
|
69
|
+
typeof window !== "undefined" ? getFirebaseApp() : null;
|
|
70
|
+
typeof window !== "undefined" ? getFirebaseAuth() : null;
|
|
71
|
+
typeof window !== "undefined" ? getFirebaseFirestore() : null;
|
|
72
|
+
typeof window !== "undefined" ? getFirebaseStorage() : null;
|
|
73
|
+
|
|
74
|
+
// src/firebase/auth.ts
|
|
75
|
+
async function signInWithEmail(email, password) {
|
|
76
|
+
const auth2 = getFirebaseAuth();
|
|
77
|
+
return signInWithEmailAndPassword(auth2, email, password);
|
|
78
|
+
}
|
|
79
|
+
async function signUpWithEmail(email, password, displayName) {
|
|
80
|
+
const auth2 = getFirebaseAuth();
|
|
81
|
+
const credential = await createUserWithEmailAndPassword(auth2, email, password);
|
|
82
|
+
if (displayName && credential.user) {
|
|
83
|
+
await updateProfile(credential.user, { displayName });
|
|
84
|
+
}
|
|
85
|
+
return credential;
|
|
86
|
+
}
|
|
87
|
+
async function signOut() {
|
|
88
|
+
const auth2 = getFirebaseAuth();
|
|
89
|
+
return signOut$1(auth2);
|
|
90
|
+
}
|
|
91
|
+
async function resetPassword(email) {
|
|
92
|
+
const auth2 = getFirebaseAuth();
|
|
93
|
+
return sendPasswordResetEmail(auth2, email);
|
|
94
|
+
}
|
|
95
|
+
async function sendVerificationEmail(user) {
|
|
96
|
+
return sendEmailVerification(user);
|
|
97
|
+
}
|
|
98
|
+
async function updateUserProfile(user, profile) {
|
|
99
|
+
return updateProfile(user, profile);
|
|
100
|
+
}
|
|
101
|
+
async function signInWithGoogle() {
|
|
102
|
+
const auth2 = getFirebaseAuth();
|
|
103
|
+
const provider = new GoogleAuthProvider();
|
|
104
|
+
provider.addScope("email");
|
|
105
|
+
provider.addScope("profile");
|
|
106
|
+
return signInWithPopup(auth2, provider);
|
|
107
|
+
}
|
|
108
|
+
function subscribeToAuthState(callback) {
|
|
109
|
+
const auth2 = getFirebaseAuth();
|
|
110
|
+
return onAuthStateChanged(auth2, callback);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/hooks/useAuth.ts
|
|
114
|
+
function useAuth() {
|
|
115
|
+
const [user, setUser] = useState(null);
|
|
116
|
+
const [loading, setLoading] = useState(true);
|
|
117
|
+
const [error, setError] = useState(null);
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
const unsubscribe = subscribeToAuthState((user2) => {
|
|
120
|
+
setUser(user2);
|
|
121
|
+
setLoading(false);
|
|
122
|
+
});
|
|
123
|
+
return unsubscribe;
|
|
124
|
+
}, []);
|
|
125
|
+
const signIn = useCallback(async (email, password) => {
|
|
126
|
+
setError(null);
|
|
127
|
+
try {
|
|
128
|
+
return await signInWithEmail(email, password);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
const error2 = err;
|
|
131
|
+
setError(error2);
|
|
132
|
+
throw error2;
|
|
133
|
+
}
|
|
134
|
+
}, []);
|
|
135
|
+
const signUp = useCallback(
|
|
136
|
+
async (email, password, displayName) => {
|
|
137
|
+
setError(null);
|
|
138
|
+
try {
|
|
139
|
+
return await signUpWithEmail(email, password, displayName);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
const error2 = err;
|
|
142
|
+
setError(error2);
|
|
143
|
+
throw error2;
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
[]
|
|
147
|
+
);
|
|
148
|
+
const signOut2 = useCallback(async () => {
|
|
149
|
+
setError(null);
|
|
150
|
+
try {
|
|
151
|
+
await signOut();
|
|
152
|
+
} catch (err) {
|
|
153
|
+
const error2 = err;
|
|
154
|
+
setError(error2);
|
|
155
|
+
throw error2;
|
|
156
|
+
}
|
|
157
|
+
}, []);
|
|
158
|
+
const signInWithGoogle2 = useCallback(async () => {
|
|
159
|
+
setError(null);
|
|
160
|
+
try {
|
|
161
|
+
return await signInWithGoogle();
|
|
162
|
+
} catch (err) {
|
|
163
|
+
const error2 = err;
|
|
164
|
+
setError(error2);
|
|
165
|
+
throw error2;
|
|
166
|
+
}
|
|
167
|
+
}, []);
|
|
168
|
+
const resetPassword2 = useCallback(async (email) => {
|
|
169
|
+
setError(null);
|
|
170
|
+
try {
|
|
171
|
+
await resetPassword(email);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
const error2 = err;
|
|
174
|
+
setError(error2);
|
|
175
|
+
throw error2;
|
|
176
|
+
}
|
|
177
|
+
}, []);
|
|
178
|
+
const updateProfile2 = useCallback(
|
|
179
|
+
async (profile) => {
|
|
180
|
+
setError(null);
|
|
181
|
+
if (!user) {
|
|
182
|
+
const error2 = new Error("No authenticated user");
|
|
183
|
+
setError(error2);
|
|
184
|
+
throw error2;
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
await updateUserProfile(user, profile);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
const error2 = err;
|
|
190
|
+
setError(error2);
|
|
191
|
+
throw error2;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
[user]
|
|
195
|
+
);
|
|
196
|
+
const sendEmailVerificationFn = useCallback(async () => {
|
|
197
|
+
setError(null);
|
|
198
|
+
if (!user) {
|
|
199
|
+
const error2 = new Error("No authenticated user");
|
|
200
|
+
setError(error2);
|
|
201
|
+
throw error2;
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
await sendVerificationEmail(user);
|
|
205
|
+
} catch (err) {
|
|
206
|
+
const error2 = err;
|
|
207
|
+
setError(error2);
|
|
208
|
+
throw error2;
|
|
209
|
+
}
|
|
210
|
+
}, [user]);
|
|
211
|
+
const clearError = useCallback(() => {
|
|
212
|
+
setError(null);
|
|
213
|
+
}, []);
|
|
214
|
+
return {
|
|
215
|
+
user,
|
|
216
|
+
loading,
|
|
217
|
+
error,
|
|
218
|
+
signIn,
|
|
219
|
+
signUp,
|
|
220
|
+
signOut: signOut2,
|
|
221
|
+
signInWithGoogle: signInWithGoogle2,
|
|
222
|
+
resetPassword: resetPassword2,
|
|
223
|
+
updateProfile: updateProfile2,
|
|
224
|
+
sendEmailVerification: sendEmailVerificationFn,
|
|
225
|
+
clearError
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function getDocRef(collectionPath, documentId) {
|
|
229
|
+
const db2 = getFirebaseFirestore();
|
|
230
|
+
return doc(db2, collectionPath, documentId);
|
|
231
|
+
}
|
|
232
|
+
function getCollectionRef(collectionPath) {
|
|
233
|
+
const db2 = getFirebaseFirestore();
|
|
234
|
+
return collection(db2, collectionPath);
|
|
235
|
+
}
|
|
236
|
+
async function getDocument(collectionPath, documentId) {
|
|
237
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
238
|
+
const docSnap = await getDoc(docRef);
|
|
239
|
+
if (docSnap.exists()) {
|
|
240
|
+
return { id: docSnap.id, ...docSnap.data() };
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
async function getCollection(collectionPath, constraints = []) {
|
|
245
|
+
const collectionRef = getCollectionRef(collectionPath);
|
|
246
|
+
const q = query(collectionRef, ...constraints);
|
|
247
|
+
const querySnapshot = await getDocs(q);
|
|
248
|
+
return querySnapshot.docs.map((doc2) => ({
|
|
249
|
+
id: doc2.id,
|
|
250
|
+
...doc2.data()
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
253
|
+
async function addDocument(collectionPath, data) {
|
|
254
|
+
const collectionRef = getCollectionRef(collectionPath);
|
|
255
|
+
const docRef = await addDoc(collectionRef, {
|
|
256
|
+
...data,
|
|
257
|
+
createdAt: serverTimestamp(),
|
|
258
|
+
updatedAt: serverTimestamp()
|
|
259
|
+
});
|
|
260
|
+
return docRef.id;
|
|
261
|
+
}
|
|
262
|
+
async function setDocument(collectionPath, documentId, data, merge = false) {
|
|
263
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
264
|
+
await setDoc(
|
|
265
|
+
docRef,
|
|
266
|
+
{
|
|
267
|
+
...data,
|
|
268
|
+
updatedAt: serverTimestamp(),
|
|
269
|
+
...merge ? {} : { createdAt: serverTimestamp() }
|
|
270
|
+
},
|
|
271
|
+
{ merge }
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
async function updateDocument(collectionPath, documentId, data) {
|
|
275
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
276
|
+
await updateDoc(docRef, {
|
|
277
|
+
...data,
|
|
278
|
+
updatedAt: serverTimestamp()
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
async function deleteDocument(collectionPath, documentId) {
|
|
282
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
283
|
+
await deleteDoc(docRef);
|
|
284
|
+
}
|
|
285
|
+
function subscribeToDocument(collectionPath, documentId, callback) {
|
|
286
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
287
|
+
return onSnapshot(docRef, (docSnap) => {
|
|
288
|
+
if (docSnap.exists()) {
|
|
289
|
+
callback({ id: docSnap.id, ...docSnap.data() });
|
|
290
|
+
} else {
|
|
291
|
+
callback(null);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function subscribeToCollection(collectionPath, callback, constraints = []) {
|
|
296
|
+
const collectionRef = getCollectionRef(collectionPath);
|
|
297
|
+
const q = query(collectionRef, ...constraints);
|
|
298
|
+
return onSnapshot(q, (querySnapshot) => {
|
|
299
|
+
const data = querySnapshot.docs.map((doc2) => ({
|
|
300
|
+
id: doc2.id,
|
|
301
|
+
...doc2.data()
|
|
302
|
+
}));
|
|
303
|
+
callback(data);
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// src/hooks/useFirestore.ts
|
|
308
|
+
function useDocument(collectionPath, documentId, options = {}) {
|
|
309
|
+
const { subscribe = false } = options;
|
|
310
|
+
const [data, setData] = useState(null);
|
|
311
|
+
const [loading, setLoading] = useState(true);
|
|
312
|
+
const [error, setError] = useState(null);
|
|
313
|
+
useEffect(() => {
|
|
314
|
+
if (!documentId) {
|
|
315
|
+
setData(null);
|
|
316
|
+
setLoading(false);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
setLoading(true);
|
|
320
|
+
setError(null);
|
|
321
|
+
if (subscribe) {
|
|
322
|
+
const unsubscribe = subscribeToDocument(
|
|
323
|
+
collectionPath,
|
|
324
|
+
documentId,
|
|
325
|
+
(doc2) => {
|
|
326
|
+
setData(doc2);
|
|
327
|
+
setLoading(false);
|
|
328
|
+
}
|
|
329
|
+
);
|
|
330
|
+
return unsubscribe;
|
|
331
|
+
} else {
|
|
332
|
+
getDocument(collectionPath, documentId).then((doc2) => {
|
|
333
|
+
setData(doc2);
|
|
334
|
+
setLoading(false);
|
|
335
|
+
}).catch((err) => {
|
|
336
|
+
setError(err);
|
|
337
|
+
setLoading(false);
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}, [collectionPath, documentId, subscribe]);
|
|
341
|
+
const refresh = useCallback(async () => {
|
|
342
|
+
if (!documentId) return;
|
|
343
|
+
setLoading(true);
|
|
344
|
+
try {
|
|
345
|
+
const doc2 = await getDocument(collectionPath, documentId);
|
|
346
|
+
setData(doc2);
|
|
347
|
+
} catch (err) {
|
|
348
|
+
setError(err);
|
|
349
|
+
} finally {
|
|
350
|
+
setLoading(false);
|
|
351
|
+
}
|
|
352
|
+
}, [collectionPath, documentId]);
|
|
353
|
+
return { data, loading, error, refresh };
|
|
354
|
+
}
|
|
355
|
+
function useCollection(collectionPath, constraints = [], options = {}) {
|
|
356
|
+
const { subscribe = false } = options;
|
|
357
|
+
const [data, setData] = useState([]);
|
|
358
|
+
const [loading, setLoading] = useState(true);
|
|
359
|
+
const [error, setError] = useState(null);
|
|
360
|
+
const constraintsKey = JSON.stringify(
|
|
361
|
+
constraints.map((c) => c.type)
|
|
362
|
+
);
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
setLoading(true);
|
|
365
|
+
setError(null);
|
|
366
|
+
if (subscribe) {
|
|
367
|
+
const unsubscribe = subscribeToCollection(
|
|
368
|
+
collectionPath,
|
|
369
|
+
(docs) => {
|
|
370
|
+
setData(docs);
|
|
371
|
+
setLoading(false);
|
|
372
|
+
},
|
|
373
|
+
constraints
|
|
374
|
+
);
|
|
375
|
+
return unsubscribe;
|
|
376
|
+
} else {
|
|
377
|
+
getCollection(collectionPath, constraints).then((docs) => {
|
|
378
|
+
setData(docs);
|
|
379
|
+
setLoading(false);
|
|
380
|
+
}).catch((err) => {
|
|
381
|
+
setError(err);
|
|
382
|
+
setLoading(false);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}, [collectionPath, constraintsKey, subscribe]);
|
|
386
|
+
const refresh = useCallback(async () => {
|
|
387
|
+
setLoading(true);
|
|
388
|
+
try {
|
|
389
|
+
const docs = await getCollection(collectionPath, constraints);
|
|
390
|
+
setData(docs);
|
|
391
|
+
} catch (err) {
|
|
392
|
+
setError(err);
|
|
393
|
+
} finally {
|
|
394
|
+
setLoading(false);
|
|
395
|
+
}
|
|
396
|
+
}, [collectionPath, constraintsKey]);
|
|
397
|
+
return { data, loading, error, refresh };
|
|
398
|
+
}
|
|
399
|
+
function useFirestoreMutation(collectionPath) {
|
|
400
|
+
const [loading, setLoading] = useState(false);
|
|
401
|
+
const [error, setError] = useState(null);
|
|
402
|
+
const add = useCallback(
|
|
403
|
+
async (data) => {
|
|
404
|
+
setLoading(true);
|
|
405
|
+
setError(null);
|
|
406
|
+
try {
|
|
407
|
+
const id = await addDocument(collectionPath, data);
|
|
408
|
+
return id;
|
|
409
|
+
} catch (err) {
|
|
410
|
+
setError(err);
|
|
411
|
+
throw err;
|
|
412
|
+
} finally {
|
|
413
|
+
setLoading(false);
|
|
414
|
+
}
|
|
415
|
+
},
|
|
416
|
+
[collectionPath]
|
|
417
|
+
);
|
|
418
|
+
const set = useCallback(
|
|
419
|
+
async (documentId, data, merge = false) => {
|
|
420
|
+
setLoading(true);
|
|
421
|
+
setError(null);
|
|
422
|
+
try {
|
|
423
|
+
await setDocument(collectionPath, documentId, data, merge);
|
|
424
|
+
} catch (err) {
|
|
425
|
+
setError(err);
|
|
426
|
+
throw err;
|
|
427
|
+
} finally {
|
|
428
|
+
setLoading(false);
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
[collectionPath]
|
|
432
|
+
);
|
|
433
|
+
const update = useCallback(
|
|
434
|
+
async (documentId, data) => {
|
|
435
|
+
setLoading(true);
|
|
436
|
+
setError(null);
|
|
437
|
+
try {
|
|
438
|
+
await updateDocument(collectionPath, documentId, data);
|
|
439
|
+
} catch (err) {
|
|
440
|
+
setError(err);
|
|
441
|
+
throw err;
|
|
442
|
+
} finally {
|
|
443
|
+
setLoading(false);
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
[collectionPath]
|
|
447
|
+
);
|
|
448
|
+
const remove = useCallback(
|
|
449
|
+
async (documentId) => {
|
|
450
|
+
setLoading(true);
|
|
451
|
+
setError(null);
|
|
452
|
+
try {
|
|
453
|
+
await deleteDocument(collectionPath, documentId);
|
|
454
|
+
} catch (err) {
|
|
455
|
+
setError(err);
|
|
456
|
+
throw err;
|
|
457
|
+
} finally {
|
|
458
|
+
setLoading(false);
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
[collectionPath]
|
|
462
|
+
);
|
|
463
|
+
const clearError = useCallback(() => {
|
|
464
|
+
setError(null);
|
|
465
|
+
}, []);
|
|
466
|
+
return {
|
|
467
|
+
add,
|
|
468
|
+
set,
|
|
469
|
+
update,
|
|
470
|
+
remove,
|
|
471
|
+
loading,
|
|
472
|
+
error,
|
|
473
|
+
clearError
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function getStorageRef(path) {
|
|
477
|
+
const storage2 = getFirebaseStorage();
|
|
478
|
+
return ref(storage2, path);
|
|
479
|
+
}
|
|
480
|
+
async function uploadFile(path, file, metadata) {
|
|
481
|
+
const storageRef = getStorageRef(path);
|
|
482
|
+
await uploadBytes(storageRef, file, metadata);
|
|
483
|
+
return getDownloadURL(storageRef);
|
|
484
|
+
}
|
|
485
|
+
function uploadFileWithProgress(path, file, onProgress, metadata) {
|
|
486
|
+
const storageRef = getStorageRef(path);
|
|
487
|
+
const task = uploadBytesResumable(storageRef, file, metadata);
|
|
488
|
+
if (onProgress) {
|
|
489
|
+
task.on("state_changed", (snapshot) => {
|
|
490
|
+
const progress = snapshot.bytesTransferred / snapshot.totalBytes * 100;
|
|
491
|
+
onProgress(progress);
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
const promise = new Promise((resolve, reject) => {
|
|
495
|
+
task.on(
|
|
496
|
+
"state_changed",
|
|
497
|
+
null,
|
|
498
|
+
(error) => reject(error),
|
|
499
|
+
async () => {
|
|
500
|
+
const url = await getDownloadURL(task.snapshot.ref);
|
|
501
|
+
resolve(url);
|
|
502
|
+
}
|
|
503
|
+
);
|
|
504
|
+
});
|
|
505
|
+
return { task, promise };
|
|
506
|
+
}
|
|
507
|
+
async function getFileURL(path) {
|
|
508
|
+
const storageRef = getStorageRef(path);
|
|
509
|
+
return getDownloadURL(storageRef);
|
|
510
|
+
}
|
|
511
|
+
async function deleteFile(path) {
|
|
512
|
+
const storageRef = getStorageRef(path);
|
|
513
|
+
return deleteObject(storageRef);
|
|
514
|
+
}
|
|
515
|
+
function generateFilePath(folder, filename, userId) {
|
|
516
|
+
const timestamp = Date.now();
|
|
517
|
+
const extension = filename.split(".").pop() || "";
|
|
518
|
+
const baseName = filename.replace(/\.[^/.]+$/, "");
|
|
519
|
+
const sanitizedName = baseName.replace(/[^a-zA-Z0-9]/g, "_");
|
|
520
|
+
if (userId) {
|
|
521
|
+
return `${folder}/${userId}/${timestamp}_${sanitizedName}.${extension}`;
|
|
522
|
+
}
|
|
523
|
+
return `${folder}/${timestamp}_${sanitizedName}.${extension}`;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// src/hooks/useStorage.ts
|
|
527
|
+
function useStorage() {
|
|
528
|
+
const [uploading, setUploading] = useState(false);
|
|
529
|
+
const [progress, setProgress] = useState(0);
|
|
530
|
+
const [error, setError] = useState(null);
|
|
531
|
+
const upload = useCallback(
|
|
532
|
+
async (path, file, metadata) => {
|
|
533
|
+
setError(null);
|
|
534
|
+
setUploading(true);
|
|
535
|
+
setProgress(0);
|
|
536
|
+
try {
|
|
537
|
+
const url = await uploadFile(path, file, metadata);
|
|
538
|
+
setProgress(100);
|
|
539
|
+
return url;
|
|
540
|
+
} catch (err) {
|
|
541
|
+
const error2 = err;
|
|
542
|
+
setError(error2);
|
|
543
|
+
throw error2;
|
|
544
|
+
} finally {
|
|
545
|
+
setUploading(false);
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
[]
|
|
549
|
+
);
|
|
550
|
+
const uploadWithProgress = useCallback(
|
|
551
|
+
async (path, file, metadata) => {
|
|
552
|
+
setError(null);
|
|
553
|
+
setUploading(true);
|
|
554
|
+
setProgress(0);
|
|
555
|
+
try {
|
|
556
|
+
const { promise } = uploadFileWithProgress(
|
|
557
|
+
path,
|
|
558
|
+
file,
|
|
559
|
+
(p) => setProgress(p),
|
|
560
|
+
metadata
|
|
561
|
+
);
|
|
562
|
+
const url = await promise;
|
|
563
|
+
return url;
|
|
564
|
+
} catch (err) {
|
|
565
|
+
const error2 = err;
|
|
566
|
+
setError(error2);
|
|
567
|
+
throw error2;
|
|
568
|
+
} finally {
|
|
569
|
+
setUploading(false);
|
|
570
|
+
}
|
|
571
|
+
},
|
|
572
|
+
[]
|
|
573
|
+
);
|
|
574
|
+
const remove = useCallback(async (path) => {
|
|
575
|
+
setError(null);
|
|
576
|
+
try {
|
|
577
|
+
await deleteFile(path);
|
|
578
|
+
} catch (err) {
|
|
579
|
+
const error2 = err;
|
|
580
|
+
setError(error2);
|
|
581
|
+
throw error2;
|
|
582
|
+
}
|
|
583
|
+
}, []);
|
|
584
|
+
const getURL = useCallback(async (path) => {
|
|
585
|
+
setError(null);
|
|
586
|
+
try {
|
|
587
|
+
return await getFileURL(path);
|
|
588
|
+
} catch (err) {
|
|
589
|
+
const error2 = err;
|
|
590
|
+
setError(error2);
|
|
591
|
+
throw error2;
|
|
592
|
+
}
|
|
593
|
+
}, []);
|
|
594
|
+
const clearError = useCallback(() => {
|
|
595
|
+
setError(null);
|
|
596
|
+
}, []);
|
|
597
|
+
return {
|
|
598
|
+
upload,
|
|
599
|
+
uploadWithProgress,
|
|
600
|
+
remove,
|
|
601
|
+
getURL,
|
|
602
|
+
uploading,
|
|
603
|
+
progress,
|
|
604
|
+
error,
|
|
605
|
+
clearError
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export { generateFilePath, useAuth, useCollection, useDocument, useFirestoreMutation, useStorage };
|
|
610
|
+
//# sourceMappingURL=index.mjs.map
|
|
611
|
+
//# sourceMappingURL=index.mjs.map
|