mirror of
https://github.com/outline/outline.git
synced 2026-11-30 17:10:36 +03:00
fix: Reduce false unsynced changes warnings when leaving a document (#14006)
* fix: Only warn about unsynced changes when navigating to a different document * fix: Do not prompt about uploading images when staying on the same document * fix: Do not report edits before the first sync as unsynced when stored locally * fix: Treat opening a revision as leaving the document
This commit is contained in:
1 parent
0e19c7d89a
commit
a6b50dcc11
3 files changed
+94
-41
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import type { Location } from "history";
|
||||
import { observer } from "mobx-react";
|
||||
import { AllSelection } from "prosemirror-state";
|
||||
import { useRef, useCallback } from "react";
|
||||
import * as React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Prompt, useHistory, useLocation } from "react-router-dom";
|
||||
import { Prompt, matchPath, useHistory, useLocation } from "react-router-dom";
|
||||
import { toast } from "sonner";
|
||||
import styled from "styled-components";
|
||||
import breakpoint from "styled-components-breakpoint";
|
||||
@@ -12,6 +13,7 @@ import { s } from "@shared/styles";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { IconType, TOCPosition, TeamPreference } from "@shared/types";
|
||||
import { determineIconType } from "@shared/utils/icon";
|
||||
import parseDocumentSlug from "@shared/utils/parseDocumentSlug";
|
||||
import type Document from "~/models/Document";
|
||||
import type Revision from "~/models/Revision";
|
||||
import { useDocumentContext } from "~/components/DocumentContext";
|
||||
@@ -33,7 +35,11 @@ import isTextInput from "~/utils/isTextInput";
|
||||
import Logger from "~/utils/Logger";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import { emojiToUrl } from "~/utils/emoji";
|
||||
import { documentHistoryPath, documentEditPath } from "~/utils/routeHelpers";
|
||||
import {
|
||||
documentHistoryPath,
|
||||
documentEditPath,
|
||||
matchDocumentHistory,
|
||||
} from "~/utils/routeHelpers";
|
||||
import { useDocumentSave } from "../hooks/useDocumentSave";
|
||||
import Container from "./Container";
|
||||
import Contents from "./Contents";
|
||||
@@ -332,7 +338,18 @@ function DocumentScene({
|
||||
});
|
||||
};
|
||||
|
||||
const handleBlockNavigation = () => {
|
||||
const handleBlockNavigation = (nextLocation: Location) => {
|
||||
// Moving between views of this document, such as edit mode or a heading
|
||||
// anchor, keeps the editor mounted, but a revision view replaces it.
|
||||
const isLeaving =
|
||||
!parseDocumentSlug(nextLocation.pathname)?.endsWith(document.urlId) ||
|
||||
!!matchPath<{ revisionId?: string }>(
|
||||
nextLocation.pathname,
|
||||
matchDocumentHistory
|
||||
)?.params.revisionId;
|
||||
if (!isLeaving) {
|
||||
return true;
|
||||
}
|
||||
if (isUploading && !isEditorDirty) {
|
||||
return t(
|
||||
`Images are still uploading.\nAre you sure you want to discard them?`
|
||||
|
||||
@@ -158,41 +158,66 @@ describe("CollaborationProvider", () => {
|
||||
expect(events).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("reports when local persistence stops", () => {
|
||||
provider.destroy();
|
||||
const listeners = new Set<() => void>();
|
||||
const localProvider = {
|
||||
stopped: false,
|
||||
onStop: (listener: () => void) => {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
},
|
||||
};
|
||||
provider = new CollaborationProvider({
|
||||
url: "ws://localhost",
|
||||
name: "test",
|
||||
document: doc,
|
||||
connect: false,
|
||||
localProvider,
|
||||
});
|
||||
provider.on("syncStateChange", (event: SyncStateEvent) => {
|
||||
events.push(event);
|
||||
});
|
||||
expect(provider.hasLocalPersistence).toBe(true);
|
||||
describe("with local persistence", () => {
|
||||
let stopLocalProvider: () => void;
|
||||
|
||||
edit();
|
||||
expect(events.at(-1)).toEqual({
|
||||
hasUnsyncedChanges: true,
|
||||
hasLocalPersistence: true,
|
||||
beforeEach(() => {
|
||||
provider.destroy();
|
||||
const listeners = new Set<() => void>();
|
||||
stopLocalProvider = () => listeners.forEach((listener) => listener());
|
||||
provider = new CollaborationProvider({
|
||||
url: "ws://localhost",
|
||||
name: "test",
|
||||
document: doc,
|
||||
connect: false,
|
||||
localProvider: {
|
||||
stopped: false,
|
||||
onStop: (listener: () => void) => {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
provider.on("syncStateChange", (event: SyncStateEvent) => {
|
||||
events.push(event);
|
||||
});
|
||||
});
|
||||
|
||||
listeners.forEach((listener) => listener());
|
||||
expect(provider.hasLocalPersistence).toBe(false);
|
||||
expect(events.at(-1)).toEqual({
|
||||
hasUnsyncedChanges: true,
|
||||
hasLocalPersistence: false,
|
||||
it("does not report edits made before the first sync", () => {
|
||||
expect(provider.hasLocalPersistence).toBe(true);
|
||||
|
||||
edit();
|
||||
|
||||
expect(provider.hasPendingChanges).toBe(false);
|
||||
expect(events).toEqual([]);
|
||||
});
|
||||
|
||||
it("reports edits made after the connection drops", () => {
|
||||
provider.synced = true;
|
||||
provider.synced = false;
|
||||
|
||||
edit();
|
||||
|
||||
expect(provider.hasPendingChanges).toBe(true);
|
||||
expect(events.at(-1)).toEqual({
|
||||
hasUnsyncedChanges: true,
|
||||
hasLocalPersistence: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("reports edits made before the first sync when local persistence stops", () => {
|
||||
edit();
|
||||
expect(provider.hasPendingChanges).toBe(false);
|
||||
|
||||
stopLocalProvider();
|
||||
|
||||
expect(provider.hasLocalPersistence).toBe(false);
|
||||
expect(events.at(-1)).toEqual({
|
||||
hasUnsyncedChanges: true,
|
||||
hasLocalPersistence: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -114,6 +114,9 @@ export class CollaborationProvider extends HocuspocusProvider {
|
||||
/** Whether an edit was made since the server last confirmed a full sync. */
|
||||
private editedSinceSync = false;
|
||||
|
||||
/** Whether the server has confirmed a full sync since the provider was created. */
|
||||
private hasSyncedOnce = false;
|
||||
|
||||
private unsyncedTimeout?: ReturnType<typeof setTimeout>;
|
||||
|
||||
private handleDocumentUpdate = (_update: Uint8Array, origin: unknown) => {
|
||||
@@ -134,6 +137,7 @@ export class CollaborationProvider extends HocuspocusProvider {
|
||||
});
|
||||
if (state) {
|
||||
this.unsyncedChanges = 0;
|
||||
this.hasSyncedOnce = true;
|
||||
}
|
||||
this.updateSyncState();
|
||||
};
|
||||
@@ -145,18 +149,14 @@ export class CollaborationProvider extends HocuspocusProvider {
|
||||
if (this.synced && !this.hasUnsyncedChanges) {
|
||||
this.editedSinceSync = false;
|
||||
}
|
||||
const hasUnsyncedChanges = this.synced
|
||||
? this.hasUnsyncedChanges
|
||||
: this.editedSinceSync;
|
||||
const hasUnsyncedChanges = this.computeUnsyncedChanges();
|
||||
|
||||
// A connected server echoes each update within milliseconds, so only
|
||||
// report changes as unsynced once they have been pending for a while.
|
||||
if (hasUnsyncedChanges && this.synced) {
|
||||
this.unsyncedTimeout ??= setTimeout(() => {
|
||||
this.unsyncedTimeout = undefined;
|
||||
this.emitSyncState(
|
||||
this.synced ? this.hasUnsyncedChanges : this.editedSinceSync
|
||||
);
|
||||
this.emitSyncState(this.computeUnsyncedChanges());
|
||||
}, unsyncedGracePeriod);
|
||||
return;
|
||||
}
|
||||
@@ -164,6 +164,17 @@ export class CollaborationProvider extends HocuspocusProvider {
|
||||
this.emitSyncState(hasUnsyncedChanges);
|
||||
};
|
||||
|
||||
private computeUnsyncedChanges() {
|
||||
if (this.synced) {
|
||||
return this.hasUnsyncedChanges;
|
||||
}
|
||||
// Edits before the first sync are kept locally and are mostly load repairs.
|
||||
if (!this.hasSyncedOnce && this.localPersistence) {
|
||||
return false;
|
||||
}
|
||||
return this.editedSinceSync;
|
||||
}
|
||||
|
||||
private emitSyncState(hasUnsyncedChanges: boolean) {
|
||||
const next: SyncStateEvent = {
|
||||
hasUnsyncedChanges,
|
||||
|
||||
Reference in new issue
Block a user