Skip to content

Commit

Permalink
Track end of document edit session
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoldowsky committed Sep 24, 2024
1 parent 21fad09 commit e23d568
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/hooks/use-document-sync-to-firebase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useMemo } from "react";
import { useEffect, useMemo, useRef } from "react";
import { throttle as _throttle } from "lodash";
import { onSnapshot, SnapshotOut } from "mobx-state-tree";
import { OnDisconnect } from "firebase-admin/database";
import { useSyncMstNodeToFirebase } from "./use-sync-mst-node-to-firebase";
import { useSyncMstPropToFirebase } from "./use-sync-mst-prop-to-firebase";
import { DEBUG_DOCUMENT, DEBUG_SAVE } from "../lib/debug";
Expand Down Expand Up @@ -40,6 +41,7 @@ export function useDocumentSyncToFirebase(
) {
const { key, type, uid, contentStatus } = document;
const { content: contentPath, metadata, typedMetadata } = firebase.getUserDocumentPaths(user, type, key, uid);
const disconnectHandler = useRef<OnDisconnect|undefined>(undefined);

// TODO: when running in doc-editor this warning was printed constantly
// Ideally we'd figure out how to separate the syncing from the document stuff so the doc-editor can use
Expand All @@ -62,9 +64,14 @@ export function useDocumentSyncToFirebase(
if (document.treeMonitor) {
document.treeMonitor.enabled = false;
}
// If an onDisconnect was set, remove it and set updated timestamp to now.
if (disconnectHandler.current) {
firebase.setLastEditedNow(user, key, uid, disconnectHandler.current);
console.log("Doc closed, setting last modified", key);

Check warning on line 70 in src/hooks/use-document-sync-to-firebase.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement
}
};
}
}, [readOnly, contentStatus, document.treeMonitor]);
}, [readOnly, contentStatus, document.treeMonitor, firebase, user, key, uid]);

if (!readOnly && DEBUG_DOCUMENT) {
// provide the document to the console so developers can inspect its content
Expand Down Expand Up @@ -196,10 +203,13 @@ export function useDocumentSyncToFirebase(
({ changeCount: document.incChangeCount(), content: JSON.stringify(snapshot) });

const mutation = useMutation((snapshot: DocumentContentSnapshotType) => {
const tileMap = snapshot.tileMap || {};
if (!disconnectHandler.current) {
disconnectHandler.current = firebase.setLastEditedOnDisconnect(user, key, uid);
console.log("Doc modified, tracking disconnect", key);

Check warning on line 208 in src/hooks/use-document-sync-to-firebase.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement
}

const tileMap = snapshot.tileMap || {};
const tools: string[] = [];

Object.keys(tileMap).forEach((tileKey) => {
const tileInfo = tileMap[tileKey] as ITileMapEntry;
const tileType = tileInfo.content.type;
Expand Down
26 changes: 26 additions & 0 deletions src/lib/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ export class Firebase {
return `${this.getUserPath(user, userId)}/documentMetadata${suffix}`;
}

public getLastEditedMetadataPath(user: UserModelType, documentKey: string, userId?: string) {
return `${this.getUserDocumentMetadataPath(user, documentKey, userId)}/lastEditedAt`;
}

/**
* Set up a Firebase onDisconnect handler to update the lastEditedAt timestamp when the user disconnects.
*/
public setLastEditedOnDisconnect(user: UserModelType, documentKey: string, userId?: string) {
const ref = this.ref(this.getLastEditedMetadataPath(user, documentKey, userId));
const onDisconnect = ref.onDisconnect();
onDisconnect.set(firebase.database.ServerValue.TIMESTAMP);
return onDisconnect;
}

/**
* Set the lastEditedAt timestamp to the current time, optionally cancelling an onDisconnect handler.
*/
public setLastEditedNow(user: UserModelType, documentKey: string, userId: string|undefined,
onDisconnect?: firebase.database.OnDisconnect) {
if (onDisconnect) {
onDisconnect.cancel();
}
return this.ref(this.getLastEditedMetadataPath(user, documentKey, userId))
.set(firebase.database.ServerValue.TIMESTAMP);
}

// Unpublished personal document/learning log metadata
public getOtherDocumentPath(user: UserModelType, documentType: OtherDocumentType, documentKey?: string) {
const dir = documentType === PersonalDocument ? "personalDocs" : "learningLogs";
Expand Down

0 comments on commit e23d568

Please sign in to comment.