Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / containers / organization / useConvertExternalAddresses.ts
blob92019b16da7350a0bb3258b4ebc3284918503701
1 import { useEffect, useRef } from 'react';
3 import { useCustomDomains } from '@proton/account/domains/hooks';
4 import {
5     convertExternalAddresses,
6     convertMemberExternalAddresses,
7 } from '@proton/account/organizationKey/convertAddresses';
8 import { useGetUser } from '@proton/account/user/hooks';
9 import useKTVerifier from '@proton/components/containers/keyTransparency/useKTVerifier';
10 import useApi from '@proton/components/hooks/useApi';
11 import { useDispatch } from '@proton/redux-shared-store';
13 let oncePerPageLoad = false;
15 // TODO: This hook should be migrated fully to redux once all the KT hooks have been migrated.
16 const useConvertExternalAddresses = () => {
17     const dispatch = useDispatch();
18     const api = useApi();
19     const [domains] = useCustomDomains();
20     const getUser = useGetUser();
21     const { keyTransparencyVerify, keyTransparencyCommit } = useKTVerifier(api, getUser);
22     const runningRef = useRef(false);
24     useEffect(() => {
25         if (!domains?.length || oncePerPageLoad || runningRef.current) {
26             return;
27         }
28         runningRef.current = true;
29         Promise.all([
30             dispatch(
31                 convertExternalAddresses({
32                     domains,
33                     keyTransparencyVerify,
34                     keyTransparencyCommit,
35                 })
36             ),
37             dispatch(
38                 convertMemberExternalAddresses({
39                     domains,
40                     keyTransparencyVerify,
41                     keyTransparencyCommit,
42                 })
43             ),
44         ])
45             .catch(() => {
46                 oncePerPageLoad = true;
47             })
48             .finally(() => {
49                 runningRef.current = false;
50             });
51     }, [domains]);
54 export default useConvertExternalAddresses;