1 import { useCallback } from 'react';
3 import getPublicKeysEmailHelper from '@proton/shared/lib/api/helpers/getPublicKeysEmailHelper';
4 import { MINUTE } from '@proton/shared/lib/constants';
5 import type { GetPublicKeysForInbox } from '@proton/shared/lib/interfaces/hooks/GetPublicKeysForInbox';
7 import { useKeyTransparencyContext } from '../containers/keyTransparency/useKeyTransparencyContext';
8 import useApi from './useApi';
9 import useCache from './useCache';
10 import { getPromiseValue } from './useCachedModelResult';
12 export const CACHE_KEY = 'PUBLIC_KEYS';
14 const DEFAULT_LIFETIME = 30 * MINUTE;
17 * Get public keys valid in the context of Inbox apps.
18 * In particular, internal address keys from external accounts are not returned.
20 export const useGetPublicKeysForInbox = () => {
21 const cache = useCache();
23 const { verifyOutboundPublicKeys, ktActivation } = useKeyTransparencyContext();
24 return useCallback<GetPublicKeysForInbox>(
27 lifetime = DEFAULT_LIFETIME,
30 includeInternalKeysWithE2EEDisabledForMail,
32 if (!cache.has(CACHE_KEY)) {
33 cache.set(CACHE_KEY, new Map());
35 const subCache = cache.get(CACHE_KEY);
37 getPublicKeysEmailHelper({
40 includeInternalKeysWithE2EEDisabledForMail,
43 verifyOutboundPublicKeys,
47 const cacheEntryID = `${email},${ktActivation},${internalKeysOnly},${includeInternalKeysWithE2EEDisabledForMail}`;
48 return getPromiseValue(subCache, cacheEntryID, miss, lifetime);
50 [api, cache, ktActivation]
54 export default useGetPublicKeysForInbox;