diff --git a/src/components/ModForm.tsx b/src/components/ModForm.tsx index e17f646..a7c3699 100644 --- a/src/components/ModForm.tsx +++ b/src/components/ModForm.tsx @@ -22,7 +22,7 @@ import { ModFormState, ModPageLoaderResult } from '../types' -import { initializeFormState } from '../utils' +import { initializeFormState, MOD_DRAFT_CACHE_KEY } from '../utils' import { CheckboxField, InputField, InputFieldWithImageUpload } from './Inputs' import { OriginalAuthor } from './OriginalAuthor' import { CategoryAutocomplete } from './CategoryAutocomplete' @@ -49,7 +49,8 @@ export const ModForm = () => { // Enable cache for the new mod const isEditing = typeof mod !== 'undefined' - const [cache, setCache, clearCache] = useLocalCache('draft-mod') + const [cache, setCache, clearCache] = + useLocalCache(MOD_DRAFT_CACHE_KEY) const [formState, setFormState] = useState( isEditing ? initializeFormState(mod) : cache ? cache : initializeFormState() ) diff --git a/src/pages/submitMod/action.ts b/src/pages/submitMod/action.ts index 5d042b5..ab5af31 100644 --- a/src/pages/submitMod/action.ts +++ b/src/pages/submitMod/action.ts @@ -12,7 +12,9 @@ import { isValidUrl, log, LogType, - now + MOD_DRAFT_CACHE_KEY, + now, + removeLocalStorageItem } from 'utils' import { v4 as uuidv4 } from 'uuid' import { T_TAG_VALUE } from '../../constants' @@ -141,6 +143,8 @@ export const submitModRouteAction = )}` ) + !isEditing && removeLocalStorageItem(MOD_DRAFT_CACHE_KEY) + const naddr = nip19.naddrEncode({ identifier: aTag, pubkey: signedEvent.pubkey, diff --git a/src/pages/write/action.ts b/src/pages/write/action.ts index daefbb4..f08a3fb 100644 --- a/src/pages/write/action.ts +++ b/src/pages/write/action.ts @@ -2,7 +2,15 @@ import { NDKContextType } from 'contexts/NDKContext' import { ActionFunctionArgs, redirect } from 'react-router-dom' import { getBlogPageRoute } from 'routes' import { BlogFormErrors, BlogEventSubmitForm, BlogEventEditForm } from 'types' -import { isReachable, isValidImageUrl, log, LogType, now } from 'utils' +import { + BLOG_DRAFT_CACHE_KEY, + isReachable, + isValidImageUrl, + log, + LogType, + now, + removeLocalStorageItem +} from 'utils' import { kinds, UnsignedEvent, Event, nip19 } from 'nostr-tools' import { toast } from 'react-toastify' import { NDKEvent } from '@nostr-dev-kit/ndk' @@ -118,6 +126,9 @@ export const writeRouteAction = '\n' )}` ) + + !isEditing && removeLocalStorageItem(BLOG_DRAFT_CACHE_KEY) + const naddr = nip19.naddrEncode({ identifier: uuid, pubkey: signedEvent.pubkey, diff --git a/src/pages/write/index.tsx b/src/pages/write/index.tsx index 0153465..1d505b9 100644 --- a/src/pages/write/index.tsx +++ b/src/pages/write/index.tsx @@ -22,7 +22,7 @@ import { LoadingSpinner } from 'components/LoadingSpinner' import { AlertPopup } from 'components/AlertPopup' import { Editor, EditorRef } from 'components/Markdown/Editor' import { InputError } from 'components/Inputs/Error' -import { initializeBlogForm } from 'utils' +import { BLOG_DRAFT_CACHE_KEY, initializeBlogForm } from 'utils' import 'styles/innerPage.css' import 'styles/styles.css' import 'styles/write.css' @@ -40,7 +40,7 @@ export const WritePage = () => { // Enable cache for the new blog const isEditing = typeof data?.blog !== 'undefined' const [cache, setCache, clearCache] = - useLocalCache('draft-blog') + useLocalCache(BLOG_DRAFT_CACHE_KEY) const title = isEditing ? 'Edit blog post' : 'Submit a blog post' const [formState, setFormState] = useState< diff --git a/src/utils/blog.ts b/src/utils/blog.ts index b937522..8d54f65 100644 --- a/src/utils/blog.ts +++ b/src/utils/blog.ts @@ -75,3 +75,5 @@ export const initializeBlogForm = ( published_at: blog.published_at }) }) + +export const BLOG_DRAFT_CACHE_KEY = 'draft-blog' diff --git a/src/utils/mod.ts b/src/utils/mod.ts index 3d40002..04ecdfa 100644 --- a/src/utils/mod.ts +++ b/src/utils/mod.ts @@ -146,3 +146,5 @@ export const initializeFormState = ( } ] }) + +export const MOD_DRAFT_CACHE_KEY = 'draft-mod'