2022-09-07 10:00:28 +03:00
|
|
|
import { FC, useEffect, useState } from 'react';
|
|
|
|
import { CrossfadeImage } from '../../ui/CrossfadeImage/CrossfadeImage';
|
|
|
|
import styles from './VideoPoster.module.scss';
|
2022-04-26 23:39:35 +03:00
|
|
|
|
2022-05-11 01:36:09 +03:00
|
|
|
const REFRESH_INTERVAL = 20_000;
|
2022-04-26 23:39:35 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type VideoPosterProps = {
|
2022-05-11 01:36:09 +03:00
|
|
|
initialSrc: string;
|
|
|
|
src: string;
|
|
|
|
online: boolean;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-04-26 23:39:35 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const VideoPoster: FC<VideoPosterProps> = ({ online, initialSrc, src: base }) => {
|
2022-05-11 01:36:09 +03:00
|
|
|
let timer: ReturnType<typeof setInterval>;
|
|
|
|
const [src, setSrc] = useState(initialSrc);
|
|
|
|
const [duration, setDuration] = useState('0s');
|
2022-04-26 23:39:35 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-11 01:36:09 +03:00
|
|
|
clearInterval(timer);
|
|
|
|
timer = setInterval(() => {
|
|
|
|
if (duration === '0s') {
|
|
|
|
setDuration('3s');
|
|
|
|
}
|
|
|
|
setSrc(`${base}?${Date.now()}`);
|
|
|
|
}, REFRESH_INTERVAL);
|
|
|
|
}, []);
|
2022-04-26 23:39:35 +03:00
|
|
|
|
|
|
|
return (
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.poster}>
|
2022-06-30 00:01:41 +03:00
|
|
|
{!online && <img src={initialSrc} alt="logo" />}
|
2022-05-11 01:36:09 +03:00
|
|
|
|
|
|
|
{online && (
|
|
|
|
<CrossfadeImage
|
|
|
|
src={src}
|
|
|
|
duration={duration}
|
2022-07-08 10:10:18 +03:00
|
|
|
objectFit="cover"
|
2022-05-11 01:36:09 +03:00
|
|
|
width="100%"
|
2022-06-30 00:01:41 +03:00
|
|
|
height="100%"
|
2022-05-11 01:36:09 +03:00
|
|
|
/>
|
|
|
|
)}
|
2022-04-26 23:39:35 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|