import React, { useState, useRef } from 'react'; import PropTypes from 'prop-types'; import './ImageUpload.scss'; import initMatrix from '../../../client/initMatrix'; import Text from '../../atoms/text/Text'; import Avatar from '../../atoms/avatar/Avatar'; import Spinner from '../../atoms/spinner/Spinner'; function ImageUpload({ text, bgColor, imageSrc, onUpload, onRequestRemove, }) { const [uploadPromise, setUploadPromise] = useState(null); const uploadImageRef = useRef(null); async function uploadImage(e) { const file = e.target.files.item(0); if (file === null) return; try { const uPromise = initMatrix.matrixClient.uploadContent(file, { onlyContentUri: false }); setUploadPromise(uPromise); const res = await uPromise; if (typeof res?.content_uri === 'string') onUpload(res.content_uri); setUploadPromise(null); } catch { setUploadPromise(null); } uploadImageRef.current.value = null; } function cancelUpload() { initMatrix.matrixClient.cancelUpload(uploadPromise); setUploadPromise(null); uploadImageRef.current.value = null; } return (