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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // @flow
  2. import React from 'react';
  3. import { Image, Text, View } from 'react-native';
  4. import { connect } from '../../../redux';
  5. import { type StyleType } from '../../../styles';
  6. import AbstractAvatar, {
  7. _mapStateToProps,
  8. type Props as AbstractProps,
  9. DEFAULT_SIZE
  10. } from '../AbstractAvatar';
  11. import RemoteAvatar, { DEFAULT_AVATAR } from './RemoteAvatar';
  12. import styles from './styles';
  13. type Props = AbstractProps & {
  14. /**
  15. * External style of the component.
  16. */
  17. style?: StyleType
  18. }
  19. /**
  20. * Implements an avatar component that has 4 ways to render an avatar:
  21. *
  22. * - Based on an explicit avatar URI, if provided
  23. * - Gravatar, if there is any
  24. * - Based on initials generated from name or email
  25. * - Default avatar icon, if any of the above fails
  26. */
  27. class Avatar extends AbstractAvatar<Props> {
  28. _onAvatarLoadError: () => void;
  29. /**
  30. * Implements {@code AbstractAvatar#_renderDefaultAvatar}.
  31. *
  32. * @inheritdoc
  33. */
  34. _renderDefaultAvatar() {
  35. return this._wrapAvatar(
  36. <Image
  37. source = { DEFAULT_AVATAR }
  38. style = { [
  39. styles.avatarContent(this.props.size || DEFAULT_SIZE),
  40. styles.staticAvatar
  41. ] } />
  42. );
  43. }
  44. /**
  45. * Implements {@code AbstractAvatar#_renderGravatar}.
  46. *
  47. * @inheritdoc
  48. */
  49. _renderInitialsAvatar(initials, color) {
  50. return this._wrapAvatar(
  51. <View
  52. style = { [
  53. styles.initialsContainer,
  54. {
  55. backgroundColor: color
  56. }
  57. ] }>
  58. <Text style = { styles.initialsText(this.props.size || DEFAULT_SIZE) }> { initials } </Text>
  59. </View>
  60. );
  61. }
  62. /**
  63. * Implements {@code AbstractAvatar#_renderGravatar}.
  64. *
  65. * @inheritdoc
  66. */
  67. _renderURLAvatar(uri) {
  68. return this._wrapAvatar(
  69. <RemoteAvatar
  70. onError = { this._onAvatarLoadError }
  71. size = { this.props.size || DEFAULT_SIZE }
  72. uri = { uri } />
  73. );
  74. }
  75. /**
  76. * Wraps an avatar into a common wrapper.
  77. *
  78. * @param {React#Component} avatar - The avatar component.
  79. * @returns {React#Component}
  80. */
  81. _wrapAvatar(avatar) {
  82. return (
  83. <View
  84. style = { [
  85. styles.avatarContainer(this.props.size || DEFAULT_SIZE),
  86. this.props.style
  87. ] }>
  88. { avatar }
  89. </View>
  90. );
  91. }
  92. }
  93. export default connect(_mapStateToProps)(Avatar);