Merge branch 'INDA-330-pii-update' into 'main'
[ProtonMail-WebClient.git] / applications / mail / src / app / hooks / message / useGetMessageKeys.ts
blob25b8f3bcbacc66b9fc781c0b67de7c9bfae88da3
1 import { useCallback } from 'react';
3 import { useGetAddressKeys } from '@proton/account/addressKeys/hooks';
4 import type { Message } from '@proton/shared/lib/interfaces/mail/Message';
5 import { splitKeys } from '@proton/shared/lib/keys/keys';
7 import type { PublicPrivateKey } from '../../store/messages/messagesTypes';
9 export type GetMessageKeys = (message: Pick<Message, 'AddressID'>) => Promise<PublicPrivateKey>;
10 export type UseGetMessageKeys = () => GetMessageKeys;
12 /**
13  * Add user public and private keys to the MessageExtended if not already there
14  */
15 export const useGetMessageKeys: UseGetMessageKeys = () => {
16     const getAddressKeys = useGetAddressKeys();
18     return useCallback(
19         async (message: Pick<Message, 'AddressID'>) => {
20             const { publicKeys, privateKeys } = splitKeys(await getAddressKeys(message.AddressID));
21             return { publicKeys, privateKeys, type: 'publicPrivate' };
22         },
23         [getAddressKeys]
24     );