import { useState , useEffect , useCallback } from 'react' ; import { getStorage } from '@shared/manager/storage' ; param key param defaultValue param isPersist export function useStorage < T = { } > ( key , defaultValue ? , isPersist = false ) { const [ value , setValue ] = useState < T > ( defaultValue ?? ( null as any ) ) ; useEffect ( ( ) => { getStorage ( ) . get ( key , defaultValue ) . then ( ( val ) => setValue ( val ) ) ; } , [ key ] ) ; const set = useCallback ( ( val ) : Promise < T > => { setValue ( val ! ) ; const p = isPersist ? getStorage ( ) . save ( key , val ) : getStorage ( ) . set ( key , val ) ; return p . then ( ( val ) => { return val ! ; } ) ; } , [ key , isPersist ] ) ; return [ value , set ] ; }