import React, { useState, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import { twemojify } from '../../../util/twemojify'; import initMatrix from '../../../client/initMatrix'; import colorMXID from '../../../util/colorMXID'; import Text from '../../atoms/text/Text'; import IconButton from '../../atoms/button/IconButton'; import Button from '../../atoms/button/Button'; import ImageUpload from '../../molecules/image-upload/ImageUpload'; import Input from '../../atoms/input/Input'; import PencilIC from '../../../../public/res/ic/outlined/pencil.svg'; import './ProfileEditor.scss'; // TODO Fix bug that prevents 'Save' button from enabling up until second changed. function ProfileEditor({ userId }) { const [isEditing, setIsEditing] = useState(false); const mx = initMatrix.matrixClient; const user = mx.getUser(mx.getUserId()); const displayNameRef = useRef(null); const [avatarSrc, setAvatarSrc] = useState(user.avatarUrl ? mx.mxcUrlToHttp(user.avatarUrl, 80, 80, 'crop') : null); const [username, setUsername] = useState(user.displayName); const [disabled, setDisabled] = useState(true); useEffect(() => { let isMounted = true; mx.getProfileInfo(mx.getUserId()).then((info) => { if (!isMounted) return; setAvatarSrc(info.avatar_url ? mx.mxcUrlToHttp(info.avatar_url, 80, 80, 'crop') : null); setUsername(info.displayname); }); return () => { isMounted = false; }; }, [userId]); const handleAvatarUpload = (url) => { if (url === null) { if (confirm('Are you sure that you want to remove avatar?')) { mx.setAvatarUrl(''); setAvatarSrc(null); } return; } mx.setAvatarUrl(url); setAvatarSrc(mx.mxcUrlToHttp(url, 80, 80, 'crop')); }; const saveDisplayName = () => { const newDisplayName = displayNameRef.current.value; if (newDisplayName !== null && newDisplayName !== username) { mx.setDisplayName(newDisplayName); setUsername(newDisplayName); setDisabled(true); setIsEditing(false); } }; const onDisplayNameInputChange = () => { setDisabled(username === displayNameRef.current.value || displayNameRef.current.value == null); }; const cancelDisplayNameChanges = () => { displayNameRef.current.value = username; onDisplayNameInputChange(); setIsEditing(false); }; const renderForm = () => (
); const renderInfo = () => (