homepage/client/src/pages/Media.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-03-21 22:42:23 +01:00
import React, { useEffect, useRef, useState } from 'react';
import '../css/Media.css';
const ClipEntry = ({ name, filename, uploader, thumbnail, duration, clickHandler }) => {
const _uploader = `${ uploader.tag } (${ uploader.id })`;
2022-03-20 16:36:58 +01:00
return (
2022-03-21 22:42:23 +01:00
<div className='clip-listing' onClick={clickHandler}>
<div className='flex-container'>
<div title={name} id='title' className='listing-element'>
<p><strong>Title</strong>: {name}</p>
</div>
<div title={_uploader} id='uploader' className='listing-element'>
<p> <strong>Uploader</strong>: {_uploader }</p>
</div>
<div title={filename} id='filename' className='listing-element'>
<p> <strong>Filename</strong>: {filename}</p>
</div>
<div title={duration} id='duration' className='listing-element'>
<p> <strong>Length</strong>: {duration}</p>
</div>
</div>
<img className='thumbnail' alt='Thumbnail' src={`/api/thumbnails/${thumbnail}`}/>
</div>
);
};
const VideoPlayer = ({ refF, video }) => {
const { filename, name } = video;
const source = `/api/clips/${filename}`;
return (
<div ref={refF} className='video-popup'>
<div>
<h1 className='no-margin no-padding'>
{name}
</h1>
</div>
<video className='video-player center' controls >
<source src={source} type='video/mp4' />
2022-03-20 16:36:58 +01:00
</video>
</div>
2022-03-21 22:42:23 +01:00
);
2022-03-20 16:36:58 +01:00
2022-03-21 22:42:23 +01:00
};
2022-03-20 16:36:58 +01:00
const Media = () => {
2022-03-21 22:42:23 +01:00
const [index, updateIndex] = useState([]);
const [video, setVideo] = useState();
const ref = useRef(null);
document.addEventListener('mousedown', (event) => {
if(ref.current && !ref.current.contains(event.target)) setVideo(null);
});
2022-03-20 16:36:58 +01:00
useEffect(() => {
(async () => {
2022-03-21 22:42:23 +01:00
const response = await fetch(`/api/clips`);
2022-03-20 16:36:58 +01:00
if (response.status !== 200) return console.error('Failed to get clip index');
2022-03-21 22:42:23 +01:00
updateIndex(await response.json());
})();
2022-03-20 16:36:58 +01:00
}, []);
2022-03-21 22:42:23 +01:00
let i = 0;
2022-03-20 16:36:58 +01:00
return (
2022-03-21 22:42:23 +01:00
<div className='media-page'>
{index.map(entry => <ClipEntry clickHandler={() => setVideo(entry)} key={i++} {...entry} />)}
{video ? <VideoPlayer refF={ref} video={video} />:''}
2022-03-20 16:36:58 +01:00
</div>
2022-03-21 22:42:23 +01:00
);
};
2022-03-20 16:36:58 +01:00
export default Media;