Skip to content

Commit

Permalink
Don't crash standalone doc editor
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoldowsky committed Sep 25, 2024
1 parent 3d554b8 commit bdd53ed
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/hooks/use-document-sync-to-firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export function useDocumentSyncToFirebase(
!disableFirebaseSync && !readOnly && (user.id !== uid) &&
console.warn("useDocumentSyncToFirebase monitoring another user's document?!?");

const commonSyncEnabled = !disableFirebaseSync && contentStatus === ContentStatus.Valid;

useEffect(() => {
// Tree monitoring should be disabled if the document status is error
if (!readOnly && contentStatus === ContentStatus.Valid) {
Expand All @@ -68,15 +70,17 @@ export function useDocumentSyncToFirebase(
document.treeMonitor.enabled = true;
}
// Set up listener for online status
firebase.onlineStatusRef.on('value', handlePresenceChange);
if (commonSyncEnabled) {
firebase.onlineStatusRef.on('value', handlePresenceChange);
}

return () => {
// disable history tracking on this document
if (document.treeMonitor) {
document.treeMonitor.enabled = false;
}
// Remove the online status listener
if (!readOnly && contentStatus === ContentStatus.Valid) {
if (!readOnly && commonSyncEnabled) {
firebase.onlineStatusRef.off('value', handlePresenceChange);
}
// If an onDisconnect is set, remove it and set the updated timestamp to now.
Expand All @@ -85,7 +89,8 @@ export function useDocumentSyncToFirebase(
}
};
}
}, [readOnly, contentStatus, document.treeMonitor, firebase, user, key, uid, handlePresenceChange]);
}, [readOnly, contentStatus, document.treeMonitor, firebase, user, key, uid,
handlePresenceChange, commonSyncEnabled]);

if (!readOnly && DEBUG_DOCUMENT) {
// provide the document to the console so developers can inspect its content
Expand All @@ -95,8 +100,6 @@ export function useDocumentSyncToFirebase(
(window as any).currentDocument = document;
}

const commonSyncEnabled = !disableFirebaseSync && contentStatus === ContentStatus.Valid;

/**
* We currently have multiple firestore metadata docs for each real doc.
* Use this function to update a property in all of them.
Expand Down Expand Up @@ -217,7 +220,7 @@ export function useDocumentSyncToFirebase(
({ changeCount: document.incChangeCount(), content: JSON.stringify(snapshot) });

const mutation = useMutation((snapshot: DocumentContentSnapshotType) => {
if (!disconnectHandler.current) {
if (!disconnectHandler.current && commonSyncEnabled) {
disconnectHandler.current = firebase.setLastEditedOnDisconnect(user, key, uid);
}

Expand Down

0 comments on commit bdd53ed

Please sign in to comment.