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
+41 -7
View File
@@ -1,13 +1,47 @@
// src/storage/index.js — barrel re-export of Firebase SDK.
// Phase C: App.js swaps imports from 'firebase/*' to here.
// STORAGE=firebase = identical behavior. Zero risk.
// Later: factory picks impl based on REACT_APP_STORAGE env.
// src/storage/index.js — storage factory + SDK re-exports.
// STORAGE=firebase (default): adapter wraps SDK. STORAGE=ws: backend.
// App.js imports getStorage() for subscribe; still imports SDK pieces for writes (per-group refactor pending).
export { initializeApp } from 'firebase/app';
export {
import { initializeApp } from 'firebase/app';
import {
getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken,
} from 'firebase/auth';
export {
import {
getFirestore, doc, setDoc, getDoc, getDocs, addDoc, collection,
onSnapshot, updateDoc, deleteDoc, query, orderBy, limit, writeBatch,
} from 'firebase/firestore';
import { initFirebase, createFirebaseStorage } from './firebase';
let storageInstance = null;
// Returns adapter instance implementing interface (getDoc/setDoc/subscribeDoc/etc).
export function getStorage() {
if (storageInstance) return storageInstance;
const mode = process.env.REACT_APP_STORAGE || 'firebase';
if (mode === 'firebase') {
const ok = initFirebase();
if (!ok) throw new Error('Firebase config missing. Check REACT_APP_FIREBASE_* env.');
storageInstance = createFirebaseStorage();
} else if (mode === 'ws') {
const { createWsStorage } = require('./ws');
storageInstance = createWsStorage({
baseUrl: process.env.REACT_APP_BACKEND_URL || 'http://127.0.0.1:4001',
wsUrl: process.env.REACT_APP_BACKEND_WS || 'ws://127.0.0.1:4001/ws',
});
} else {
const { createMemoryStorage } = require('./memory');
storageInstance = createMemoryStorage();
}
return storageInstance;
}
export function getStorageMode() {
return process.env.REACT_APP_STORAGE || 'firebase';
}
export {
initializeApp,
getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken,
getFirestore, doc, setDoc, getDoc, getDocs, addDoc, collection,
onSnapshot, updateDoc, deleteDoc, query, orderBy, limit, writeBatch,
};