mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-20 13:01:07 +02:00
fix cached forms not being removed when submitted
This commit is contained in:
@@ -14,12 +14,14 @@ export default function useCacheForm<TFieldValues extends FieldValues = FieldVal
|
|||||||
const log = useMemo(() => (key ? logger.extend(`CachedForm:${key}`) : () => {}), [key]);
|
const log = useMemo(() => (key ? logger.extend(`CachedForm:${key}`) : () => {}), [key]);
|
||||||
const storageKey = key && "cached-form-" + key;
|
const storageKey = key && "cached-form-" + key;
|
||||||
|
|
||||||
const stateRef = useRef<UseFormStateReturn<TFieldValues>>(state);
|
const isSubmitted = useRef<boolean>(state.isSubmitted);
|
||||||
stateRef.current = state;
|
isSubmitted.current = state.isSubmitted;
|
||||||
|
|
||||||
// NOTE: this watches the state
|
const isSubmitting = useRef<boolean>(state.isSubmitting);
|
||||||
state.isDirty;
|
isSubmitting.current = state.isSubmitting;
|
||||||
state.isSubmitted;
|
|
||||||
|
const isDirty = useRef<boolean>(state.isDirty);
|
||||||
|
isDirty.current = state.isDirty;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!storageKey) return;
|
if (!storageKey) return;
|
||||||
@@ -44,10 +46,10 @@ export default function useCacheForm<TFieldValues extends FieldValues = FieldVal
|
|||||||
|
|
||||||
// save previous key on change or unmount
|
// save previous key on change or unmount
|
||||||
return () => {
|
return () => {
|
||||||
if (stateRef.current.isSubmitted) {
|
if (isSubmitted.current || isSubmitting.current) {
|
||||||
log("Removing because submitted");
|
log("Removing because submitted");
|
||||||
localStorage.removeItem(storageKey);
|
localStorage.removeItem(storageKey);
|
||||||
} else if (stateRef.current.isDirty) {
|
} else if (isDirty.current) {
|
||||||
const values = getValues();
|
const values = getValues();
|
||||||
log("Saving form", values);
|
log("Saving form", values);
|
||||||
localStorage.setItem(storageKey, JSON.stringify(values));
|
localStorage.setItem(storageKey, JSON.stringify(values));
|
||||||
@@ -58,10 +60,10 @@ export default function useCacheForm<TFieldValues extends FieldValues = FieldVal
|
|||||||
const saveOnClose = useCallback(() => {
|
const saveOnClose = useCallback(() => {
|
||||||
if (!storageKey) return;
|
if (!storageKey) return;
|
||||||
|
|
||||||
if (stateRef.current.isSubmitted) {
|
if (isSubmitted.current || isSubmitting.current) {
|
||||||
log("Removing because submitted");
|
log("Removing because submitted");
|
||||||
localStorage.removeItem(storageKey);
|
localStorage.removeItem(storageKey);
|
||||||
} else if (stateRef.current.isDirty) {
|
} else if (isDirty.current) {
|
||||||
const values = getValues();
|
const values = getValues();
|
||||||
log("Saving form", values);
|
log("Saving form", values);
|
||||||
localStorage.setItem(storageKey, JSON.stringify(values));
|
localStorage.setItem(storageKey, JSON.stringify(values));
|
||||||
|
@@ -45,6 +45,7 @@ export default function ReplyForm({ item, onCancel, onSubmitted, replyKind = kin
|
|||||||
},
|
},
|
||||||
mode: "all",
|
mode: "all",
|
||||||
});
|
});
|
||||||
|
|
||||||
const clearCache = useCacheForm<{ content: string }>(`reply-${item.event.id}`, getValues, reset, formState);
|
const clearCache = useCacheForm<{ content: string }>(`reply-${item.event.id}`, getValues, reset, formState);
|
||||||
|
|
||||||
const contentMentions = getPubkeysMentionedInContent(getValues().content);
|
const contentMentions = getPubkeysMentionedInContent(getValues().content);
|
||||||
|
Reference in New Issue
Block a user