2016-01-15 15:02:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2018-03-12 18:24:32 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2019-08-08 22:21:53 +03:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2020-02-21 13:41:33 +03:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2016-01-15 15:02:28 +03:00
|
|
|
|
|
|
|
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-02-21 13:41:33 +03:00
|
|
|
import React, {useCallback, useContext, useEffect, useMemo, useState} from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as AvatarLogic from '../../../Avatar';
|
2019-02-08 19:44:03 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2017-01-25 01:41:52 +03:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2019-12-17 20:26:12 +03:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2020-02-21 13:41:33 +03:00
|
|
|
import {useEventEmitter} from "../../../hooks/useEventEmitter";
|
2016-01-15 15:02:28 +03:00
|
|
|
|
2020-02-21 13:41:33 +03:00
|
|
|
const useImageUrl = ({url, urls, idName, name, defaultToInitialLetter}) => {
|
|
|
|
const [imageUrls, setUrls] = useState([]);
|
|
|
|
const [urlsIndex, setIndex] = useState();
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2020-02-21 13:41:33 +03:00
|
|
|
const onError = () => {
|
|
|
|
const nextIndex = urlsIndex + 1;
|
|
|
|
if (nextIndex < imageUrls.length) {
|
|
|
|
// try the next one
|
|
|
|
setIndex(nextIndex);
|
2018-02-06 20:50:53 +03:00
|
|
|
}
|
2020-02-21 13:41:33 +03:00
|
|
|
};
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2020-02-21 13:41:33 +03:00
|
|
|
const defaultImageUrl = useMemo(() => AvatarLogic.defaultAvatarUrlForString(idName || name), [idName, name]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2016-01-15 20:05:05 +03:00
|
|
|
// work out the full set of urls to try to load. This is formed like so:
|
2020-02-21 13:41:33 +03:00
|
|
|
// imageUrls: [ props.url, ...props.urls, default image ]
|
2016-01-15 20:05:05 +03:00
|
|
|
|
2020-02-21 13:41:33 +03:00
|
|
|
let _urls = [];
|
2019-02-08 19:44:03 +03:00
|
|
|
if (!SettingsStore.getValue("lowBandwidth")) {
|
2020-02-21 13:41:33 +03:00
|
|
|
_urls = urls || [];
|
2019-02-08 19:44:03 +03:00
|
|
|
|
2020-02-21 13:41:33 +03:00
|
|
|
if (url) {
|
|
|
|
_urls.unshift(url); // put in urls[0]
|
2019-02-08 19:44:03 +03:00
|
|
|
}
|
2016-01-15 20:05:05 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:41:33 +03:00
|
|
|
if (defaultToInitialLetter) {
|
|
|
|
_urls.push(defaultImageUrl); // lowest priority
|
2016-01-15 15:02:28 +03:00
|
|
|
}
|
2019-08-08 22:21:53 +03:00
|
|
|
|
|
|
|
// deduplicate URLs
|
2020-02-21 13:41:33 +03:00
|
|
|
_urls = Array.from(new Set(_urls));
|
|
|
|
|
|
|
|
setIndex(0);
|
|
|
|
setUrls(_urls);
|
|
|
|
}, [url, ...(urls || [])]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
|
|
|
const cli = useContext(MatrixClientContext);
|
|
|
|
const onClientSync = useCallback((syncState, prevState) => {
|
|
|
|
// Consider the client reconnected if there is no error with syncing.
|
|
|
|
// This means the state could be RECONNECTING, SYNCING, PREPARED or CATCHUP.
|
|
|
|
const reconnected = syncState !== "ERROR" && prevState !== syncState;
|
|
|
|
if (reconnected && urlsIndex > 0 ) { // Did we fall back?
|
|
|
|
// Start from the highest priority URL again
|
|
|
|
setIndex(0);
|
2016-01-15 15:02:28 +03:00
|
|
|
}
|
2020-02-21 13:41:33 +03:00
|
|
|
}, [urlsIndex]);
|
|
|
|
useEventEmitter(cli, "sync", onClientSync);
|
|
|
|
|
|
|
|
const imageUrl = imageUrls[urlsIndex];
|
|
|
|
return [imageUrl, imageUrl === defaultImageUrl, onError];
|
|
|
|
};
|
|
|
|
|
|
|
|
const BaseAvatar = (props) => {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
idName,
|
|
|
|
title,
|
|
|
|
url,
|
|
|
|
urls,
|
|
|
|
width=40,
|
|
|
|
height=40,
|
|
|
|
resizeMethod="crop", // eslint-disable-line no-unused-vars
|
|
|
|
defaultToInitialLetter=true,
|
|
|
|
onClick,
|
|
|
|
inputRef,
|
|
|
|
...otherProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const [imageUrl, isDefault, onError] = useImageUrl({url, urls, idName, name, defaultToInitialLetter});
|
|
|
|
|
|
|
|
if (isDefault) {
|
|
|
|
const initialLetter = AvatarLogic.getInitialLetter(name);
|
|
|
|
const textNode = (
|
|
|
|
<span
|
|
|
|
className="mx_BaseAvatar_initial"
|
|
|
|
aria-hidden="true"
|
|
|
|
style={{
|
|
|
|
fontSize: (width * 0.65) + "px",
|
2017-02-02 21:47:24 +03:00
|
|
|
width: width + "px",
|
2020-02-21 13:41:33 +03:00
|
|
|
lineHeight: height + "px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{ initialLetter }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
const imgNode = (
|
|
|
|
<img
|
|
|
|
className="mx_BaseAvatar_image"
|
|
|
|
src={imageUrl}
|
|
|
|
alt=""
|
|
|
|
title={title}
|
|
|
|
onError={onError}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
aria-hidden="true" />
|
|
|
|
);
|
|
|
|
|
2017-01-06 23:01:37 +03:00
|
|
|
if (onClick != null) {
|
|
|
|
return (
|
2019-12-23 15:24:49 +03:00
|
|
|
<AccessibleButton
|
2020-02-21 13:41:33 +03:00
|
|
|
{...otherProps}
|
|
|
|
element="span"
|
|
|
|
className="mx_BaseAvatar"
|
2017-02-02 20:36:26 +03:00
|
|
|
onClick={onClick}
|
2019-12-23 15:24:49 +03:00
|
|
|
inputRef={inputRef}
|
2020-02-21 13:41:33 +03:00
|
|
|
>
|
|
|
|
{ textNode }
|
|
|
|
{ imgNode }
|
|
|
|
</AccessibleButton>
|
2017-01-06 23:01:37 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2020-02-21 13:41:33 +03:00
|
|
|
<span className="mx_BaseAvatar" ref={inputRef} {...otherProps}>
|
|
|
|
{ textNode }
|
|
|
|
{ imgNode }
|
|
|
|
</span>
|
2017-01-06 23:01:37 +03:00
|
|
|
);
|
|
|
|
}
|
2020-02-21 13:41:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (onClick != null) {
|
|
|
|
return (
|
|
|
|
<AccessibleButton
|
|
|
|
className="mx_BaseAvatar mx_BaseAvatar_image"
|
|
|
|
element='img'
|
|
|
|
src={imageUrl}
|
|
|
|
onClick={onClick}
|
|
|
|
onError={onError}
|
|
|
|
width={width} height={height}
|
|
|
|
title={title} alt=""
|
|
|
|
inputRef={inputRef}
|
|
|
|
{...otherProps} />
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<img
|
|
|
|
className="mx_BaseAvatar mx_BaseAvatar_image"
|
|
|
|
src={imageUrl}
|
|
|
|
onError={onError}
|
|
|
|
width={width} height={height}
|
|
|
|
title={title} alt=""
|
|
|
|
ref={inputRef}
|
|
|
|
{...otherProps} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseAvatar.displayName = "BaseAvatar";
|
|
|
|
|
|
|
|
BaseAvatar.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired, // The name (first initial used as default)
|
|
|
|
idName: PropTypes.string, // ID for generating hash colours
|
|
|
|
title: PropTypes.string, // onHover title text
|
|
|
|
url: PropTypes.string, // highest priority of them all, shortcut to set in urls[0]
|
|
|
|
urls: PropTypes.array, // [highest_priority, ... , lowest_priority]
|
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
|
|
|
// XXX resizeMethod not actually used.
|
|
|
|
resizeMethod: PropTypes.string,
|
|
|
|
defaultToInitialLetter: PropTypes.bool, // true to add default url
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
inputRef: PropTypes.oneOfType([
|
|
|
|
// Either a function
|
|
|
|
PropTypes.func,
|
|
|
|
// Or the instance of a DOM native element
|
|
|
|
PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
|
|
|
|
]),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default BaseAvatar;
|