From ff9f53ebc3756befed0ce6e4722b9b9b0a38255c Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 23 Mar 2022 17:08:32 +0200 Subject: [PATCH] refactoring some of the video player stuff --- client/src/pages/Media.js | 33 ++++++++++++++----------------- client/src/util/ClickOutside.js | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 client/src/util/ClickOutside.js diff --git a/client/src/pages/Media.js b/client/src/pages/Media.js index 17df219..ad732aa 100644 --- a/client/src/pages/Media.js +++ b/client/src/pages/Media.js @@ -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 { Helmet } from 'react-helmet'; import '../css/Media.css'; +import ClickDetector from '../util/ClickOutside'; const thumbnailBase = '/api/thumbnails/'; @@ -13,16 +14,16 @@ const ClipEntry = ({ name, filename, uploader, thumbnail, duration, clickHandler
-

Title: {name}

+

Title:
{name}

-

Uploader: {_uploader }

+

Uploader:
{_uploader }

-

Filename: {filename}

+

Filename:
{filename}

-

Length: {duration}

+

Length:
{duration}

Thumbnail @@ -66,18 +67,10 @@ const Media = () => { const [index, updateIndex] = useState([]); const [video, setVideo] = useState(); - const ref = useRef(null); const location = useLocation(); const navigate = useNavigate(); - document.addEventListener('mousedown', (event) => { - if (ref.current && !ref.current.contains(event.target)) { - setVideo(null); - navigate('/media'); - } - }); - useEffect(() => { (async () => { @@ -85,10 +78,7 @@ const Media = () => { if (response.status !== 200) return console.error('Failed to get clip index'); const data = await response.json(); 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('/'); if (videoname) { const video = data.find(vid => vid.name.toLowerCase() === videoname); @@ -104,6 +94,11 @@ const Media = () => { navigate(`/media/${video.name}`); }; + const cb = () => { + setVideo(null); + navigate('/media'); + }; + let i = 0; return (
@@ -117,7 +112,9 @@ const Media = () => { {index.map(entry => clickHandler(entry)} key={i++} {...entry} />)} - {video ? :''} + + {video ? : ''} +
); diff --git a/client/src/util/ClickOutside.js b/client/src/util/ClickOutside.js new file mode 100644 index 0000000..955869d --- /dev/null +++ b/client/src/util/ClickOutside.js @@ -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 ( +
+ {children} +
+ ); + +}; + +export default ClickDetector; \ No newline at end of file