M2: refactor hooks to storage adapter (subscribe)

- src/storage/index.js: getStorage() factory + SDK re-exports
- App.js: useFirestoreDocument/Collection call storage.subscribeDoc/Collection
- getStorage import added

56 frontend tests green. Hooks now impl-agnostic (firebase vs ws).
This commit is contained in:
david raistrick
2026-06-28 19:03:44 -04:00
parent 35b5a1d238
commit 5bb9e5fc19
2 changed files with 59 additions and 46 deletions
+18 -39
View File
@@ -1,7 +1,7 @@
import React, { useState, useEffect, useRef, useMemo } from 'react';
import { initializeApp } from './storage';
import { getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken } from './storage';
import { getFirestore, doc, setDoc, getDoc, getDocs, addDoc, collection, onSnapshot, updateDoc, deleteDoc, query, orderBy, limit, writeBatch } from './storage';
import { getFirestore, doc, setDoc, getDoc, getDocs, addDoc, collection, onSnapshot, updateDoc, deleteDoc, query, orderBy, limit, writeBatch, getStorage } from './storage';
import {
PlusCircle, Users, Swords, Trash2, Eye, Edit3, Save, XCircle, ChevronsUpDown,
UserCheck, UserX, HeartCrack, HeartPulse, Zap, EyeOff, ExternalLink, AlertTriangle,
@@ -201,32 +201,22 @@ function useFirestoreDocument(docPath) {
const [error, setError] = useState(null);
useEffect(() => {
if (!db || !docPath) {
if (!docPath) {
setData(null);
setIsLoading(false);
setError(docPath ? "Firestore not available." : "Document path not provided.");
setError("Document path not provided.");
return;
}
setIsLoading(true);
setError(null);
const docRef = doc(db, docPath);
const unsubscribe = onSnapshot(
docRef,
(docSnap) => {
setData(docSnap.exists() ? { id: docSnap.id, ...docSnap.data() } : null);
setIsLoading(false);
},
(err) => {
console.error(`Error fetching document ${docPath}:`, err);
setError(err.message || "Failed to fetch document.");
setIsLoading(false);
setData(null);
}
);
return () => unsubscribe();
const storage = getStorage();
const unsubscribe = storage.subscribeDoc(docPath, (doc) => {
setData(doc);
setIsLoading(false);
});
return () => { if (typeof unsubscribe === 'function') unsubscribe(); };
}, [docPath]);
return { data, isLoading, error };
@@ -239,34 +229,23 @@ function useFirestoreCollection(collectionPath, queryConstraints = []) {
const queryString = useMemo(() => JSON.stringify(queryConstraints), [queryConstraints]);
useEffect(() => {
if (!db || !collectionPath) {
if (!collectionPath) {
setData([]);
setIsLoading(false);
setError(collectionPath ? "Firestore not available." : "Collection path not provided.");
setError("Collection path not provided.");
return;
}
setIsLoading(true);
setError(null);
const q = query(collection(db, collectionPath), ...queryConstraints);
const unsubscribe = onSnapshot(
q,
(snapshot) => {
const items = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
setData(items);
setIsLoading(false);
},
(err) => {
console.error(`Error fetching collection ${collectionPath}:`, err);
setError(err.message || "Failed to fetch collection.");
setIsLoading(false);
setData([]);
}
);
return () => unsubscribe();
// We use queryString instead of queryConstraints to avoid re-renders on array reference changes
const storage = getStorage();
const unsubscribe = storage.subscribeCollection(collectionPath, (items) => {
setData(items);
setIsLoading(false);
});
return () => { if (typeof unsubscribe === 'function') unsubscribe(); };
// queryString, not array ref
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collectionPath, queryString]);