refactoring some of the video player stuff

This commit is contained in:
Erik 2022-03-23 17:08:32 +02:00
parent 670239c373
commit ff9f53ebc3
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589
2 changed files with 50 additions and 18 deletions

View File

@ -1,7 +1,8 @@
import React, { useEffect, useRef, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom'; import { useLocation, useNavigate } from 'react-router-dom';
import { Helmet } from 'react-helmet'; import { Helmet } from 'react-helmet';
import '../css/Media.css'; import '../css/Media.css';
import ClickDetector from '../util/ClickOutside';
const thumbnailBase = '/api/thumbnails/'; const thumbnailBase = '/api/thumbnails/';
@ -13,16 +14,16 @@ const ClipEntry = ({ name, filename, uploader, thumbnail, duration, clickHandler
<div className='clip-listing shadow' onClick={clickHandler}> <div className='clip-listing shadow' onClick={clickHandler}>
<div className='flex-container'> <div className='flex-container'>
<div title={name} id='title' className='listing-element'> <div title={name} id='title' className='listing-element'>
<p><strong>Title</strong>: {name}</p> <p><strong>Title</strong>:<br/> {name}</p>
</div> </div>
<div title={_uploader} id='uploader' className='listing-element'> <div title={_uploader} id='uploader' className='listing-element'>
<p> <strong>Uploader</strong>: {_uploader }</p> <p> <strong>Uploader</strong>:<br /> {_uploader }</p>
</div> </div>
<div title={filename} id='filename' className='listing-element'> <div title={filename} id='filename' className='listing-element'>
<p> <strong>Filename</strong>: {filename}</p> <p> <strong>Filename</strong>:<br /> {filename}</p>
</div> </div>
<div title={duration} id='duration' className='listing-element'> <div title={duration} id='duration' className='listing-element'>
<p> <strong>Length</strong>: {duration}</p> <p> <strong>Length</strong>:<br /> {duration}</p>
</div> </div>
</div> </div>
<img className='thumbnail shadow' alt='Thumbnail' src={`${thumbnailBase}${thumbnail}`}/> <img className='thumbnail shadow' alt='Thumbnail' src={`${thumbnailBase}${thumbnail}`}/>
@ -66,18 +67,10 @@ const Media = () => {
const [index, updateIndex] = useState([]); const [index, updateIndex] = useState([]);
const [video, setVideo] = useState(); const [video, setVideo] = useState();
const ref = useRef(null);
const location = useLocation(); const location = useLocation();
const navigate = useNavigate(); const navigate = useNavigate();
document.addEventListener('mousedown', (event) => {
if (ref.current && !ref.current.contains(event.target)) {
setVideo(null);
navigate('/media');
}
});
useEffect(() => { useEffect(() => {
(async () => { (async () => {
@ -85,10 +78,7 @@ const Media = () => {
if (response.status !== 200) return console.error('Failed to get clip index'); if (response.status !== 200) return console.error('Failed to get clip index');
const data = await response.json(); const data = await response.json();
updateIndex(data); updateIndex(data);
// if (location.hash) {
// const video = data.find(vid => `#${vid.name.toLowerCase()}` === location.hash.toLowerCase());
// if (video) setVideo(video);
// }
const [, , videoname] = location.pathname.toLowerCase().split('/'); const [, , videoname] = location.pathname.toLowerCase().split('/');
if (videoname) { if (videoname) {
const video = data.find(vid => vid.name.toLowerCase() === videoname); const video = data.find(vid => vid.name.toLowerCase() === videoname);
@ -104,6 +94,11 @@ const Media = () => {
navigate(`/media/${video.name}`); navigate(`/media/${video.name}`);
}; };
const cb = () => {
setVideo(null);
navigate('/media');
};
let i = 0; let i = 0;
return ( return (
<div className='media-page'> <div className='media-page'>
@ -117,7 +112,9 @@ const Media = () => {
</Helmet> </Helmet>
{index.map(entry => <ClipEntry clickHandler={() => clickHandler(entry)} key={i++} {...entry} />)} {index.map(entry => <ClipEntry clickHandler={() => clickHandler(entry)} key={i++} {...entry} />)}
{video ? <VideoPlayer refF={ref} video={video} />:''}
{video ? <ClickDetector callback={cb} ><VideoPlayer video={video} /></ClickDetector> : ''}
</div> </div>
); );

View File

@ -0,0 +1,35 @@
import React, { useEffect, useRef } from "react";
const alerter = (ref, callback) => {
useEffect(() => {
const listener = (event) => {
if (ref.current && !ref.current.contains(event.target)) {
return callback();
}
};
document.addEventListener('mousedown', listener);
return () => {
document.removeEventListener('mousedown', listener);
};
}, [ref]);
};
// Component wrapper to enable listening for clicks outside of the given component
const ClickDetector = ({children, callback}) => {
const wrapperRef = useRef(null);
alerter(wrapperRef, callback);
return (
<div ref={wrapperRef}>
{children}
</div>
);
};
export default ClickDetector;