2020-11-05 18:42:45 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
2020-12-28 00:46:56 +03:00
|
|
|
import React, {useContext, useRef, useState} from 'react';
|
2020-12-28 00:01:51 +03:00
|
|
|
import {EventType} from 'matrix-js-sdk/src/@types/event';
|
2020-11-05 18:42:45 +03:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import AccessibleButton from "./AccessibleButton";
|
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
|
|
|
import {useTimeout} from "../../../hooks/useTimeout";
|
2020-11-19 14:11:57 +03:00
|
|
|
import Analytics from "../../../Analytics";
|
|
|
|
import CountlyAnalytics from '../../../CountlyAnalytics';
|
2020-12-28 00:01:51 +03:00
|
|
|
import RoomContext from "../../../contexts/RoomContext";
|
2020-11-05 18:42:45 +03:00
|
|
|
|
|
|
|
export const AVATAR_SIZE = 52;
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
hasAvatar: boolean;
|
|
|
|
noAvatarLabel?: string;
|
|
|
|
hasAvatarLabel?: string;
|
|
|
|
setAvatarUrl(url: string): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MiniAvatarUploader: React.FC<IProps> = ({ hasAvatar, hasAvatarLabel, noAvatarLabel, setAvatarUrl, children }) => {
|
|
|
|
const cli = useContext(MatrixClientContext);
|
|
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const [hover, setHover] = useState(false);
|
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
|
|
|
|
useTimeout(() => {
|
|
|
|
setShow(true);
|
2020-11-10 13:00:55 +03:00
|
|
|
}, 3000); // show after 3 seconds
|
2020-11-05 18:42:45 +03:00
|
|
|
useTimeout(() => {
|
|
|
|
setShow(false);
|
2020-11-10 13:00:55 +03:00
|
|
|
}, 13000); // hide after being shown for 10 seconds
|
2020-11-05 18:42:45 +03:00
|
|
|
|
|
|
|
const uploadRef = useRef<HTMLInputElement>();
|
|
|
|
|
2020-11-10 13:00:55 +03:00
|
|
|
const label = (hasAvatar || busy) ? hasAvatarLabel : noAvatarLabel;
|
2020-11-05 18:42:45 +03:00
|
|
|
|
2020-12-28 00:01:51 +03:00
|
|
|
const {room} = useContext(RoomContext);
|
|
|
|
const canSetAvatar = room?.currentState.maySendStateEvent(EventType.RoomAvatar, cli.getUserId());
|
2020-12-28 00:25:48 +03:00
|
|
|
if (!canSetAvatar) return <React.Fragment>{ children }</React.Fragment>;
|
2020-12-28 00:01:51 +03:00
|
|
|
|
2020-12-28 00:13:01 +03:00
|
|
|
const visible = !!label && (hover || show);
|
2020-11-05 18:42:45 +03:00
|
|
|
return <React.Fragment>
|
|
|
|
<input
|
|
|
|
type="file"
|
|
|
|
ref={uploadRef}
|
|
|
|
className="mx_MiniAvatarUploader_input"
|
|
|
|
onChange={async (ev) => {
|
|
|
|
if (!ev.target.files?.length) return;
|
|
|
|
setBusy(true);
|
2020-11-19 14:11:57 +03:00
|
|
|
Analytics.trackEvent("mini_avatar", "upload");
|
|
|
|
CountlyAnalytics.instance.track("mini_avatar_upload");
|
2020-11-05 18:42:45 +03:00
|
|
|
const file = ev.target.files[0];
|
|
|
|
const uri = await cli.uploadContent(file);
|
|
|
|
await setAvatarUrl(uri);
|
|
|
|
setBusy(false);
|
|
|
|
}}
|
|
|
|
accept="image/*"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<AccessibleButton
|
|
|
|
className={classNames("mx_MiniAvatarUploader", {
|
|
|
|
mx_MiniAvatarUploader_busy: busy,
|
|
|
|
mx_MiniAvatarUploader_hasAvatar: hasAvatar,
|
|
|
|
})}
|
|
|
|
disabled={busy}
|
|
|
|
onClick={() => {
|
|
|
|
uploadRef.current.click();
|
|
|
|
}}
|
|
|
|
onMouseOver={() => setHover(true)}
|
|
|
|
onMouseLeave={() => setHover(false)}
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
|
2020-12-28 00:13:01 +03:00
|
|
|
<div className={classNames("mx_Tooltip", {
|
|
|
|
"mx_Tooltip_visible": visible,
|
|
|
|
"mx_Tooltip_invisible": !visible,
|
|
|
|
})}>
|
|
|
|
<div className="mx_Tooltip_chevron" />
|
|
|
|
{ label }
|
|
|
|
</div>
|
2020-11-05 18:42:45 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
</React.Fragment>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MiniAvatarUploader;
|