Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Avatar.js 5.3KB

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