fix cached forms not being removed when submitted

This commit is contained in:
hzrd149
2024-08-31 06:52:16 -05:00
parent edd74189f3
commit 8deeec45ce
2 changed files with 12 additions and 9 deletions

View File

@@ -14,12 +14,14 @@ export default function useCacheForm<TFieldValues extends FieldValues = FieldVal
const log = useMemo(() => (key ? logger.extend(`CachedForm:${key}`) : () => {}), [key]);
const storageKey = key && "cached-form-" + key;
const stateRef = useRef<UseFormStateReturn<TFieldValues>>(state);
stateRef.current = state;
const isSubmitted = useRef<boolean>(state.isSubmitted);
isSubmitted.current = state.isSubmitted;
// NOTE: this watches the state
state.isDirty;
state.isSubmitted;
const isSubmitting = useRef<boolean>(state.isSubmitting);
isSubmitting.current = state.isSubmitting;
const isDirty = useRef<boolean>(state.isDirty);
isDirty.current = state.isDirty;
useEffect(() => {
if (!storageKey) return;
@@ -44,10 +46,10 @@ export default function useCacheForm<TFieldValues extends FieldValues = FieldVal
// save previous key on change or unmount
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));
@@ -58,10 +60,10 @@ export default function useCacheForm<TFieldValues extends FieldValues = FieldVal
const saveOnClose = useCallback(() => {
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));

View File

@@ -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);