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.0KB

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