diff options
author | fusagiko / takayamaki <24884114+takayamaki@users.noreply.github.com> | 2023-04-16 23:09:04 +0900 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2023-04-22 11:22:49 +0200 |
commit | 799e9917e43455405dd510ba50b8f0b0ca1af443 (patch) | |
tree | bbf85f48ffdd58f167929860399ae78c88559f89 | |
parent | 6ea91372330e5c268603a70f7bde461b1dfc623a (diff) |
[Glitch] Rewrite GIFV component with React hooks
Port cf3fa1e814c59fd25008b56a976dbc67bfc0efd0 to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
4 files changed, 70 insertions, 78 deletions
diff --git a/app/javascript/flavours/glitch/components/gifv.jsx b/app/javascript/flavours/glitch/components/gifv.jsx deleted file mode 100644 index 1ce7e7c29..000000000 --- a/app/javascript/flavours/glitch/components/gifv.jsx +++ /dev/null @@ -1,76 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; - -export default class GIFV extends React.PureComponent { - - static propTypes = { - src: PropTypes.string.isRequired, - alt: PropTypes.string, - lang: PropTypes.string, - width: PropTypes.number, - height: PropTypes.number, - onClick: PropTypes.func, - }; - - state = { - loading: true, - }; - - handleLoadedData = () => { - this.setState({ loading: false }); - }; - - componentWillReceiveProps (nextProps) { - if (nextProps.src !== this.props.src) { - this.setState({ loading: true }); - } - } - - handleClick = e => { - const { onClick } = this.props; - - if (onClick) { - e.stopPropagation(); - onClick(); - } - }; - - render () { - const { src, width, height, alt, lang } = this.props; - const { loading } = this.state; - - return ( - <div className='gifv' style={{ position: 'relative' }}> - {loading && ( - <canvas - width={width} - height={height} - role='button' - tabIndex={0} - aria-label={alt} - title={alt} - lang={lang} - onClick={this.handleClick} - /> - )} - - <video - src={src} - role='button' - tabIndex={0} - aria-label={alt} - title={alt} - lang={lang} - muted - loop - autoPlay - playsInline - onClick={this.handleClick} - onLoadedData={this.handleLoadedData} - style={{ position: loading ? 'absolute' : 'static', top: 0, left: 0 }} - /> - </div> - ); - } - -} diff --git a/app/javascript/flavours/glitch/components/gifv.tsx b/app/javascript/flavours/glitch/components/gifv.tsx new file mode 100644 index 000000000..8968170c5 --- /dev/null +++ b/app/javascript/flavours/glitch/components/gifv.tsx @@ -0,0 +1,68 @@ +import React, { useCallback, useState } from 'react'; + +type Props = { + src: string; + key: string; + alt?: string; + lang?: string; + width: number; + height: number; + onClick?: () => void; +} + +export const GIFV: React.FC<Props> = ({ + src, + alt, + lang, + width, + height, + onClick, +})=> { + const [loading, setLoading] = useState(true); + + const handleLoadedData: React.ReactEventHandler<HTMLVideoElement> = useCallback(() => { + setLoading(false); + }, [setLoading]); + + const handleClick: React.MouseEventHandler = useCallback((e) => { + if (onClick) { + e.stopPropagation(); + onClick(); + } + }, [onClick]); + + return ( + <div className='gifv' style={{ position: 'relative' }}> + {loading && ( + <canvas + width={width} + height={height} + role='button' + tabIndex={0} + aria-label={alt} + title={alt} + lang={lang} + onClick={handleClick} + /> + )} + + <video + src={src} + role='button' + tabIndex={0} + aria-label={alt} + title={alt} + lang={lang} + muted + loop + autoPlay + playsInline + onClick={handleClick} + onLoadedData={handleLoadedData} + style={{ position: loading ? 'absolute' : 'static', top: 0, left: 0 }} + /> + </div> + ); +}; + +export default GIFV; diff --git a/app/javascript/flavours/glitch/features/ui/components/focal_point_modal.jsx b/app/javascript/flavours/glitch/features/ui/components/focal_point_modal.jsx index a5637d31c..78aee8dfe 100644 --- a/app/javascript/flavours/glitch/features/ui/components/focal_point_modal.jsx +++ b/app/javascript/flavours/glitch/features/ui/components/focal_point_modal.jsx @@ -371,7 +371,7 @@ class FocalPointModal extends ImmutablePureComponent { {focals && ( <div className={classNames('focal-point', { dragging })} ref={this.setRef} onMouseDown={this.handleMouseDown} onTouchStart={this.handleTouchStart}> {media.get('type') === 'image' && <ImageLoader src={media.get('url')} width={width} height={height} alt='' />} - {media.get('type') === 'gifv' && <GIFV src={media.get('url')} width={width} height={height} />} + {media.get('type') === 'gifv' && <GIFV src={media.get('url')} key={media.get('url')} width={width} height={height} />} <div className='focal-point__preview'> <strong><FormattedMessage id='upload_modal.preview_label' defaultMessage='Preview ({ratio})' values={{ ratio: '16:9' }} /></strong> diff --git a/app/javascript/flavours/glitch/features/ui/components/media_modal.jsx b/app/javascript/flavours/glitch/features/ui/components/media_modal.jsx index fd2bd43cf..6ca96b743 100644 --- a/app/javascript/flavours/glitch/features/ui/components/media_modal.jsx +++ b/app/javascript/flavours/glitch/features/ui/components/media_modal.jsx @@ -188,7 +188,7 @@ class MediaModal extends ImmutablePureComponent { src={image.get('url')} width={width} height={height} - key={image.get('preview_url')} + key={image.get('url')} alt={image.get('description')} lang={language} onClick={this.toggleNavigation} |