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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * URL of the avatar, if any.
  44. */
  45. url: ?string,
  46. }
  47. type State = {
  48. avatarFailed: boolean
  49. }
  50. export const DEFAULT_SIZE = 65;
  51. /**
  52. * Implements a class to render avatars in the app.
  53. */
  54. class Avatar<P: Props> extends PureComponent<P, State> {
  55. /**
  56. * Instantiates a new {@code Component}.
  57. *
  58. * @inheritdoc
  59. */
  60. constructor(props: P) {
  61. super(props);
  62. this.state = {
  63. avatarFailed: false
  64. };
  65. this._onAvatarLoadError = this._onAvatarLoadError.bind(this);
  66. }
  67. /**
  68. * Implements {@code Component#componentDidUpdate}.
  69. *
  70. * @inheritdoc
  71. */
  72. componentDidUpdate(prevProps: P) {
  73. if (prevProps.url !== this.props.url) {
  74. // URI changed, so we need to try to fetch it again.
  75. // Eslint doesn't like this statement, but based on the React doc, it's safe if it's
  76. // wrapped in a condition: https://reactjs.org/docs/react-component.html#componentdidupdate
  77. // eslint-disable-next-line react/no-did-update-set-state
  78. this.setState({
  79. avatarFailed: false
  80. });
  81. }
  82. }
  83. /**
  84. * Implements {@code Componenr#render}.
  85. *
  86. * @inheritdoc
  87. */
  88. render() {
  89. const {
  90. _initialsBase,
  91. _loadableAvatarUrl,
  92. className,
  93. colorBase,
  94. id,
  95. size,
  96. url
  97. } = this.props;
  98. const { avatarFailed } = this.state;
  99. const avatarProps = {
  100. className,
  101. color: undefined,
  102. id,
  103. initials: undefined,
  104. onAvatarLoadError: undefined,
  105. size,
  106. url: undefined
  107. };
  108. // _loadableAvatarUrl is validated that it can be loaded, but uri (if present) is not, so
  109. // we still need to do a check for that. And an explicitly provided URI is higher priority than
  110. // an avatar URL anyhow.
  111. const effectiveURL = (!avatarFailed && url) || _loadableAvatarUrl;
  112. if (effectiveURL) {
  113. avatarProps.onAvatarLoadError = this._onAvatarLoadError;
  114. avatarProps.url = effectiveURL;
  115. } else {
  116. const initials = getInitials(_initialsBase);
  117. if (initials) {
  118. avatarProps.color = getAvatarColor(colorBase || _initialsBase);
  119. avatarProps.initials = initials;
  120. }
  121. }
  122. return (
  123. <StatelessAvatar
  124. { ...avatarProps } />
  125. );
  126. }
  127. _onAvatarLoadError: () => void;
  128. /**
  129. * Callback to handle the error while loading of the avatar URI.
  130. *
  131. * @returns {void}
  132. */
  133. _onAvatarLoadError() {
  134. this.setState({
  135. avatarFailed: true
  136. });
  137. }
  138. }
  139. /**
  140. * Maps part of the Redux state to the props of this component.
  141. *
  142. * @param {Object} state - The Redux state.
  143. * @param {Props} ownProps - The own props of the component.
  144. * @returns {Props}
  145. */
  146. export function _mapStateToProps(state: Object, ownProps: Props) {
  147. const { displayName, participantId } = ownProps;
  148. const _participant = participantId && getParticipantById(state, participantId);
  149. const _initialsBase = (_participant && _participant.name) || displayName;
  150. return {
  151. _initialsBase,
  152. _loadableAvatarUrl: _participant && _participant.loadableAvatarUrl
  153. };
  154. }
  155. export default connect(_mapStateToProps)(Avatar);