Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useGetPublicKeysForInbox.ts
blobfca179890eb13bb16f699de7fc82cec12a8b7014
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         ({
26             email,
27             lifetime = DEFAULT_LIFETIME,
28             noCache = false,
29             internalKeysOnly,
30             includeInternalKeysWithE2EEDisabledForMail,
31         }) => {
32             if (!cache.has(CACHE_KEY)) {
33                 cache.set(CACHE_KEY, new Map());
34             }
35             const subCache = cache.get(CACHE_KEY);
36             const miss = () =>
37                 getPublicKeysEmailHelper({
38                     email,
39                     internalKeysOnly,
40                     includeInternalKeysWithE2EEDisabledForMail,
41                     api,
42                     ktActivation,
43                     verifyOutboundPublicKeys,
44                     silence: true,
45                     noCache,
46                 });
47             const cacheEntryID = `${email},${ktActivation},${internalKeysOnly},${includeInternalKeysWithE2EEDisabledForMail}`;
48             return getPromiseValue(subCache, cacheEntryID, miss, lifetime);
49         },
50         [api, cache, ktActivation]
51     );
54 export default useGetPublicKeysForInbox;