Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Avatar.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import { getParticipantById } from '../../participants';
  4. import { connect } from '../../redux';
  5. import { getAvatarColor, getInitials, isCORSAvatarURL } from '../functions';
  6. import { StatelessAvatar } from './';
  7. export type Props = {
  8. /**
  9. * The URL patterns for URLs that needs to be handled with CORS.
  10. */
  11. _corsAvatarURLs: Array<string>,
  12. /**
  13. * Custom avatar backgrounds from branding.
  14. */
  15. _customAvatarBackgrounds: Array<string>,
  16. /**
  17. * The string we base the initials on (this is generated from a list of precedences).
  18. */
  19. _initialsBase: ?string,
  20. /**
  21. * An URL that we validated that it can be loaded.
  22. */
  23. _loadableAvatarUrl: ?string,
  24. /**
  25. * Indicates whether _loadableAvatarUrl should use CORS or not.
  26. */
  27. _loadableAvatarUrlUseCORS: ?boolean,
  28. /**
  29. * A prop to maintain compatibility with web.
  30. */
  31. className?: string,
  32. /**
  33. * A string to override the initials to generate a color of. This is handy if you don't want to make
  34. * the background color match the string that the initials are generated from.
  35. */
  36. colorBase?: string,
  37. /**
  38. * Display name of the entity to render an avatar for (if any). This is handy when we need
  39. * an avatar for a non-participasnt entity (e.g. A recent list item).
  40. */
  41. displayName?: string,
  42. /**
  43. * Whether or not to update the background color of the avatar.
  44. */
  45. dynamicColor?: Boolean,
  46. /**
  47. * ID of the element, if any.
  48. */
  49. id?: string,
  50. /**
  51. * The ID of the participant to render an avatar for (if it's a participant avatar).
  52. */
  53. participantId?: string,
  54. /**
  55. * The size of the avatar.
  56. */
  57. size: number,
  58. /**
  59. * One of the expected status strings (e.g. 'available') to render a badge on the avatar, if necessary.
  60. */
  61. status?: ?string,
  62. /**
  63. * TestId of the element, if any.
  64. */
  65. testId?: string,
  66. /**
  67. * URL of the avatar, if any.
  68. */
  69. url: ?string,
  70. /**
  71. * Indicates whether to load the avatar using CORS or not.
  72. */
  73. useCORS?: ?boolean
  74. }
  75. type State = {
  76. avatarFailed: boolean,
  77. isUsingCORS: boolean
  78. }
  79. export const DEFAULT_SIZE = 65;
  80. /**
  81. * Implements a class to render avatars in the app.
  82. */
  83. class Avatar<P: Props> extends PureComponent<P, State> {
  84. /**
  85. * Default values for {@code Avatar} component's properties.
  86. *
  87. * @static
  88. */
  89. static defaultProps = {
  90. dynamicColor: true
  91. };
  92. /**
  93. * Instantiates a new {@code Component}.
  94. *
  95. * @inheritdoc
  96. */
  97. constructor(props: P) {
  98. super(props);
  99. const {
  100. _corsAvatarURLs,
  101. url,
  102. useCORS
  103. } = props;
  104. this.state = {
  105. avatarFailed: false,
  106. isUsingCORS: Boolean(useCORS) || Boolean(url && isCORSAvatarURL(url, _corsAvatarURLs))
  107. };
  108. this._onAvatarLoadError = this._onAvatarLoadError.bind(this);
  109. }
  110. /**
  111. * Implements {@code Component#componentDidUpdate}.
  112. *
  113. * @inheritdoc
  114. */
  115. componentDidUpdate(prevProps: P) {
  116. const { _corsAvatarURLs, url } = this.props;
  117. if (prevProps.url !== url) {
  118. // URI changed, so we need to try to fetch it again.
  119. // Eslint doesn't like this statement, but based on the React doc, it's safe if it's
  120. // wrapped in a condition: https://reactjs.org/docs/react-component.html#componentdidupdate
  121. // eslint-disable-next-line react/no-did-update-set-state
  122. this.setState({
  123. avatarFailed: false,
  124. isUsingCORS: Boolean(this.props.useCORS) || Boolean(url && isCORSAvatarURL(url, _corsAvatarURLs))
  125. });
  126. }
  127. }
  128. /**
  129. * Implements {@code Componenr#render}.
  130. *
  131. * @inheritdoc
  132. */
  133. render() {
  134. const {
  135. _customAvatarBackgrounds,
  136. _initialsBase,
  137. _loadableAvatarUrl,
  138. _loadableAvatarUrlUseCORS,
  139. className,
  140. colorBase,
  141. dynamicColor,
  142. id,
  143. size,
  144. status,
  145. testId,
  146. url
  147. } = this.props;
  148. const { avatarFailed, isUsingCORS } = this.state;
  149. const avatarProps = {
  150. className,
  151. color: undefined,
  152. id,
  153. initials: undefined,
  154. onAvatarLoadError: undefined,
  155. onAvatarLoadErrorParams: undefined,
  156. size,
  157. status,
  158. testId,
  159. url: undefined,
  160. useCORS: isUsingCORS
  161. };
  162. // _loadableAvatarUrl is validated that it can be loaded, but uri (if present) is not, so
  163. // we still need to do a check for that. And an explicitly provided URI is higher priority than
  164. // an avatar URL anyhow.
  165. const useReduxLoadableAvatarURL = avatarFailed || !url;
  166. const effectiveURL = useReduxLoadableAvatarURL ? _loadableAvatarUrl : url;
  167. if (effectiveURL) {
  168. avatarProps.onAvatarLoadError = this._onAvatarLoadError;
  169. if (useReduxLoadableAvatarURL) {
  170. avatarProps.onAvatarLoadErrorParams = { dontRetry: true };
  171. avatarProps.useCORS = _loadableAvatarUrlUseCORS;
  172. }
  173. avatarProps.url = effectiveURL;
  174. }
  175. const initials = getInitials(_initialsBase);
  176. if (initials) {
  177. if (dynamicColor) {
  178. avatarProps.color = getAvatarColor(colorBase || _initialsBase, _customAvatarBackgrounds);
  179. }
  180. avatarProps.initials = initials;
  181. }
  182. return (
  183. <StatelessAvatar
  184. { ...avatarProps } />
  185. );
  186. }
  187. _onAvatarLoadError: () => void;
  188. /**
  189. * Callback to handle the error while loading of the avatar URI.
  190. *
  191. * @param {Object} params - An object with parameters.
  192. * @param {boolean} params.dontRetry - If false we will retry to load the Avatar with different CORS mode.
  193. * @returns {void}
  194. */
  195. _onAvatarLoadError(params = {}) {
  196. const { dontRetry = false } = params;
  197. if (Boolean(this.props.useCORS) === this.state.isUsingCORS && !dontRetry) {
  198. // try different mode of loading the avatar.
  199. this.setState({
  200. isUsingCORS: !this.state.isUsingCORS
  201. });
  202. } else {
  203. // we already have tried loading the avatar with and without CORS and it failed.
  204. this.setState({
  205. avatarFailed: true
  206. });
  207. }
  208. }
  209. }
  210. /**
  211. * Maps part of the Redux state to the props of this component.
  212. *
  213. * @param {Object} state - The Redux state.
  214. * @param {Props} ownProps - The own props of the component.
  215. * @returns {Props}
  216. */
  217. export function _mapStateToProps(state: Object, ownProps: Props) {
  218. const { colorBase, displayName, participantId } = ownProps;
  219. const _participant: ?Object = participantId && getParticipantById(state, participantId);
  220. const _initialsBase = _participant?.name ?? displayName;
  221. const { corsAvatarURLs } = state['features/base/config'];
  222. return {
  223. _customAvatarBackgrounds: state['features/dynamic-branding'].avatarBackgrounds,
  224. _corsAvatarURLs: corsAvatarURLs,
  225. _initialsBase,
  226. _loadableAvatarUrl: _participant?.loadableAvatarUrl,
  227. _loadableAvatarUrlUseCORS: _participant?.loadableAvatarUrlUseCORS,
  228. colorBase
  229. };
  230. }
  231. export default connect(_mapStateToProps)(Avatar);