1 import { useEffect, useState } from 'react';
3 import { c } from 'ttag';
5 import { Button } from '@proton/atoms';
6 import RemoteImage from '@proton/components/components/image/RemoteImage';
7 import type { ContactImageProps } from '@proton/components/containers/contacts/modals/ContactImageModal';
8 import type { VCardProperty } from '@proton/shared/lib/interfaces/contacts/VCard';
11 vCardProperty: VCardProperty<string>;
12 onChange: (vCardProperty: VCardProperty) => void;
13 onSelectImage: (props: ContactImageProps) => void;
16 const ContactFieldImage = ({ vCardProperty, onChange, onSelectImage }: Props) => {
17 const [loadNewImage, setLoadNewImage] = useState(false);
19 const handleChangeImage = () => {
20 const handleSubmit = (value: string) => {
21 onChange({ ...vCardProperty, value });
22 setLoadNewImage(true);
24 onSelectImage({ url: vCardProperty.value, onSubmit: handleSubmit });
28 * Load image by default in edit mode
29 * AND allow image update when "default" contact photo is deleted
30 * In that case, photo from "Other" field will become the contact photo (if any).
31 * But we need to reload them, using this useEffect
34 if (vCardProperty.value) {
35 setLoadNewImage(true);
37 }, [vCardProperty.value]);
41 {vCardProperty.value ? (
42 <RemoteImage src={vCardProperty.value} autoLoad={loadNewImage} />
44 <Button onClick={handleChangeImage}>{c('Action').t`Upload picture`}</Button>
50 export default ContactFieldImage;