FocusFlow’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/piratta/gymApp/llms.txt
Use this file to discover all available pages before exploring further.
src/lib/firebase.ts exports the core persistence layer built on Firebase Firestore and Authentication. Every write is first committed to a local offline backup before hitting Firestore, enabling a fully offline-first experience — if the network is unavailable, data is queued in a dirty set and pushed to the cloud automatically the next time connectivity is restored. This reference documents every exported function and constant in the module.
Core persistence functions
saveToCloud
One of the 16
SYNC_KEYS (e.g. "fit_users", "fit_routines"). Used as both the Firestore document ID under coaching_data/ and the localStorage backup key prefix (fit_offline_backup_<key>).The value to persist. Serialized with
JSON.stringify before writing to Firestore under a payload field.Promise<boolean> — true when the Firestore write succeeds within the 10-second timeout; false when the write falls back to local storage only.
Behavior on failure: if the Firestore write times out or fails (non-permission error), key is added to the fit_cloud_dirty_keys set in localStorage so it can be retried by syncOfflineDataToCloud. A permission error immediately throws via handleFirestoreError.
loadFromCloud
The
SYNC_KEYS key to load. Queries coaching_data/<key> in Firestore.- The reconciled data value (array, object, or primitive) on success.
nullif the Firestore document does not exist and there is no local backup.undefinedon total failure (network error + no local backup).
reconcileLocalAndCloud is called. If the cloud and local copies differ, the merged result is silently pushed back to Firestore to keep the server up to date.
Fallback chain:
- Attempt Firestore read (15-second timeout).
- On timeout/error: read
fit_offline_backup_<key>from localStorage. - On localStorage failure: return
undefined.
loadAllFromCloud
SYNC_KEYS in parallel using Promise.all. This is the primary bootstrap call executed when the app initialises.
Returns a Record<string, any> mapping each sync key to its loaded value. Keys that fail to load will be undefined.
All 16 Firestore reads fire simultaneously. On a fresh install with an empty database every read resolves to
null; FocusFlow treats that as an empty initial state.syncOfflineDataToCloud
saveToCloud for each. Designed to be called whenever the app detects that connectivity has been restored.
Returns true if every pending key synced successfully; false if one or more keys failed.
getDirtyKeys
Set<string> of sync keys that have unsaved local changes not yet confirmed in Firestore. The set is read from the fit_cloud_dirty_keys entry in localStorage.
reconcileLocalAndCloud
loadFromCloud.
The sync key. Controls the merge strategy applied.
The data parsed from the Firestore
payload field.The data parsed from the
fit_offline_backup_<key> localStorage entry.| Condition | Winner |
|---|---|
key is in the dirty set | localData (local uncommitted changes take priority) |
key === "fit_reviews" | Cloud structure wins, but photos and feedbackPhotos arrays are restored from the matching local Review object (photos are never persisted to Firestore) |
| All other keys, clean state | cloudData |
handleFirestoreError
FirestoreErrorInfo object containing the raw error message, operation type, document path, and full Firebase Auth context of the current user, then throws it as a JSON-serialised Error. This function always throws — it never returns.
The raw error caught from a Firestore call.
The
OperationType enum value describing the failing operation.The Firestore document path that was being accessed, e.g.
"coaching_data/fit_users". Pass null if no specific path is available.Authentication exports
FocusFlow uses Firebase Anonymous Auth by default. Google OAuth is layered on top for features that require Drive or Sheets access (photo uploads, data exports).whenAuthReady
Promise<void> that resolves once Firebase Auth has finished initialising — either by restoring a persisted user session or by completing anonymous sign-in. Await this before making any Firestore calls to ensure auth.currentUser is populated.
signInWithGoogleWorkspace
spreadsheets.readonly and drive.file scopes) and returns the access token. If a token is already cached in memory the popup is skipped and the cached value is returned immediately.
Returns Promise<string> — the Google OAuth access token.
Throws if the popup is blocked or the user cancels.
signInWithGoogleSheets
signInWithGoogleWorkspace. Used in contexts where the intent is specifically spreadsheet access.
getGoogleAccessToken
null if no Google sign-in has occurred yet in this session.
setGoogleAccessToken
subscribeToGoogleToken callback has been registered, it is invoked immediately when a non-null token is set.
The new token value, or
null to clear it.subscribeToGoogleToken
Called with the token string once it is available.
logoutGoogleSync
SYNC_KEYS constant
- The Firestore document ID within the
coaching_datacollection. - The localStorage offline backup key suffix (
fit_offline_backup_<key>). - A property key in the
Record<string, any>returned byloadAllFromCloud.
safeStorage
Defined insrc/lib/storage.ts and re-exported implicitly through firebase.ts internals.
localStorage wrapper that falls back to an in-memory Record<string, string> when window.localStorage is unavailable or throws a SecurityError (common in sandboxed iframes and certain browser privacy modes).
All four persistence functions in firebase.ts use safeStorage internally — you should prefer it over direct localStorage access anywhere else in the codebase for the same resilience.