From f517dfdf1d1d6caefdf2e961435354d63b5ec93a Mon Sep 17 00:00:00 2001 From: thecodrr Date: Wed, 9 Sep 2020 11:56:36 +0500 Subject: [PATCH] fix: auto delete empty note (#148) --- apps/web/package.json | 2 +- apps/web/src/app.js | 15 ++++++++++++--- apps/web/src/components/editor/header.js | 7 +++---- .../components/editor/modules/markdown/index.js | 2 +- apps/web/src/stores/note-store.js | 9 +++++++++ apps/web/yarn.lock | 2 +- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/apps/web/package.json b/apps/web/package.json index 4cba31503..99ea34974 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -66,4 +66,4 @@ "last 4 edge version" ] } -} \ No newline at end of file +} diff --git a/apps/web/src/app.js b/apps/web/src/app.js index 5fc82e5dd..5e58cc8a9 100644 --- a/apps/web/src/app.js +++ b/apps/web/src/app.js @@ -4,27 +4,30 @@ import { Flex, Box } from "rebass"; import ThemeProvider from "./components/theme-provider"; import { usePersistentState } from "./utils/hooks"; import { useStore } from "./stores/app-store"; -import { useStore as useAppStore } from "./stores/app-store"; import { useStore as useEditorStore } from "./stores/editor-store"; import { useStore as useUserStore } from "./stores/user-store"; +import { useStore as useNotesStore } from "./stores/note-store"; import Animated from "./components/animated"; import NavigationMenu from "./components/navigationmenu"; import NavigationContainer from "./navigation/container"; import RootNavigator from "./navigation/navigators/rootnavigator"; import EditorNavigator from "./navigation/navigators/editornavigator"; import { isMobile } from "./utils/dimensions"; +import { db } from "./common"; function App() { const [show, setShow] = usePersistentState("isContainerVisible", true); const refreshColors = useStore((store) => store.refreshColors); - const isFocusMode = useAppStore((store) => store.isFocusMode); + const isFocusMode = useStore((store) => store.isFocusMode); const initUser = useUserStore((store) => store.init); + const initNotes = useNotesStore((store) => store.init); const openLastSession = useEditorStore((store) => store.openLastSession); useEffect(() => { refreshColors(); initUser(); - }, [refreshColors, initUser]); + initNotes(); + }, [refreshColors, initUser, initNotes]); useEffect(() => { if (isFocusMode) { @@ -47,6 +50,12 @@ function App() { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + useEffect(() => { + return () => { + db.ev.unsubscribeAll(); + }; + }, []); + return ( diff --git a/apps/web/src/components/editor/header.js b/apps/web/src/components/editor/header.js index 46d7fd7b9..ed5ef9943 100644 --- a/apps/web/src/components/editor/header.js +++ b/apps/web/src/components/editor/header.js @@ -64,9 +64,8 @@ function Header() { sx={{ display: "flex", alignItems: "center", - marginTop: dateEdited || text.length || id.length ? 0 : [0, 2, 2], - marginBottom: - dateEdited || text.length || id.length ? [1, 2, 2] : 0, + marginTop: dateEdited || text?.length || id ? 0 : [0, 2, 2], + marginBottom: dateEdited || text?.length || id ? [1, 2, 2] : 0, }} > {dateEdited > 0 ? ( @@ -81,7 +80,7 @@ function Header() { ) : null} - {id && id.length > 0 ? <>{isSaving ? "Saving" : "Saved"} : null} + {id ? <>{isSaving ? "Saving" : "Saved"} : null} { + db.ev.subscribe("notes:removeEmptyNote", (id) => { + const { session, newSession } = editorStore.get(); + if (session.id === id) { + newSession(); + } + }); + }; + setSelectedNote = (id) => { this.set((state) => (state.selectedNote = id)); }; diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock index 6817fcf4c..53d67d078 100644 --- a/apps/web/yarn.lock +++ b/apps/web/yarn.lock @@ -7319,7 +7319,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1: "notes-core@git+ssh://git@github.com:streetwriters/notesnook-core.git": version "1.5.0" - resolved "git+ssh://git@github.com:streetwriters/notesnook-core.git#9e4abf6e98d98183bada040f9279989959f1bd14" + resolved "git+ssh://git@github.com:streetwriters/notesnook-core.git#85b016c0756aaeeab9487de9068027edd40a075d" dependencies: crypto-random-string "^3.2.0" event-source-polyfill "^1.0.16"