Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix: prevent stale comment drafts from being restored

memoclawe520b637fd8d
diff --git a/web/src/components/MemoEditor/services/cacheService.ts b/web/src/components/MemoEditor/services/cacheService.ts
index bc311952e..5c93e3b4f 100644
--- a/web/src/components/MemoEditor/services/cacheService.ts
+++ b/web/src/components/MemoEditor/services/cacheService.ts
@@ -1,25 +1,49 @@
-import { debounce } from "lodash-es";
-
export const CACHE_DEBOUNCE_DELAY = 500;
+const pendingSaves = new Map<string, ReturnType<typeof window.setTimeout>>();
+
export const cacheService = {
key: (username: string, cacheKey?: string): string => {
return `${username}-${cacheKey || ""}`;
},
- save: debounce((key: string, content: string) => {
- if (content.trim()) {
- localStorage.setItem(key, content);
- } else {
- localStorage.removeItem(key);
+ save: (key: string, content: string) => {
+ const pendingSave = pendingSaves.get(key);
+ if (pendingSave) {
+ window.clearTimeout(pendingSave);
}
- }, CACHE_DEBOUNCE_DELAY),
+
+ const timeoutId = window.setTimeout(() => {
+ pendingSaves.delete(key);
+
+ if (content.trim()) {
+ localStorage.setItem(key, content);
+ } else {
+ localStorage.removeItem(key);
+ }
+ }, CACHE_DEBOUNCE_DELAY);
+
+ pendingSaves.set(key, timeoutId);
+ },
load(key: string): string {
return localStorage.getItem(key) || "";
},
clear(key: string): void {
+ const pendingSave = pendingSaves.get(key);
+ if (pendingSave) {
+ window.clearTimeout(pendingSave);
+ pendingSaves.delete(key);
+ }
+
localStorage.removeItem(key);
},
+
+ clearAll(): void {
+ for (const timeoutId of pendingSaves.values()) {
+ window.clearTimeout(timeoutId);
+ }
+ pendingSaves.clear();
+ },
};