import { getCSTTime } from './date' ; export const storeCachedData = ( key , value ) => { localStorage . setItem ( key , JSON . stringify ( value ) ) ; } ; export const fetchCachedData = < T > ( key ) : T | null => { const data = localStorage . getItem ( key ) ; if ( data ) { try { const object = JSON . parse ( data ) as T ; if ( object ) { return object ; } } catch ( error ) { console . error ( error ) ; } } return null ; } ; export const removeCachedData = ( key ) => { localStorage . removeItem ( key ) ; } ; export const storeDateChartCache = ( key , value ) => { let cacheKey = fetchCachedData < string > ( key ) ; if ( cacheKey && fetchCachedData ( cacheKey ) ) { localStorage . removeItem ( cacheKey ) ; } cacheKey = `${ key }&${ getCSTTime ( ) }` ; storeCachedData ( key , cacheKey ) ; storeCachedData ( cacheKey , value ) ; } ; export const fetchDateChartCache = ( key ) => { const cacheKey = fetchCachedData < string > ( key ) ; if ( ! cacheKey ) return null ; const storeTime = Number ( cacheKey . substring ( cacheKey . indexOf ( '&' ) + 1 ) ) ; const now = new Date ( getCSTTime ( ) ) ; const dataUpdateTime = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) , 0 , 10 , 0 ) . getTime ( ) ; return storeTime > dataUpdateTime ? fetchCachedData ( cacheKey ) : null ; } ; export const storeEpochChartCache = ( key , value ) => { storeDateChartCache ( key , value ) ; } ; const ThreeHours = 3 * 60 * 60 * 1000 ; export const fetchEpochChartCache = ( key ) => { const cacheKey = fetchCachedData < string > ( key ) ; if ( ! cacheKey ) return null ; const storeTime = Number ( cacheKey . substring ( cacheKey . indexOf ( '&' ) + 1 ) ) ; return getCSTTime ( ) - storeTime < ThreeHours ? fetchCachedData ( cacheKey ) : null ; } ; export default { storeCachedData , fetchCachedData , storeDateChartCache , fetchDateChartCache , storeEpochChartCache , fetchEpochChartCache , } ;