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.

AvatarImage.native.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import React, { Component } from 'react';
  2. import { Image, View } from 'react-native';
  3. import { Platform } from '../../react';
  4. /**
  5. * The default avatar to be used, in case the requested URI is not available
  6. * or fails to be loaded.
  7. *
  8. * This is an inline version of images/avatar2.png.
  9. *
  10. * @type {string}
  11. */
  12. const DEFAULT_AVATAR = require('./defaultAvatar.png');
  13. /**
  14. * The amount of time to wait when the avatar URI is undefined before we start
  15. * showing a default locally generated one. Note that since we have no URI, we
  16. * have nothing we can cache, so the color will be random.
  17. *
  18. * @type {number}
  19. */
  20. const UNDEFINED_AVATAR_TIMEOUT = 1000;
  21. /**
  22. * Implements an Image component wrapper, which returns a default image if the
  23. * requested one fails to load. The default image background is chosen by
  24. * hashing the URL of the image.
  25. */
  26. export default class AvatarImage extends Component {
  27. /**
  28. * AvatarImage component's property types.
  29. *
  30. * @static
  31. */
  32. static propTypes = {
  33. /**
  34. * If set to <tt>true</tt> it will not load the URL, but will use the
  35. * default instead.
  36. */
  37. forceDefault: React.PropTypes.bool,
  38. /**
  39. * The source the {@link Image}.
  40. */
  41. source: React.PropTypes.object,
  42. /**
  43. * The optional style to add to the {@link Image} in order to customize
  44. * its base look (and feel).
  45. */
  46. style: React.PropTypes.object
  47. };
  48. /**
  49. * Initializes new AvatarImage component.
  50. *
  51. * @param {Object} props - Component props.
  52. */
  53. constructor(props) {
  54. super(props);
  55. this.state = {
  56. failed: false,
  57. showDefault: false
  58. };
  59. this.componentWillReceiveProps(props);
  60. this._onError = this._onError.bind(this);
  61. }
  62. /**
  63. * Notifies this mounted React Component that it will receive new props.
  64. * If the URI is undefined, wait {@code UNDEFINED_AVATAR_TIMEOUT} ms and
  65. * start showing a default locally generated avatar afterwards.
  66. *
  67. * Once a URI is passed, it will be rendered instead, except if loading it
  68. * fails, in which case we fallback to a locally generated avatar again.
  69. *
  70. * @inheritdoc
  71. * @param {Object} nextProps - The read-only React Component props that this
  72. * instance will receive.
  73. * @returns {void}
  74. */
  75. componentWillReceiveProps(nextProps) {
  76. const prevURI = this.props.source && this.props.source.uri;
  77. const nextURI = nextProps.source && nextProps.source.uri;
  78. if (typeof prevURI === 'undefined') {
  79. clearTimeout(this._timeout);
  80. if (typeof nextURI === 'undefined') {
  81. this._timeout = setTimeout(() => {
  82. this.setState({ showDefault: true });
  83. }, UNDEFINED_AVATAR_TIMEOUT);
  84. } else {
  85. this.setState({ showDefault: nextProps.forceDefault });
  86. }
  87. }
  88. }
  89. /**
  90. * Clear the timer just in case. See {@code componentWillReceiveProps} for
  91. * details.
  92. *
  93. * @inheritdoc
  94. */
  95. componentWillUnmount() {
  96. clearTimeout(this._timeout);
  97. }
  98. /**
  99. * Computes a hash over the URI and returns a HSL background color. We use
  100. * 75% as lightness, for nice pastel style colors.
  101. *
  102. * @returns {string} - The HSL CSS property.
  103. * @private
  104. */
  105. _getBackgroundColor() {
  106. const uri = this.props.source.uri;
  107. let hash = 0;
  108. // If we have no URI yet we have no data to hash from, so use a random
  109. // value.
  110. if (typeof uri === 'undefined') {
  111. hash = Math.floor(Math.random() * 360);
  112. } else {
  113. /* eslint-disable no-bitwise */
  114. for (let i = 0; i < uri.length; i++) {
  115. hash = uri.charCodeAt(i) + ((hash << 5) - hash);
  116. hash |= 0; // Convert to 32bit integer
  117. }
  118. /* eslint-enable no-bitwise */
  119. }
  120. return `hsl(${hash % 360}, 100%, 75%)`;
  121. }
  122. /**
  123. * Error handler for image loading. When an image fails to load we'll mark
  124. * it as failed and load the default URI instead.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _onError() {
  130. this.setState({ failed: true });
  131. }
  132. /**
  133. * Implements React's {@link Component#render()}.
  134. *
  135. * @inheritdoc
  136. */
  137. render() {
  138. // eslint-disable-next-line no-unused-vars
  139. const { forceDefault, source, style, ...props } = this.props;
  140. const { failed, showDefault } = this.state;
  141. if (failed || showDefault) {
  142. const coloredBackground = {
  143. ...style,
  144. backgroundColor: this._getBackgroundColor(),
  145. overflow: 'hidden'
  146. };
  147. let element = React.createElement(Image, {
  148. ...props,
  149. source: DEFAULT_AVATAR,
  150. style: Platform.OS === 'android' ? style : coloredBackground
  151. });
  152. if (Platform.OS === 'android') {
  153. // Here we need to wrap the Image in a View because of a bug in
  154. // React Native for Android:
  155. // https://github.com/facebook/react-native/issues/3198
  156. element = React.createElement(View,
  157. { style: coloredBackground }, element);
  158. }
  159. return element;
  160. } else if (typeof source.uri === 'undefined') {
  161. return null;
  162. }
  163. // We have a URI and it's time to render it.
  164. return (
  165. <Image
  166. { ...props }
  167. onError = { this._onError }
  168. source = { source }
  169. style = { style } />
  170. );
  171. }
  172. }