import { Form , useCMS , useWatchFormValues } from 'tinacms' ; import { useCallback , useEffect } from 'react' ; import { flattenFormData } from './flatten-form-data' ; export const useLocalStorageCache = ( path , form , preview ) => { const cms = useCMS ( ) ; const saveToStorage = useCallback ( _formData => { cms . api . storage . save ( path , flattenFormData ( form . finalForm ) ) ; } , [ path ] ) ; useWatchFormValues ( form , saveToStorage ) ; useEffect ( ( ) => { if ( ! preview ) return ; const values = cms . api . storage . load ( path ) ; if ( values ) { form . updateValues ( values ) ; } } , [ form , preview ] ) ; } ;