forked from nucleuscloud/neosync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEOS-1061 Improves render performance on AI Generate Forms (nucleuscl…
- Loading branch information
1 parent
e08e287
commit 5193cd1
Showing
4 changed files
with
191 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ReactElement } from 'react'; | ||
import { FieldValues, UseFormReturn } from 'react-hook-form'; | ||
import useFormPersist from './useFormPersist'; | ||
|
||
interface FormPersistProps<T extends FieldValues> { | ||
form: UseFormReturn<T>; | ||
formKey: string; | ||
} | ||
const isBrowser = () => typeof window !== 'undefined'; | ||
|
||
export default function FormPersist<T extends FieldValues>( | ||
props: FormPersistProps<T> | ||
): ReactElement { | ||
const { form, formKey } = props; | ||
useFormPersist(formKey, { | ||
control: form.control, | ||
setValue: form.setValue, | ||
storage: isBrowser() ? window.sessionStorage : undefined, | ||
}); | ||
return <></>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useEffect } from 'react'; | ||
import { Control, SetFieldValue, useWatch } from 'react-hook-form'; | ||
|
||
interface FormPersistConfig { | ||
storage?: Storage; | ||
control: Control<any>; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
setValue: SetFieldValue<any>; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
exclude?: string[]; | ||
onDataRestored?: (data: any) => void; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
validate?: boolean; | ||
dirty?: boolean; | ||
touch?: boolean; | ||
onTimeout?: () => void; | ||
timeout?: number; | ||
} | ||
|
||
interface UseFormPersistResult { | ||
clear(): void; | ||
} | ||
|
||
export default function useFormPersist( | ||
name: string, | ||
{ | ||
storage, | ||
control, | ||
setValue, | ||
exclude = [], | ||
onDataRestored, | ||
validate = false, | ||
dirty = false, | ||
touch = false, | ||
onTimeout, | ||
timeout, | ||
}: FormPersistConfig | ||
): UseFormPersistResult { | ||
const watchedValues = useWatch({ | ||
control, | ||
}); | ||
|
||
const getStorage = () => storage || window.sessionStorage; | ||
|
||
const clearStorage = () => getStorage().removeItem(name); | ||
|
||
useEffect(() => { | ||
const str = getStorage().getItem(name); | ||
|
||
if (str) { | ||
const { _timestamp = null, ...values } = JSON.parse(str); | ||
const dataRestored: { [key: string]: any } = {}; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
const currTimestamp = Date.now(); | ||
|
||
if (timeout && currTimestamp - _timestamp > timeout) { | ||
onTimeout && onTimeout(); | ||
clearStorage(); | ||
return; | ||
} | ||
|
||
Object.keys(values).forEach((key) => { | ||
const shouldSet = !exclude.includes(key); | ||
if (shouldSet) { | ||
dataRestored[key] = values[key]; | ||
setValue(key, values[key], { | ||
shouldValidate: validate, | ||
shouldDirty: dirty, | ||
shouldTouch: touch, | ||
}); | ||
} | ||
}); | ||
|
||
if (onDataRestored) { | ||
onDataRestored(dataRestored); | ||
} | ||
} | ||
}, [storage, name, onDataRestored, setValue]); | ||
|
||
useEffect(() => { | ||
const values: Record<string, unknown> = exclude.length | ||
? Object.entries(watchedValues) | ||
.filter(([key]) => !exclude.includes(key)) | ||
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {}) | ||
: Object.assign({}, watchedValues); | ||
|
||
if (Object.entries(values).length) { | ||
if (timeout !== undefined) { | ||
values._timestamp = Date.now(); | ||
} | ||
getStorage().setItem(name, JSON.stringify(values)); | ||
} | ||
}, [watchedValues, timeout]); | ||
|
||
return { | ||
clear: () => getStorage().removeItem(name), | ||
}; | ||
} |