1 import type { ChangeEvent } from 'react';
2 import { useState } from 'react';
4 import { c } from 'ttag';
6 import Field from '@proton/components/components/container/Field';
7 import Row from '@proton/components/components/container/Row';
8 import Editor from '@proton/components/components/editor/Editor';
9 import { useToolbar } from '@proton/components/components/editor/hooks/useToolbar';
10 import type { EditorActions } from '@proton/components/components/editor/interface';
11 import Input from '@proton/components/components/input/Input';
12 import Label from '@proton/components/components/label/Label';
13 import FormModal from '@proton/components/components/modal/FormModal';
14 import useApi from '@proton/components/hooks/useApi';
15 import useEventManager from '@proton/components/hooks/useEventManager';
16 import useNotifications from '@proton/components/hooks/useNotifications';
17 import { useLoading } from '@proton/hooks';
18 import { updateAddress } from '@proton/shared/lib/api/addresses';
19 import type { Address } from '@proton/shared/lib/interfaces';
21 const EMPTY_VALUES = [/^(<div><br><\/div>)+$/, /^(<div>\s*<\/div>)+$/];
23 const formatSignature = (value: string) => (EMPTY_VALUES.some((regex) => regex.test(value)) ? '' : value);
29 const EditAddressModal = ({ onClose, address, ...rest }: Props) => {
31 const { call } = useEventManager();
32 const [loading, withLoading] = useLoading();
33 const [model, updateModel] = useState({
34 displayName: address.DisplayName,
35 signature: address.Signature,
37 const { createNotification } = useNotifications();
39 const handleReady = (actions: EditorActions) => {
40 actions.setContent(model.signature);
43 const handleDisplayName = ({ target }: ChangeEvent<HTMLInputElement>) =>
44 updateModel({ ...model, displayName: target.value });
46 const handleSignature = (value: string) => updateModel({ ...model, signature: value });
48 const handleSubmit = async () => {
50 updateAddress(address.ID, { DisplayName: model.displayName, Signature: formatSignature(model.signature) })
54 createNotification({ text: c('Success').t`Address updated` });
57 const { openEmojiPickerRef, toolbarConfig, setToolbarConfig, modalLink, modalImage, modalDefaultFont } = useToolbar(
64 onSubmit={() => withLoading(handleSubmit())}
65 title={c('Title').t`Edit address`}
66 submit={c('Action').t`Save address`}
71 <Label>{c('Label').t`Address`}</Label>
72 <div className="relative pt-2 text-ellipsis max-w-full" title={address.Email}>
77 <Label>{c('Label').t`Display name`}</Label>
80 value={model.displayName}
81 placeholder={c('Placeholder').t`Choose display name`}
82 onChange={handleDisplayName}
89 onChange={handleSignature}
91 openEmojiPickerRef={openEmojiPickerRef}
92 toolbarConfig={toolbarConfig}
93 setToolbarConfig={setToolbarConfig}
95 modalImage={modalImage}
96 modalDefaultFont={modalDefaultFont}
103 export default EditAddressModal;