Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions apps/web/src/components/documents/useDocumentActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
useSaveLocalRevisionMutation,
} from '@/queries/revisions';
import { useActions, useSelector } from '@/store';
import { toast } from 'sonner';

interface UseDocumentActionsOptions {
listenForSaveRequests?: boolean;
Expand Down Expand Up @@ -71,11 +72,20 @@ export function useDocumentActions(

const isPreviouslySaved = !!cloudRevision;
const isUnchangedSinceLoad = !!loadedChecksum && loadedChecksum === checksum;
const isUpToDate =
isUnchangedSinceLoad ||
(isPreviouslySaved &&
document?.currentRevisionId === cloudRevision.id &&
cloudRevision.checksum === checksum);
const isCurrentHeadContent =
isPreviouslySaved &&
document?.currentRevisionId === cloudRevision.id &&
cloudRevision.checksum === checksum;
const isUpToDate = isUnchangedSinceLoad || isCurrentHeadContent;

// Saves can be performed by any of this hook's instances (tab button,
// Cmd+S, tab context menu), and each instance keeps its own baseline.
// When the current content is confirmed as the saved head, re-baseline so
// an instance that did not perform the save cannot keep a stale view.
if (checksum && isCurrentHeadContent && (loadedChecksum !== checksum || hasUserEdit)) {
setLoadedChecksum(checksum);
setHasUserEdit(false);
}

const isLoading =
!document ||
Expand Down Expand Up @@ -147,6 +157,11 @@ export function useDocumentActions(
});
await saveLocalRevision({ serializedEditorState: JSON.parse(revision.content) });
}
// This path reuses an existing revision, so the save mutation's own
// toast never fires; confirm explicit saves here.
if (!isAutosave) {
toast.success('Document saved');
}
} else {
const serializedEditorState = await queryClient
.ensureQueryData(
Expand Down Expand Up @@ -282,19 +297,22 @@ export function useDocumentActions(
};
}, [options?.listenForSaveRequests, tabId]);

// Dirty means the user edited AND the content still differs from a saved
// state — the same condition that enables saving, so the indicator and the
// save button cannot disagree (e.g. after undoing back to the original).
useEffect(() => {
if (isLoading) return;
if (isDocumentTab) {
setTabDirty(tab.id, hasUserEdit);
setTabDirty(tab.id, hasUserEdit && !isUpToDate);
}
}, [isDocumentTab, isLoading, hasUserEdit, setTabDirty, tab?.id]);
}, [isDocumentTab, isLoading, hasUserEdit, isUpToDate, setTabDirty, tab?.id]);

const backgroundClassName = (() => {
if (!handle) return undefined;
if (isJustSaved) {
return 'bg-green-100/80 dark:bg-green-900/40';
}
if (!isLoading && hasUserEdit) {
if (!isLoading && hasUserEdit && !isUpToDate) {
// Document has unsaved changes
return 'bg-yellow-100/70 dark:bg-yellow-900/30';
}
Expand Down
12 changes: 9 additions & 3 deletions packages/editor/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ export const Editor: React.FC<{
[documentId, tabId, updateEditorStoreState],
);

// Deferred: listeners for this event mount in the same commit as the editor
// but their effects run later, so a synchronous dispatch is lost and the
// first keystroke would be mistaken for the loaded baseline.
useEffect(() => {
const editorState = editor.getEditorState();
const serializedEditorState = serializeEditorState(editorState);
updateChecksum(serializedEditorState);
const timeout = setTimeout(() => {
const editorState = editor.getEditorState();
const serializedEditorState = serializeEditorState(editorState);
updateChecksum(serializedEditorState);
}, 0);
return () => clearTimeout(timeout);
}, [editor, updateChecksum]);

const onChangeHandler = useCallback(
Expand Down
Loading