diff options
author | Sasha Sorokin <dafri.nochiterov8@gmail.com> | 2020-07-09 18:01:30 +0700 |
---|---|---|
committer | Thibaut Girka <thib@sitedethib.com> | 2020-07-10 16:59:53 +0200 |
commit | 042c32ea3bcb4f5986dcb6b39a25a8cead3bf86a (patch) | |
tree | 98f069dea26b4f2d7d3f023b69c6a5307bd983c6 /app/javascript/flavours/glitch/features/account_gallery/components | |
parent | 50544627592db864cfe9c7ddb138b299344f0d44 (diff) |
[Glitch] Replace repetitive blurhash code with component (#14267)
Port 61c07c37317f01c1ab4981826704750fe9937fe7 to glitch-soc Signed-off-by: Thibaut Girka <thib@sitedethib.com>
Diffstat (limited to 'app/javascript/flavours/glitch/features/account_gallery/components')
-rw-r--r-- | app/javascript/flavours/glitch/features/account_gallery/components/media_item.js | 40 |
1 files changed, 9 insertions, 31 deletions
diff --git a/app/javascript/flavours/glitch/features/account_gallery/components/media_item.js b/app/javascript/flavours/glitch/features/account_gallery/components/media_item.js index f1cb3f9e4..b88f23aa4 100644 --- a/app/javascript/flavours/glitch/features/account_gallery/components/media_item.js +++ b/app/javascript/flavours/glitch/features/account_gallery/components/media_item.js @@ -1,7 +1,7 @@ -import { decode } from 'blurhash'; +import Blurhash from 'flavours/glitch/components/blurhash'; import classNames from 'classnames'; import Icon from 'flavours/glitch/components/icon'; -import { autoPlayGif, displayMedia } from 'flavours/glitch/util/initial_state'; +import { autoPlayGif, displayMedia, useBlurhash } from 'flavours/glitch/util/initial_state'; import { isIOS } from 'flavours/glitch/util/is_mobile'; import PropTypes from 'prop-types'; import React from 'react'; @@ -21,34 +21,6 @@ export default class MediaItem extends ImmutablePureComponent { loaded: false, }; - componentDidMount () { - if (this.props.attachment.get('blurhash')) { - this._decode(); - } - } - - componentDidUpdate (prevProps) { - if (prevProps.attachment.get('blurhash') !== this.props.attachment.get('blurhash') && this.props.attachment.get('blurhash')) { - this._decode(); - } - } - - _decode () { - const hash = this.props.attachment.get('blurhash'); - const pixels = decode(hash, 32, 32); - - if (pixels) { - const ctx = this.canvas.getContext('2d'); - const imageData = new ImageData(pixels, 32, 32); - - ctx.putImageData(imageData, 0, 0); - } - } - - setCanvasRef = c => { - this.canvas = c; - } - handleImageLoad = () => { this.setState({ loaded: true }); } @@ -149,7 +121,13 @@ export default class MediaItem extends ImmutablePureComponent { return ( <div className='account-gallery__item' style={{ width, height }}> <a className='media-gallery__item-thumbnail' href={status.get('url')} onClick={this.handleClick} title={title} target='_blank' rel='noopener noreferrer'> - <canvas width={32} height={32} ref={this.setCanvasRef} className={classNames('media-gallery__preview', { 'media-gallery__preview--hidden': visible && loaded })} /> + <Blurhash + hash={attachment.get('blurhash')} + className={classNames('media-gallery__preview', { + 'media-gallery__preview--hidden': visible && loaded, + })} + dummy={!useBlurhash} + /> {visible ? thumbnail : icon} </a> </div> |