1 import type { FormEvent } from 'react';
2 import { useState } from 'react';
4 import { c } from 'ttag';
6 import { useGetOrganizationKey } from '@proton/account/organizationKey/hooks';
7 import { useGetUserKeys } from '@proton/account/userKeys/hooks';
8 import { Button } from '@proton/atoms';
9 import type { ModalProps } from '@proton/components/components/modalTwo/Modal';
10 import Modal from '@proton/components/components/modalTwo/Modal';
11 import ModalContent from '@proton/components/components/modalTwo/ModalContent';
12 import ModalFooter from '@proton/components/components/modalTwo/ModalFooter';
13 import ModalHeader from '@proton/components/components/modalTwo/ModalHeader';
14 import InputFieldTwo from '@proton/components/components/v2/field/InputField';
15 import useFormErrors from '@proton/components/components/v2/useFormErrors';
16 import useApi from '@proton/components/hooks/useApi';
17 import useEventManager from '@proton/components/hooks/useEventManager';
18 import useNotifications from '@proton/components/hooks/useNotifications';
19 import { useLoading } from '@proton/hooks';
20 import { renameInternalAddress, updateAddress } from '@proton/shared/lib/api/addresses';
21 import { CANONICALIZE_SCHEME, canonicalizeEmail, getEmailParts } from '@proton/shared/lib/helpers/email';
22 import { emailValidator, requiredValidator } from '@proton/shared/lib/helpers/formValidators';
23 import type { Address } from '@proton/shared/lib/interfaces';
24 import { getRenamedAddressKeys } from '@proton/shared/lib/keys';
25 import noop from '@proton/utils/noop';
27 interface Props extends ModalProps<'form'> {
31 const EditInternalAddressModal = ({ address, ...rest }: Props) => {
32 const [initialEmail] = useState(address.Email);
33 const [[initialLocalEmail, domain]] = useState(getEmailParts(initialEmail));
34 const [initialDisplayName] = useState(address.DisplayName);
36 const [displayName, setDisplayName] = useState(initialDisplayName);
37 const [localEmail, setEmail] = useState(initialLocalEmail);
38 const newEmail = `${localEmail}@${domain}`;
39 const getUserKeys = useGetUserKeys();
40 const getOrganizationKey = useGetOrganizationKey();
41 const { createNotification } = useNotifications();
42 const { onFormSubmit, validator } = useFormErrors();
43 const [submitting, withLoading] = useLoading();
45 const { call } = useEventManager();
47 const handleSubmit = async () => {
48 if (localEmail !== initialLocalEmail) {
49 const userKeys = await getUserKeys();
50 const organizationKey = await getOrganizationKey();
52 renameInternalAddress(address.ID, {
54 AddressKeys: await getRenamedAddressKeys({
56 addressKeys: address.Keys,
57 organizationKey: organizationKey?.privateKey ? organizationKey : undefined,
63 if (displayName !== initialDisplayName) {
65 updateAddress(address.ID, {
66 DisplayName: displayName,
67 Signature: address.Signature,
72 createNotification({ text: c('Success').t`Email address updated` });
76 const handleClose = submitting ? undefined : rest.onClose;
83 onSubmit={(event: FormEvent) => {
84 event.preventDefault();
85 event.stopPropagation();
86 if (!onFormSubmit()) {
89 withLoading(handleSubmit());
94 <ModalHeader title={c('Title').t`Edit email address`} />
96 <div className="color-weak mb-4">{c('loc_nightly_Info')
97 .t`You can change capitalization or punctuation to edit your email address.`}</div>
105 <span className="text-ellipsis" title={`@${domain}`}>
111 requiredValidator(localEmail),
112 emailValidator(newEmail),
115 canonicalizeEmail(newEmail, CANONICALIZE_SCHEME.PROTON) !==
116 canonicalizeEmail(initialEmail, CANONICALIZE_SCHEME.PROTON)
118 return c('loc_nightly_Error')
119 .t`Only capitalization and punctuation (periods, hyphens, and underscores) can be changed for this address`;
124 label={c('Label').t`Address`}
129 onValue={setDisplayName}
130 label={c('Label').t`Display name`}
134 <Button onClick={handleClose} disabled={submitting}>{c('Action').t`Cancel`}</Button>
135 <Button color="norm" type="submit" loading={submitting}>{c('Action').t`Save`}</Button>
141 export default EditInternalAddressModal;