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: preserve drafts across loading and in-flight sign out

Tin7dd3bd690b8d
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 fa149b350..1626edf96 100644
--- a/apps/web/src/components/task/task-description-save.test.tsx
+++ b/apps/web/src/components/task/task-description-save.test.tsx
@@ -309,3 +309,43 @@ describe("TaskDescription pending saves", () => {
expect(savedTaskIds().sort()).toEqual(["task-a", "task-b"]);
});
});
+
+it("restores a queued draft before uncached task data arrives", async () => {
+ const { descriptionSaveQueue } = await import("@/lib/description-save-queue");
+ descriptionSaveQueue.schedule("uncached", "retained draft", async () => {
+ throw new Error("offline");
+ });
+ const view = render(<TaskDescription taskId="uncached" />);
+ await waitFor(() =>
+ expect(view.container.textContent).toContain("retained draft"),
+ );
+ mocks.tasks.set("uncached", {
+ id: "uncached",
+ projectId: "p",
+ description: "server content",
+ });
+ view.rerender(<TaskDescription taskId="uncached" />);
+ expect(view.container.textContent).toContain("retained draft");
+ expect(view.container.textContent).not.toContain("server content");
+ view.unmount();
+ descriptionSaveQueue.clear();
+});
+
+it("disables description editing while sign-out owns the save pause", async () => {
+ const { descriptionSaveQueue } = await import("@/lib/description-save-queue");
+ mocks.tasks.set("paused", {
+ id: "paused",
+ projectId: "p",
+ description: "saved",
+ });
+ const view = render(<TaskDescription taskId="paused" />);
+ await settle();
+ let resume!: () => void;
+ act(() => {
+ resume = descriptionSaveQueue.pause("");
+ });
+ expect(latestEditor().isEditable).toBe(false);
+ act(() => resume());
+ expect(latestEditor().isEditable).toBe(true);
+ view.unmount();
+});
diff --git a/apps/web/src/components/task/task-description.tsx b/apps/web/src/components/task/task-description.tsx
index c1439c087..393bbf545 100644
--- a/apps/web/src/components/task/task-description.tsx
+++ b/apps/web/src/components/task/task-description.tsx
@@ -301,7 +301,11 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
const { data: task } = useGetTask(taskId);
const { mutateAsync: updateTaskDescription } = useUpdateTaskDescription();
const { canUpdateTasks } = useWorkspacePermission();
- const canEdit = canUpdateTasks();
+ const ownerId = useContext(AuthContext).user?.id ?? "";
+ const savesPaused = useSyncExternalStore(descriptionSaveQueue.subscribe, () =>
+ descriptionSaveQueue.isPaused(ownerId),
+ );
+ const canEdit = canUpdateTasks() && !savesPaused;
const canEditRef = useRef(canEdit);
canEditRef.current = canEdit;
@@ -608,7 +612,6 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
};
}, []);
- const ownerId = useContext(AuthContext).user?.id ?? "";
const ownerIdRef = useRef(ownerId);
ownerIdRef.current = ownerId;
const saveState = useSyncExternalStore(
@@ -849,7 +852,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
// user can't update tasks, the description renders as read-only: slash
// menus, paste handlers, and toolbar buttons all become no-ops because
// the editor refuses content mutations.
- useEffect(() => {
+ useLayoutEffect(() => {
if (!editor) return;
editor.setEditable(canEdit);
}, [editor, canEdit]);
@@ -1052,7 +1055,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {
const pendingSave = descriptionSaveQueue.get(taskId, ownerId);
const incomingMarkdown = formatMarkdown(
- task?.id && pendingSave && pendingSave.state !== "saved"
+ pendingSave && pendingSave.state !== "saved"
? pendingSave.value
: task?.description || "",
);
diff --git a/apps/web/src/hooks/mutations/use-sign-out.ts b/apps/web/src/hooks/mutations/use-sign-out.ts
index 85e3cf146..071776637 100644
--- a/apps/web/src/hooks/mutations/use-sign-out.ts
+++ b/apps/web/src/hooks/mutations/use-sign-out.ts
@@ -11,41 +11,46 @@ function useSignOut(idpLogoutUrl?: string | null) {
return useMutation({
mutationFn: async () => {
- if (!(await descriptionSaveQueue.drain(session?.user.id ?? "")))
- throw new Error(i18n.t("tasks:detail.editor.saveFailed"));
- let idToken: string | null = null;
+ const resume = descriptionSaveQueue.pause(session?.user.id ?? "");
+ try {
+ if (!(await descriptionSaveQueue.drain(session?.user.id ?? "")))
+ throw new Error(i18n.t("tasks:detail.editor.saveFailed"));
+ let idToken: string | null = null;
- if (idpLogoutUrl) {
- try {
- const data = await getIdToken();
- idToken = data.idToken;
- } catch {
- // If we can't get the id_token, proceed without it
+ if (idpLogoutUrl) {
+ try {
+ const data = await getIdToken();
+ idToken = data.idToken;
+ } catch {
+ // If we can't get the id_token, proceed without it
+ }
}
- }
- const result = await authClient.signOut({
- fetchOptions: {
- onSuccess: () => {
- descriptionSaveQueue.clear();
- if (idpLogoutUrl) {
- const redirectUri = `${window.location.origin}/auth/sign-in`;
- const url = new URL(idpLogoutUrl);
- url.searchParams.set("post_logout_redirect_uri", redirectUri);
- if (idToken) {
- url.searchParams.set("id_token_hint", idToken);
+ const result = await authClient.signOut({
+ fetchOptions: {
+ onSuccess: () => {
+ descriptionSaveQueue.clear();
+ if (idpLogoutUrl) {
+ const redirectUri = `${window.location.origin}/auth/sign-in`;
+ const url = new URL(idpLogoutUrl);
+ url.searchParams.set("post_logout_redirect_uri", redirectUri);
+ if (idToken) {
+ url.searchParams.set("id_token_hint", idToken);
+ }
+ window.location.href = url.toString();
+ } else {
+ navigate({ to: "/auth/sign-in" });
}
- window.location.href = url.toString();
- } else {
- navigate({ to: "/auth/sign-in" });
- }
+ },
},
- },
- });
- if (result.error) {
- throw new Error(result.error.message);
+ });
+ if (result.error) {
+ throw new Error(result.error.message);
+ }
+ return result.data;
+ } finally {
+ resume();
}
- return result.data;
},
});
}
diff --git a/apps/web/src/lib/description-save-queue.ts b/apps/web/src/lib/description-save-queue.ts
index fd6861d1e..3b0b49ec3 100644
--- a/apps/web/src/lib/description-save-queue.ts
+++ b/apps/web/src/lib/description-save-queue.ts
@@ -15,6 +15,7 @@ type Entry = {
export function createDescriptionSaveQueue(delay = 700) {
const entries = new Map<string, Entry>();
const listeners = new Set<() => void>();
+ const pausedOwners = new Map<string, number>();
const notify = () => listeners.forEach((listener) => listener());
const run = async (id: string) => {
const entry = entries.get(id);
@@ -61,6 +62,19 @@ export function createDescriptionSaveQueue(delay = 700) {
return promise;
};
return {
+ isPaused(ownerId: string) {
+ return pausedOwners.has(ownerId);
+ },
+ pause(ownerId: string) {
+ pausedOwners.set(ownerId, (pausedOwners.get(ownerId) ?? 0) + 1);
+ notify();
+ return () => {
+ const count = (pausedOwners.get(ownerId) ?? 1) - 1;
+ if (count) pausedOwners.set(ownerId, count);
+ else pausedOwners.delete(ownerId);
+ notify();
+ };
+ },
async drain(ownerId: string) {
while (true) {
const pending = [...entries.entries()].filter(
@@ -76,6 +90,7 @@ export function createDescriptionSaveQueue(delay = 700) {
}
},
schedule(id: string, value: string, save: Entry["save"], ownerId = "") {
+ if (pausedOwners.has(ownerId)) return;
const current = entries.get(id);
if (current?.timer && current.ownerId !== ownerId)
clearTimeout(current.timer);