Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / useGetPublicKeysForInbox.ts
blobecbfd555a82e6c52862523dba1f0b5366ea1b906
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;
16 /**
17  * Get public keys valid in the context of Inbox apps.
18  * In particular, internal address keys from external accounts are not returned.
19  */
20 export const useGetPublicKeysForInbox = () => {
21     const cache = useCache();
22     const api = useApi();
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());
28             }
29             const subCache = cache.get(CACHE_KEY);
30             const miss = () =>
31                 getPublicKeysEmailHelper({
32                     email,
33                     internalKeysOnly,
34                     includeInternalKeysWithE2EEDisabledForMail,
35                     api,
36                     ktActivation,
37                     verifyOutboundPublicKeys,
38                     silence: true,
39                     noCache: lifetime === 0,
40                 });
41             const cacheEntryID = `${email},${ktActivation},${internalKeysOnly},${includeInternalKeysWithE2EEDisabledForMail}`;
42             return getPromiseValue(subCache, cacheEntryID, miss, lifetime);
43         },
44         [api, cache, ktActivation]
45     );
48 export default useGetPublicKeysForInbox;