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

Avatar.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * URL of the avatar, if any.
  45. */
  46. url: ?string,
  47. }
  48. type State = {
  49. avatarFailed: boolean
  50. }
  51. export const DEFAULT_SIZE = 65;
  52. /**
  53. * Implements a class to render avatars in the app.
  54. */
  55. class Avatar<P: Props> extends PureComponent<P, State> {
  56. /**
  57. * Instantiates a new {@code Component}.
  58. *
  59. * @inheritdoc
  60. */
  61. constructor(props: P) {
  62. super(props);
  63. this.state = {
  64. avatarFailed: false
  65. };
  66. this._onAvatarLoadError = this._onAvatarLoadError.bind(this);
  67. }
  68. /**
  69. * Implements {@code Component#componentDidUpdate}.
  70. *
  71. * @inheritdoc
  72. */
  73. componentDidUpdate(prevProps: P) {
  74. if (prevProps.url !== this.props.url) {
  75. // URI changed, so we need to try to fetch it again.
  76. // Eslint doesn't like this statement, but based on the React doc, it's safe if it's
  77. // wrapped in a condition: https://reactjs.org/docs/react-component.html#componentdidupdate
  78. // eslint-disable-next-line react/no-did-update-set-state
  79. this.setState({
  80. avatarFailed: false
  81. });
  82. }
  83. }
  84. /**
  85. * Implements {@code Componenr#render}.
  86. *
  87. * @inheritdoc
  88. */
  89. render() {
  90. const {
  91. _initialsBase,
  92. _loadableAvatarUrl,
  93. className,
  94. colorBase,
  95. id,
  96. size,
  97. url
  98. } = this.props;
  99. const { avatarFailed } = this.state;
  100. const avatarProps = {
  101. className,
  102. color: undefined,
  103. id,
  104. initials: undefined,
  105. onAvatarLoadError: undefined,
  106. size,
  107. url: undefined
  108. };
  109. // _loadableAvatarUrl is validated that it can be loaded, but uri (if present) is not, so
  110. // we still need to do a check for that. And an explicitly provided URI is higher priority than
  111. // an avatar URL anyhow.
  112. const effectiveURL = (!avatarFailed && url) || _loadableAvatarUrl;
  113. if (effectiveURL) {
  114. avatarProps.onAvatarLoadError = this._onAvatarLoadError;
  115. avatarProps.url = effectiveURL;
  116. }
  117. const initials = getInitials(_initialsBase);
  118. if (initials) {
  119. avatarProps.color = getAvatarColor(colorBase || _initialsBase);
  120. avatarProps.initials = initials;
  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 { colorBase, displayName, participantId } = ownProps;
  148. const _participant: ?Object = participantId && getParticipantById(state, participantId);
  149. const _initialsBase = _participant?.name ?? displayName;
  150. const screenShares = state['features/video-layout'].screenShares || [];
  151. let _loadableAvatarUrl = _participant?.loadableAvatarUrl;
  152. if (participantId && screenShares.includes(participantId)) {
  153. _loadableAvatarUrl = IconShareDesktop;
  154. }
  155. return {
  156. _initialsBase,
  157. _loadableAvatarUrl,
  158. colorBase: !colorBase && _participant ? _participant.id : colorBase
  159. };
  160. }
  161. export default connect(_mapStateToProps)(Avatar);