diff --git a/src/hooks/use-cache-form.ts b/src/hooks/use-cache-form.ts index a76f0ab41..48e725409 100644 --- a/src/hooks/use-cache-form.ts +++ b/src/hooks/use-cache-form.ts @@ -14,12 +14,14 @@ export default function useCacheForm (key ? logger.extend(`CachedForm:${key}`) : () => {}), [key]); const storageKey = key && "cached-form-" + key; - const stateRef = useRef>(state); - stateRef.current = state; + const isSubmitted = useRef(state.isSubmitted); + isSubmitted.current = state.isSubmitted; - // NOTE: this watches the state - state.isDirty; - state.isSubmitted; + const isSubmitting = useRef(state.isSubmitting); + isSubmitting.current = state.isSubmitting; + + const isDirty = useRef(state.isDirty); + isDirty.current = state.isDirty; useEffect(() => { if (!storageKey) return; @@ -44,10 +46,10 @@ export default function useCacheForm { - if (stateRef.current.isSubmitted) { + if (isSubmitted.current || isSubmitting.current) { log("Removing because submitted"); localStorage.removeItem(storageKey); - } else if (stateRef.current.isDirty) { + } else if (isDirty.current) { const values = getValues(); log("Saving form", values); localStorage.setItem(storageKey, JSON.stringify(values)); @@ -58,10 +60,10 @@ export default function useCacheForm { if (!storageKey) return; - if (stateRef.current.isSubmitted) { + if (isSubmitted.current || isSubmitting.current) { log("Removing because submitted"); localStorage.removeItem(storageKey); - } else if (stateRef.current.isDirty) { + } else if (isDirty.current) { const values = getValues(); log("Saving form", values); localStorage.setItem(storageKey, JSON.stringify(values)); diff --git a/src/views/thread/components/reply-form.tsx b/src/views/thread/components/reply-form.tsx index 331b93cfe..80a10a2ad 100644 --- a/src/views/thread/components/reply-form.tsx +++ b/src/views/thread/components/reply-form.tsx @@ -45,6 +45,7 @@ export default function ReplyForm({ item, onCancel, onSubmitted, replyKind = kin }, mode: "all", }); + const clearCache = useCacheForm<{ content: string }>(`reply-${item.event.id}`, getValues, reset, formState); const contentMentions = getPubkeysMentionedInContent(getValues().content);