import { userHasAllowedDiagnosticsKey } from '@shared/utils/storage' ; import hash from 'object-hash' ; import { hashQueryKey , QueryKey } from 'react-query' ; export function textToBytes ( content ) { return new TextEncoder ( ) . encode ( content ) ; } export function bytesToText ( buffer ) { return new TextDecoder ( ) . decode ( buffer ) ; } export function makeLocalDataKey ( params ) { return hash ( hashQueryKey ( [ params , VERSION ] ) ) ; } export function getLocalData < Data > ( params ) { const key = makeLocalDataKey ( params ) ; const value = localStorage . getItem ( key ) ; if ( ! value ) return null ; return JSON . parse ( value ) as Data ; } export function setLocalData < Data > ( params , data ) { const key = makeLocalDataKey ( params ) ; localStorage . setItem ( key , JSON . stringify ( data ) ) ; return data ; } const PERSISTENT_LOCAL_DATA = [ userHasAllowedDiagnosticsKey ] ; export function clearSessionLocalData ( ) { const backup = PERSISTENT_LOCAL_DATA . map ( ( key ) => [ key , localStorage . getItem ( key ) ] ) ; localStorage . clear ( ) ; backup . forEach ( ( [ key , value ] ) => { if ( key === null || value === null ) return ; localStorage . setItem ( key , value ) ; } ) ; return localStorage ; }