Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(reading): restore deleted annotations without replacing current scope

dajiaohuangc29b666756a0
diff --git a/web/context/ReadingContext.tsx b/web/context/ReadingContext.tsx
index 8518950df..ed5131d45 100644
--- a/web/context/ReadingContext.tsx
+++ b/web/context/ReadingContext.tsx
@@ -95,7 +95,11 @@ export function ReadingProvider({ children }: { children: ReactNode }) {
const [error, setErrorState] = useState<string | null>(null);
// Guards against a slow open landing after the user opened something else.
const openTokenRef = useRef(0);
- useEffect(() => () => { openTokenRef.current += 1; }, []);
+ const annotationScopeRef = useRef(0);
+ useEffect(() => () => {
+ openTokenRef.current += 1;
+ annotationScopeRef.current += 1;
+ }, []);
// Mirror the open document into the turn-state cell, which is what the chat
// reads when it sends. One effect rather than writes scattered through the
@@ -122,6 +126,7 @@ export function ReadingProvider({ children }: { children: ReactNode }) {
if (token !== openTokenRef.current) return false;
const marks = await listAnnotations(detail.material_id);
if (token !== openTokenRef.current) return false;
+ annotationScopeRef.current += 1;
setMaterial(detail);
setAnnotations(marks);
return true;
@@ -143,6 +148,7 @@ export function ReadingProvider({ children }: { children: ReactNode }) {
const closeMaterial = useCallback(() => {
// Bump the token so an open still in flight cannot resurrect the document.
openTokenRef.current += 1;
+ annotationScopeRef.current += 1;
setMaterial(null);
setAnnotations([]);
setErrorState(null);
@@ -183,19 +189,19 @@ export function ReadingProvider({ children }: { children: ReactNode }) {
async (annotation: AnnotationItem) => {
const materialId = material?.material_id;
if (!materialId) return;
- let previous: AnnotationItem[] = [];
- setAnnotations((current) => {
- previous = current;
- return current.filter(
- (row) => row.annotation_id !== annotation.annotation_id,
- );
- });
+ const scope = annotationScopeRef.current;
+ setAnnotations((current) => current.filter(
+ (row) => row.annotation_id !== annotation.annotation_id,
+ ));
try {
await deleteAnnotationApi(materialId, annotation.annotation_id);
} catch {
+ if (scope !== annotationScopeRef.current) return;
// Put it back: pretending a failed delete succeeded would lose the mark
// on the next reload.
- setAnnotations(previous);
+ setAnnotations((current) => current.some(
+ (row) => row.annotation_id === annotation.annotation_id,
+ ) ? current : [...current, annotation]);
setErrorState("That annotation could not be removed.");
}
},
diff --git a/web/tests/reading-annotation-rollback.spec.tsx b/web/tests/reading-annotation-rollback.spec.tsx
new file mode 100644
index 000000000..3575b5d09
--- /dev/null
+++ b/web/tests/reading-annotation-rollback.spec.tsx
@@ -0,0 +1,55 @@
+import React from "react";
+import { act, render } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import { ReadingProvider, useReading } from "@/context/ReadingContext";
+import type { AnnotationItem, MaterialDetail } from "@/lib/reading-api";
+
+const api = vi.hoisted(() => ({ fetch: vi.fn() }));
+vi.mock("@/lib/api", () => ({ apiFetch: api.fetch, apiUrl: (url: string) => url }));
+
+let context: ReturnType<typeof useReading>;
+function Probe() {
+ context = useReading();
+ return null;
+}
+
+function mark(id: string): AnnotationItem {
+ return { annotation_id: id, locator: 1, kind: "highlight", quote: id } as AnnotationItem;
+}
+
+async function open(id: string, marks: AnnotationItem[]) {
+ api.fetch.mockResolvedValueOnce(new Response(JSON.stringify(marks)));
+ await act(async () => {
+ await context.openMaterial({ material_id: id, outline: [], revision: 1 } as unknown as MaterialDetail);
+ });
+}
+
+describe("Reading annotation deletion rollback", () => {
+ it("keeps annotations added while a failed deletion is pending", async () => {
+ render(<ReadingProvider><Probe /></ReadingProvider>);
+ const original = mark("original");
+ await open("document-a", [original]);
+ let fail!: (error: Error) => void;
+ api.fetch.mockImplementationOnce(() => new Promise((_resolve, reject) => { fail = reject; }));
+ let pending!: Promise<void>;
+ act(() => { pending = context.removeMark(original); });
+ act(() => { context.mergeMark(mark("new")); });
+ await act(async () => { fail(new Error("Disconnected")); await pending; });
+ expect(context.annotations.map(row => row.annotation_id).sort()).toEqual(["new", "original"]);
+ });
+
+ it("does not restore the previous document's marks after opening another document", async () => {
+ render(<ReadingProvider><Probe /></ReadingProvider>);
+ const original = mark("original");
+ await open("document-a", [original]);
+ let fail!: (error: Error) => void;
+ api.fetch.mockImplementationOnce(() => new Promise((_resolve, reject) => { fail = reject; }));
+ let pending!: Promise<void>;
+ act(() => { pending = context.removeMark(original); });
+ await open("document-b", [mark("other-document")]);
+ await act(async () => { fail(new Error("Disconnected")); await pending; });
+ expect(context.material?.material_id).toBe("document-b");
+ expect(context.annotations.map(row => row.annotation_id)).toEqual(["other-document"]);
+ expect(context.error).toBeNull();
+ });
+});