Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(editor): serialize description saves and allow retry

Tin81e265ea1584
diff --git a/apps/web/src/components/task/task-description-save.test.tsx b/apps/web/src/components/task/task-description-save.test.tsx
index 556ec1091..fa149b350 100644
--- a/apps/web/src/components/task/task-description-save.test.tsx
+++ b/apps/web/src/components/task/task-description-save.test.tsx
@@ -1,4 +1,10 @@
-import { act, fireEvent, render, waitFor } from "@testing-library/react";
+import {
+ act,
+ cleanup,
+ fireEvent,
+ render,
+ waitFor,
+} from "@testing-library/react";
import { Extension } from "@tiptap/core";
import TaskItem from "@tiptap/extension-task-item";
import { EditorView } from "@tiptap/pm/view";
@@ -169,6 +175,7 @@ beforeEach(() => {
});
afterEach(() => {
+ cleanup();
vi.useRealTimers();
});
@@ -267,7 +274,7 @@ describe("TaskDescription pending saves", () => {
await waitFor(() => expect(container.textContent).toContain("alpha"));
await settle();
- latestEditor().commands.insertContent(" edited in a");
+ act(() => latestEditor().commands.insertContent(" edited in a"));
expect(mocks.mutateAsync).not.toHaveBeenCalled();
rerender(<TaskDescription taskId="task-b" />);
@@ -287,13 +294,13 @@ describe("TaskDescription pending saves", () => {
await waitFor(() => expect(container.textContent).toContain("alpha"));
await settle();
- latestEditor().commands.insertContent(" edited in a");
+ act(() => latestEditor().commands.insertContent(" edited in a"));
rerender(<TaskDescription taskId="task-b" />);
await waitFor(() => expect(container.textContent).toContain("bravo"));
await settle();
- latestEditor().commands.insertContent(" edited in b");
+ act(() => latestEditor().commands.insertContent(" edited in b"));
await vi.waitFor(() => expect(mocks.mutateAsync).toHaveBeenCalledTimes(2), {
timeout: DEBOUNCE_MS * 4,
diff --git a/apps/web/src/components/task/task-description.tsx b/apps/web/src/components/task/task-description.tsx
index 8ec2c25c5..09cb6abff 100644
--- a/apps/web/src/components/task/task-description.tsx
+++ b/apps/web/src/components/task/task-description.tsx
@@ -44,6 +44,7 @@ import {
useMemo,
useRef,
useState,
+ useSyncExternalStore,
} from "react";
import { useTranslation } from "react-i18next";
import type { Highlighter } from "shiki";
@@ -62,6 +63,7 @@ import { useUpdateTaskDescription } from "@/hooks/mutations/task/use-update-task
import useGetTask from "@/hooks/queries/task/use-get-task";
import { useWorkspacePermission } from "@/hooks/use-workspace-permission";
import { cn } from "@/lib/cn";
+import { descriptionSaveQueue } from "@/lib/description-save-queue";
import {
extractIssueKeyFromUrl,
extractTaskIdFromUrl,
@@ -116,8 +118,6 @@ type SlashMenuState = {
selectedIndex: number;
};
-const DESCRIPTION_SAVE_DEBOUNCE_MS = 700;
-
function formatMarkdown(markdown: string) {
return markdown
.replace(/\r\n/g, "\n")
@@ -606,40 +606,16 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
};
}, []);
- const pendingDescriptionSavesRef = useRef(
- new Map<string, ReturnType<typeof setTimeout>>(),
+ const saveState = useSyncExternalStore(
+ descriptionSaveQueue.subscribe,
+ () => descriptionSaveQueue.get(taskId)?.state ?? "saved",
);
-
const scheduleDescriptionSave = useCallback((markdown: string) => {
if (!canEditRef.current) return;
-
const editedTask = taskRef.current;
if (!editedTask) return;
-
- const timers = pendingDescriptionSavesRef.current;
- const pending = timers.get(editedTask.id);
- if (pending) clearTimeout(pending);
-
- timers.set(
- editedTask.id,
- setTimeout(async () => {
- timers.delete(editedTask.id);
-
- const updateTaskFn = updateTaskRef.current;
- if (!updateTaskFn) return;
-
- const latestTask = taskRef.current;
- const base = latestTask?.id === editedTask.id ? latestTask : editedTask;
-
- try {
- await updateTaskFn({
- ...base,
- description: markdown,
- });
- } catch (error) {
- console.error("Failed to update description:", error);
- }
- }, DESCRIPTION_SAVE_DEBOUNCE_MS),
+ descriptionSaveQueue.schedule(editedTask.id, markdown, (description) =>
+ updateTaskRef.current({ ...editedTask, description }),
);
}, []);
@@ -833,7 +809,12 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
},
},
onUpdate: ({ editor: activeEditor }) => {
- if (!canEditRef.current || isSyncingExternalContentRef.current) return;
+ if (
+ !isMountedRef.current ||
+ !canEditRef.current ||
+ isSyncingExternalContentRef.current
+ )
+ return;
const markdown = formatMarkdown(activeEditor.getMarkdown());
if (markdown === latestSyncedMarkdownRef.current) return;
latestSyncedMarkdownRef.current = markdown;
@@ -1055,7 +1036,12 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
latestSyncedMarkdownRef.current = "";
}
- const incomingMarkdown = formatMarkdown(task?.description || "");
+ const pendingSave = descriptionSaveQueue.get(taskId);
+ const incomingMarkdown = formatMarkdown(
+ task?.id && pendingSave && pendingSave.state !== "saved"
+ ? pendingSave.value
+ : task?.description || "",
+ );
if (!hasHydratedRef.current) {
isSyncingExternalContentRef.current = true;
latestSyncedMarkdownRef.current = incomingMarkdown;
@@ -1070,7 +1056,8 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
return;
}
- if (editor.isFocused) return;
+ if (editor.isFocused || (pendingSave && pendingSave.state !== "saved"))
+ return;
if (incomingMarkdown === latestSyncedMarkdownRef.current) return;
isSyncingExternalContentRef.current = true;
@@ -1079,7 +1066,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
requestAnimationFrame(() => {
isSyncingExternalContentRef.current = false;
});
- }, [editor, taskId, task?.description]);
+ }, [editor, taskId, task?.id, task?.description]);
useEffect(() => {
if (!editor) return;
@@ -1429,6 +1416,28 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
onDragLeave={handleShellDragLeave}
onDrop={handleShellDrop}
>
+ {canEdit && (
+ <div
+ role="status"
+ aria-live="polite"
+ className="flex items-center gap-2 text-xs text-muted-foreground"
+ >
+ {saveState === "failed"
+ ? t("tasks:detail.editor.saveFailed")
+ : saveState === "saved"
+ ? t("tasks:detail.editor.saved")
+ : t("tasks:detail.editor.saving")}
+ {saveState === "failed" && (
+ <Button
+ size="xs"
+ variant="ghost"
+ onClick={() => descriptionSaveQueue.retry(taskId)}
+ >
+ {t("tasks:detail.editor.retrySave")}
+ </Button>
+ )}
+ </div>
+ )}
<input
ref={imageInputRef}
type="file"
diff --git a/apps/web/src/lib/description-save-queue.test.ts b/apps/web/src/lib/description-save-queue.test.ts
new file mode 100644
index 000000000..78a4cc1a0
--- /dev/null
+++ b/apps/web/src/lib/description-save-queue.test.ts
@@ -0,0 +1,50 @@
+import { afterEach, describe, expect, it, vi } from "vite-plus/test";
+import { createDescriptionSaveQueue } from "./description-save-queue";
+
+afterEach(() => vi.useRealTimers());
+
+describe("description save queue", () => {
+ it("serializes requests and coalesces edits made while a save is running", async () => {
+ vi.useFakeTimers();
+ let finish!: () => void;
+ const save = vi
+ .fn()
+ .mockImplementationOnce(
+ () =>
+ new Promise<void>((resolve) => {
+ finish = resolve;
+ }),
+ )
+ .mockResolvedValue(undefined);
+ const queue = createDescriptionSaveQueue(10);
+ queue.schedule("task", "old", save);
+ await vi.advanceTimersByTimeAsync(10);
+ queue.schedule("task", "middle", save);
+ queue.schedule("task", "newest", save);
+ await vi.advanceTimersByTimeAsync(20);
+ expect(save.mock.calls).toEqual([["old"]]);
+ finish();
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(save.mock.calls).toEqual([["old"], ["newest"]]);
+ expect(queue.get("task")).toBeUndefined();
+ });
+
+ it("retains failed content for a visible retry and keeps task queues independent", async () => {
+ vi.useFakeTimers();
+ const save = vi
+ .fn()
+ .mockRejectedValueOnce(new Error("offline"))
+ .mockResolvedValue(undefined);
+ const queue = createDescriptionSaveQueue(10);
+ queue.schedule("a", "unsaved", save);
+ queue.schedule("b", "other", vi.fn().mockResolvedValue(undefined));
+ await vi.advanceTimersByTimeAsync(10);
+ expect(queue.get("a")).toMatchObject({ value: "unsaved", state: "failed" });
+ expect(queue.get("b")).toBeUndefined();
+ queue.retry("a");
+ await Promise.resolve();
+ expect(save.mock.calls).toEqual([["unsaved"], ["unsaved"]]);
+ expect(queue.get("a")).toBeUndefined();
+ });
+});
diff --git a/apps/web/src/lib/description-save-queue.ts b/apps/web/src/lib/description-save-queue.ts
new file mode 100644
index 000000000..ce6970622
--- /dev/null
+++ b/apps/web/src/lib/description-save-queue.ts
@@ -0,0 +1,75 @@
+export type DescriptionSaveState = "saved" | "saving" | "pending" | "failed";
+
+type Entry = {
+ value: string;
+ save: (value: string) => Promise<unknown>;
+ state: DescriptionSaveState;
+ timer?: ReturnType<typeof setTimeout>;
+ running: boolean;
+ version: number;
+};
+
+/** One request per task, with pending edits coalesced to the latest value. */
+export function createDescriptionSaveQueue(delay = 700) {
+ const entries = new Map<string, Entry>();
+ const listeners = new Set<() => void>();
+ const notify = () => listeners.forEach((listener) => listener());
+ const flush = async (id: string) => {
+ const entry = entries.get(id);
+ if (!entry || entry.running) return;
+ if (entry.timer) clearTimeout(entry.timer);
+ entry.timer = undefined;
+ entry.running = true;
+ entry.state = "saving";
+ const version = entry.version;
+ notify();
+ try {
+ await entry.save(entry.value);
+ entry.running = false;
+ if (entry.version !== version) {
+ void flush(id);
+ } else {
+ entries.delete(id);
+ notify();
+ }
+ } catch {
+ entry.running = false;
+ entry.state = "failed";
+ notify();
+ }
+ };
+ return {
+ schedule(id: string, value: string, save: Entry["save"]) {
+ const previous = entries.get(id);
+ if (previous?.timer) clearTimeout(previous.timer);
+ const entry = previous ?? {
+ value,
+ save,
+ state: "pending",
+ running: false,
+ version: 0,
+ };
+ entry.value = value;
+ entry.save = save;
+ entry.version += 1;
+ if (!entry.running) entry.state = "pending";
+ entry.timer = setTimeout(() => void flush(id), delay);
+ entries.set(id, entry);
+ notify();
+ },
+ retry(id: string) {
+ void flush(id);
+ },
+ get(id: string) {
+ return entries.get(id);
+ },
+ subscribe(listener: () => void) {
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ };
+ },
+ };
+}
+
+export const descriptionSaveQueue = createDescriptionSaveQueue();
diff --git a/i18n/az-AZ.json b/i18n/az-AZ.json
index ecdacb575..8f3e6e6c9 100644
--- a/i18n/az-AZ.json
+++ b/i18n/az-AZ.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/de-DE.json b/i18n/de-DE.json
index acc98feac..8741abd7e 100644
--- a/i18n/de-DE.json
+++ b/i18n/de-DE.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Gespeichert",
+ "saving": "Wird gespeichert…",
+ "saveFailed": "Speichern fehlgeschlagen",
+ "retrySave": "Erneut speichern"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/el-GR.json b/i18n/el-GR.json
index 1f256b198..0b3decb1f 100644
--- a/i18n/el-GR.json
+++ b/i18n/el-GR.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/en-US.json b/i18n/en-US.json
index 6db38721b..994296a94 100644
--- a/i18n/en-US.json
+++ b/i18n/en-US.json
@@ -2297,7 +2297,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
}
},
"entity": {
diff --git a/i18n/es-ES.json b/i18n/es-ES.json
index 3e2f98adc..fb39cdaf8 100644
--- a/i18n/es-ES.json
+++ b/i18n/es-ES.json
@@ -2305,7 +2305,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/fr-FR.json b/i18n/fr-FR.json
index 6f1f97e99..bf591b7aa 100644
--- a/i18n/fr-FR.json
+++ b/i18n/fr-FR.json
@@ -2305,7 +2305,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/hi-IN.json b/i18n/hi-IN.json
index 50ed4b8e1..7d83edafc 100644
--- a/i18n/hi-IN.json
+++ b/i18n/hi-IN.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/id-ID.json b/i18n/id-ID.json
index e8204be15..cc9230c08 100644
--- a/i18n/id-ID.json
+++ b/i18n/id-ID.json
@@ -2283,7 +2283,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/it-IT.json b/i18n/it-IT.json
index d6757f4ea..81d429175 100644
--- a/i18n/it-IT.json
+++ b/i18n/it-IT.json
@@ -2305,7 +2305,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/ja-JP.json b/i18n/ja-JP.json
index 9bd196d1b..48b2cfdde 100644
--- a/i18n/ja-JP.json
+++ b/i18n/ja-JP.json
@@ -2283,7 +2283,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/ko-KR.json b/i18n/ko-KR.json
index c11853e5c..435b48f04 100644
--- a/i18n/ko-KR.json
+++ b/i18n/ko-KR.json
@@ -2283,7 +2283,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/mk-MK.json b/i18n/mk-MK.json
index 3f9b59ddb..fc25bc88f 100644
--- a/i18n/mk-MK.json
+++ b/i18n/mk-MK.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/nl-NL.json b/i18n/nl-NL.json
index b74b33a17..7ed79a8d9 100644
--- a/i18n/nl-NL.json
+++ b/i18n/nl-NL.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/pl-PL.json b/i18n/pl-PL.json
index b4d4d926d..a779b5eb1 100644
--- a/i18n/pl-PL.json
+++ b/i18n/pl-PL.json
@@ -2579,7 +2579,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"noActivity": "Nie znaleziono aktywności",
"openInFullPage": "Otwórz na pełnej stronie",
diff --git a/i18n/pt-BR.json b/i18n/pt-BR.json
index d91b738f4..4502434d8 100644
--- a/i18n/pt-BR.json
+++ b/i18n/pt-BR.json
@@ -2305,7 +2305,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/ru-RU.json b/i18n/ru-RU.json
index 4127b0a76..3f245c754 100644
--- a/i18n/ru-RU.json
+++ b/i18n/ru-RU.json
@@ -2324,7 +2324,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/tr-TR.json b/i18n/tr-TR.json
index b3affd384..d6c6583f9 100644
--- a/i18n/tr-TR.json
+++ b/i18n/tr-TR.json
@@ -2294,7 +2294,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/uk-UA.json b/i18n/uk-UA.json
index 8a0d1daae..7b4237b68 100644
--- a/i18n/uk-UA.json
+++ b/i18n/uk-UA.json
@@ -2316,7 +2316,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/vi-VN.json b/i18n/vi-VN.json
index 40a078eee..22841c72b 100644
--- a/i18n/vi-VN.json
+++ b/i18n/vi-VN.json
@@ -2283,7 +2283,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"
diff --git a/i18n/zh-CN.json b/i18n/zh-CN.json
index 5cf3d45f4..19e270e4f 100644
--- a/i18n/zh-CN.json
+++ b/i18n/zh-CN.json
@@ -2283,7 +2283,11 @@
"reset": "Reset size",
"resizeHandle": "Resize image",
"small": "Small"
- }
+ },
+ "saved": "Saved",
+ "saving": "Saving…",
+ "saveFailed": "Changes could not be saved",
+ "retrySave": "Retry"
},
"customFieldUpdated": "Field updated",
"selectOption": "Select option"