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>(
25 ({ email, lifetime = DEFAULT_LIFETIME, internalKeysOnly, includeInternalKeysWithE2EEDisabledForMail }) => {
26 if (!cache.has(CACHE_KEY)) {
27 cache.set(CACHE_KEY, new Map());
29 const subCache = cache.get(CACHE_KEY);
31 getPublicKeysEmailHelper({
34 includeInternalKeysWithE2EEDisabledForMail,
37 verifyOutboundPublicKeys,
39 noCache: lifetime === 0,
41 const cacheEntryID = `${email},${ktActivation},${internalKeysOnly},${includeInternalKeysWithE2EEDisabledForMail}`;
42 return getPromiseValue(subCache, cacheEntryID, miss, lifetime);
44 [api, cache, ktActivation]
48 export default useGetPublicKeysForInbox;