From 66a8e19f69b75a121edc4f2fca4dd029670bf2eb Mon Sep 17 00:00:00 2001 From: vurak Date: Fri, 9 Aug 2024 12:35:55 +0200 Subject: [PATCH 1/2] remove localstorage saved progress on post --- app/(main)/write/page.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/app/(main)/write/page.tsx b/app/(main)/write/page.tsx index 643dbcd..da394d5 100644 --- a/app/(main)/write/page.tsx +++ b/app/(main)/write/page.tsx @@ -18,6 +18,7 @@ export default function Write() { const result = await trigger({ title, content, category_id: category }) if (result) { + localStorage.removeItem(category) router.push(`/read/${result.tile.id}`) } } From f1ba0d7a3a92d60a132a313c8847f07c1394db57 Mon Sep 17 00:00:00 2001 From: vurak Date: Fri, 9 Aug 2024 12:51:41 +0200 Subject: [PATCH 2/2] improve how localsave is stored and add to edit --- app/(main)/edit/[id]/client-editor.tsx | 15 ++++++++++++--- app/(main)/write/page.tsx | 7 ++++--- components/editor/editor.tsx | 9 +++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/(main)/edit/[id]/client-editor.tsx b/app/(main)/edit/[id]/client-editor.tsx index 613d027..d083bc5 100644 --- a/app/(main)/edit/[id]/client-editor.tsx +++ b/app/(main)/edit/[id]/client-editor.tsx @@ -18,6 +18,7 @@ export default function EditClient({ const router = useRouter() const params = useSearchParams() const category = params.get("c") || "" + const localStorageKey = tileId + "-edit" const { trigger, loading, success, error } = usePATCH, any>( `/api/tile?id=${tileId}` @@ -27,10 +28,18 @@ export default function EditClient({ const result = await trigger({ title, content }) if (result) { + localStorage.removeItem(localStorageKey) router.push(`/read/${result.tile.id}`) } } + // override fetched tile content with localstorage in case something has been saved + // locally on top of it, quicksave + const localSave = + typeof window !== "undefined" ? localStorage.getItem(localStorageKey) : null + const { editorContent: editorContent_local = "", title: title_local = "" } = + localSave ? JSON.parse(localSave) : {} + return ( //
//
diff --git a/app/(main)/write/page.tsx b/app/(main)/write/page.tsx index da394d5..c65c721 100644 --- a/app/(main)/write/page.tsx +++ b/app/(main)/write/page.tsx @@ -8,6 +8,7 @@ export default function Write() { const router = useRouter() const params = useSearchParams() const category = params.get("c") || "" + const localStorageKey = category + "-write" const { trigger, loading, success, error } = usePOST, any>( "/api/tile" @@ -18,13 +19,13 @@ export default function Write() { const result = await trigger({ title, content, category_id: category }) if (result) { - localStorage.removeItem(category) + localStorage.removeItem(localStorageKey) router.push(`/read/${result.tile.id}`) } } const localSave = - typeof window !== "undefined" ? localStorage.getItem(category) : null + typeof window !== "undefined" ? localStorage.getItem(localStorageKey) : null const { editorContent = "", title = "" } = localSave ? JSON.parse(localSave) : {} @@ -38,9 +39,9 @@ export default function Write() { // diff --git a/components/editor/editor.tsx b/components/editor/editor.tsx index 850dad8..51205e7 100644 --- a/components/editor/editor.tsx +++ b/components/editor/editor.tsx @@ -24,10 +24,10 @@ import { localSave } from "@/lib/editor" type EditorProps = { onSave: (title: string, content?: string) => {} - category: string initialTitle?: string initialContent?: string loadingSave: boolean //UseMutationState + saveTo?: string } const PoemSchema = z.object({ @@ -38,10 +38,10 @@ type PoemType = z.infer const Editor = ({ onSave, - category, initialTitle, initialContent, loadingSave, + saveTo, }: EditorProps) => { const savingTimeout = useRef(null) @@ -64,12 +64,13 @@ const Editor = ({ class: "px-2 pt-1 pb-32 min-h-full focus:outline-none break-words", }, }, - onUpdate() { + onUpdate(e) { + if (!saveTo) return if (savingTimeout.current) clearTimeout(savingTimeout.current) savingTimeout.current = setTimeout(() => { console.log("saving to localStorage") - localSave(editor?.getHTML() || "", getValues("title"), category) + localSave(e.editor?.getHTML() || "", getValues("title"), saveTo) }, 1000) }, })