owncast/web/components/video/VideoPoster.tsx

46 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-05-11 00:36:09 +02:00
import { useEffect, useState } from 'react';
import CrossfadeImage from '../ui/CrossfadeImage/CrossfadeImage';
import s from './VideoPoster.module.scss';
2022-04-26 22:39:35 +02:00
2022-05-11 00:36:09 +02:00
const REFRESH_INTERVAL = 20_000;
2022-04-26 22:39:35 +02:00
2022-05-11 00:36:09 +02:00
interface Props {
initialSrc: string;
src: string;
online: boolean;
}
2022-04-26 22:39:35 +02:00
2022-05-11 00:36:09 +02:00
export default function VideoPoster(props: Props) {
const { online, initialSrc, src: base } = props;
2022-04-26 22:39:35 +02:00
2022-05-11 00:36:09 +02:00
let timer: ReturnType<typeof setInterval>;
const [src, setSrc] = useState(initialSrc);
const [duration, setDuration] = useState('0s');
2022-04-26 22:39:35 +02:00
useEffect(() => {
2022-05-11 00:36:09 +02:00
clearInterval(timer);
timer = setInterval(() => {
if (duration === '0s') {
setDuration('3s');
}
setSrc(`${base}?${Date.now()}`);
}, REFRESH_INTERVAL);
}, []);
2022-04-26 22:39:35 +02:00
return (
2022-05-11 00:36:09 +02:00
<div className={s.poster}>
{!online && <img src={initialSrc} alt="logo" />}
2022-05-11 00:36:09 +02:00
{online && (
<CrossfadeImage
src={src}
duration={duration}
objectFit="cover"
2022-05-11 00:36:09 +02:00
width="100%"
height="100%"
2022-05-11 00:36:09 +02:00
/>
)}
2022-04-26 22:39:35 +02:00
</div>
);
}