You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Avatar.js 5.6KB

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