您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Avatar.js 5.1KB

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