2019-12-16 17:58:12 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { _t } from "../../../languageHandler";
|
2020-09-22 06:00:51 +03:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import classNames from "classnames";
|
2019-12-16 17:58:12 +03:00
|
|
|
|
2021-08-14 11:53:53 +03:00
|
|
|
interface IProps {
|
|
|
|
avatarUrl?: string;
|
|
|
|
avatarName: string; // name of user/room the avatar belongs to
|
|
|
|
uploadAvatar?: (e: React.MouseEvent) => void;
|
|
|
|
removeAvatar?: (e: React.MouseEvent) => void;
|
|
|
|
avatarAltText: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AvatarSetting: React.FC<IProps> = ({ avatarUrl, avatarAltText, avatarName, uploadAvatar, removeAvatar }) => {
|
2020-09-24 17:26:59 +03:00
|
|
|
const [isHovering, setIsHovering] = useState(false);
|
2020-09-22 06:00:51 +03:00
|
|
|
const hoveringProps = {
|
|
|
|
onMouseEnter: () => setIsHovering(true),
|
|
|
|
onMouseLeave: () => setIsHovering(false),
|
|
|
|
};
|
2019-12-16 18:08:22 +03:00
|
|
|
|
2020-09-22 06:00:51 +03:00
|
|
|
let avatarElement = <AccessibleButton
|
|
|
|
element="div"
|
|
|
|
onClick={uploadAvatar}
|
|
|
|
className="mx_AvatarSetting_avatarPlaceholder"
|
|
|
|
{...hoveringProps}
|
|
|
|
/>;
|
2019-12-16 17:58:12 +03:00
|
|
|
if (avatarUrl) {
|
2019-12-16 18:08:22 +03:00
|
|
|
avatarElement = (
|
|
|
|
<AccessibleButton
|
|
|
|
element="img"
|
|
|
|
src={avatarUrl}
|
|
|
|
alt={avatarAltText}
|
|
|
|
aria-label={avatarAltText}
|
2020-09-22 06:00:51 +03:00
|
|
|
onClick={uploadAvatar}
|
|
|
|
{...hoveringProps}
|
|
|
|
/>
|
2019-12-16 18:08:22 +03:00
|
|
|
);
|
2019-12-16 17:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let uploadAvatarBtn;
|
|
|
|
if (uploadAvatar) {
|
|
|
|
// insert an empty div to be the host for a css mask containing the upload.svg
|
2020-09-22 06:00:51 +03:00
|
|
|
uploadAvatarBtn = <AccessibleButton
|
|
|
|
onClick={uploadAvatar}
|
|
|
|
className='mx_AvatarSetting_uploadButton'
|
|
|
|
{...hoveringProps}
|
|
|
|
/>;
|
2019-12-16 17:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let removeAvatarBtn;
|
|
|
|
if (avatarUrl && removeAvatar) {
|
|
|
|
removeAvatarBtn = <AccessibleButton onClick={removeAvatar} kind="link_sm">
|
2021-07-20 00:43:11 +03:00
|
|
|
{ _t("Remove") }
|
2019-12-16 17:58:12 +03:00
|
|
|
</AccessibleButton>;
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:00:51 +03:00
|
|
|
const avatarClasses = classNames({
|
|
|
|
"mx_AvatarSetting_avatar": true,
|
2020-12-22 11:29:35 +03:00
|
|
|
"mx_AvatarSetting_avatar_hovering": isHovering && uploadAvatar,
|
2020-09-22 06:09:46 +03:00
|
|
|
});
|
2020-09-22 06:00:51 +03:00
|
|
|
return <div className={avatarClasses}>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ avatarElement }
|
2020-09-22 06:00:51 +03:00
|
|
|
<div className="mx_AvatarSetting_hover">
|
|
|
|
<div className="mx_AvatarSetting_hoverBg" />
|
2021-07-20 00:43:11 +03:00
|
|
|
<span>{ _t("Upload") }</span>
|
2020-09-22 06:00:51 +03:00
|
|
|
</div>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ uploadAvatarBtn }
|
|
|
|
{ removeAvatarBtn }
|
2019-12-16 17:58:12 +03:00
|
|
|
</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AvatarSetting;
|