From fc1965a67e20528ee24931ea0337c78835d29c4a Mon Sep 17 00:00:00 2001 From: Jeet Mandaliya <45892659+sereneinserenade@users.noreply.github.com> Date: Wed, 2 Mar 2022 00:38:56 +0100 Subject: [PATCH] implemented recycle bin --- src/pages/Newtab/Main.tsx | 53 ++++++++++++++----- src/pages/Newtab/Newtab.tsx | 3 +- src/pages/Newtab/components/MainTop.scss | 2 +- src/pages/Newtab/components/MainTop.tsx | 26 +++++---- src/pages/Newtab/components/Sidebar.scss | 18 +++++-- src/pages/Newtab/components/Sidebar.tsx | 25 +++++---- .../components/editor/EditorAreaContainer.tsx | 20 +++++-- src/pages/Newtab/components/editor/Tiptap.tsx | 11 ++-- 8 files changed, 111 insertions(+), 47 deletions(-) diff --git a/src/pages/Newtab/Main.tsx b/src/pages/Newtab/Main.tsx index 0960733..00443d7 100644 --- a/src/pages/Newtab/Main.tsx +++ b/src/pages/Newtab/Main.tsx @@ -5,7 +5,7 @@ import { useRecoilState, useRecoilValue } from 'recoil'; // import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.min.css'; -import { EditorAreaContainer, Maintop, Sidebar, Tiptap } from './components'; +import { EditorAreaContainer, Maintop, Sidebar } from './components'; import { notesState, activeNoteState, sidebarActiveState, binNotesState } from './Store'; import { Note } from './types'; import PlaceNoterLogo from '../../assets/img/logo.svg'; @@ -34,17 +34,46 @@ function Main() { useEffect(() => { if (!activeNote) return + const binNoteIndex = binNotes.findIndex((n) => n?.id === activeNote.id) + + const isInBin = binNoteIndex >= 0 + const noteIndex = notes.findIndex((n) => n?.id === activeNote.id) - const foundNote = notes[noteIndex] - if (activeNote?.content === foundNote?.content && activeNote?.title === foundNote?.title) return + if (isInBin) { + const foundNote = binNotes[binNoteIndex] + + if (activeNote?.content === foundNote?.content && activeNote?.title === foundNote?.title) return + + const copyOfNotes = [...binNotes.slice()] + + if (activeNote) { + copyOfNotes[binNoteIndex] = activeNote + setBinNotes(copyOfNotes) + } + + return + } else if (noteIndex >= 0) { + + const foundNote = notes[noteIndex] - const copyOfNotes = [...notes.slice()] + if (activeNote?.content === foundNote?.content && activeNote?.title === foundNote?.title) return - if (activeNote) { - copyOfNotes[noteIndex] = activeNote - setNotes(copyOfNotes) + const copyOfNotes = [...notes.slice()] + + if (activeNote) { + copyOfNotes[noteIndex] = activeNote + setNotes(copyOfNotes) + } + + return } + + const { id, title } = activeNote + + debugger + + console.error(`This activeNote is not inside \`notes\` and \`binNotes\` \n ${JSON.stringify({ id, title })}`) }, [activeNote]) const setNotesInLocalStorage = () => storage.local.set({ dbnotes: notes }) @@ -57,7 +86,7 @@ function Main() { const debouncedSetBinNotesInLocalStorage = debounce(setBinNotesInLocalStorage, 300) - useEffect(() => { binNotesFetchedFromDb && debouncedSetBinNotesInLocalStorage() }) + useEffect(() => { binNotesFetchedFromDb && debouncedSetBinNotesInLocalStorage() }, [binNotes]) const checkIfAnEmptyNoteExists = (): Note | undefined => { return notes.find((n) => n.title.trim() === "" && n.textContent.trim() === "") @@ -68,7 +97,6 @@ function Main() { if (emptyNoteInNotesList) { setActiveNote(undefined) setTimeout(() => setActiveNote(emptyNoteInNotesList)) - return } @@ -83,8 +111,8 @@ function Main() { setActiveNote(undefined) setTimeout(() => { - setActiveNote(JSON.parse(JSON.stringify(newNote))) setNotes([newNote, ...notes]) + setActiveNote(newNote) }) } @@ -104,8 +132,9 @@ function Main() { }) storage.local.get('binNotes', ({ binNotes }) => { - if (binNotes) setBinNotes(binNotes) - else storage.local.set({ binNotes: [] }) + if (binNotes) { + setBinNotes(binNotes) + } else storage.local.set({ binNotes: [] }) setBinNotesFetchedFromDb(true) }) diff --git a/src/pages/Newtab/Newtab.tsx b/src/pages/Newtab/Newtab.tsx index 5be8959..2e970ee 100644 --- a/src/pages/Newtab/Newtab.tsx +++ b/src/pages/Newtab/Newtab.tsx @@ -16,7 +16,8 @@ const Newtab = () => { type: 'dark', theme: { colors: { - primary: '#3D9CF7' + primary: '#3D9CF7', + link: '#3D9CF7', } } }) diff --git a/src/pages/Newtab/components/MainTop.scss b/src/pages/Newtab/components/MainTop.scss index 5117554..36afaa5 100644 --- a/src/pages/Newtab/components/MainTop.scss +++ b/src/pages/Newtab/components/MainTop.scss @@ -18,6 +18,6 @@ } .right-controls { - gap: 1em; + gap: 0.5em; } } diff --git a/src/pages/Newtab/components/MainTop.tsx b/src/pages/Newtab/components/MainTop.tsx index 10b27c2..3a18018 100644 --- a/src/pages/Newtab/components/MainTop.tsx +++ b/src/pages/Newtab/components/MainTop.tsx @@ -47,8 +47,8 @@ const MainTop = () => { setActiveNote(undefined) setTimeout(() => { - setActiveNote(JSON.parse(JSON.stringify(newNote))) setNotes([newNote, ...notes]) + setActiveNote(newNote) }) } @@ -138,19 +138,20 @@ const MainTop = () => {