1 import { useCallback } from 'react';
3 import useApi from '@proton/components/hooks/useApi';
4 import { getPromiseValue } from '@proton/components/hooks/useCachedModelResult';
5 import { fetchLatestEpoch } from '@proton/key-transparency/lib';
6 import { getSilentApi } from '@proton/shared/lib/api/helpers/customConfig';
7 import { HOUR } from '@proton/shared/lib/constants';
8 import type { GetLatestEpoch } from '@proton/shared/lib/interfaces';
10 import useCache from '../../hooks/useCache';
12 export const CACHE_KEY = 'KEY_TRANSPARENCY_EPOCH';
14 const DEFAULT_LIFETIME = 2 * HOUR;
16 const useGetLatestEpoch = (): GetLatestEpoch => {
17 const cache = useCache();
18 const normalApi = useApi();
19 const silentApi = getSilentApi(normalApi);
20 const miss = useCallback(() => fetchLatestEpoch(silentApi), [silentApi]);
22 (forceRefresh?: boolean) => {
24 cache.delete(CACHE_KEY);
26 return getPromiseValue(cache, CACHE_KEY, miss, DEFAULT_LIFETIME);
32 export default useGetLatestEpoch;