From 1c0a8e00a635c59495b128cef25c863482f16962 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Mon, 11 Mar 2024 14:33:25 +0500 Subject: [PATCH] mobile: add useNoteLocked hook --- apps/mobile/app/hooks/use-db-item.ts | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/apps/mobile/app/hooks/use-db-item.ts b/apps/mobile/app/hooks/use-db-item.ts index ebe9c8d5c..d6e3dd94d 100644 --- a/apps/mobile/app/hooks/use-db-item.ts +++ b/apps/mobile/app/hooks/use-db-item.ts @@ -120,6 +120,40 @@ export const useDBItem = ( ]; }; +export const useNoteLocked = (noteId: string | undefined) => { + const [locked, setLocked] = useState(false); + + useEffect(() => { + if (!noteId) return; + db.vaults + .itemExists({ + type: "note", + id: noteId + }) + .then((exists) => { + setLocked(exists); + }); + + const sub = eSubscribeEvent(eDBItemUpdate, (id: string) => { + if (id === noteId) { + db.vaults + .itemExists({ + type: "note", + id: noteId + }) + .then((exists) => { + setLocked(exists); + }); + } + }); + return () => { + sub?.unsubscribe(); + }; + }, [noteId]); + + return locked; +}; + export const useTotalNotes = (type: "notebook" | "tag" | "color") => { const [totalNotesById, setTotalNotesById] = useState<{ [id: string]: number;