Merge branch 'fix-infinityNaN-video' into 'main'

Fix infinity problem when video is streaming or does not have duration data

Closes #1795

See merge request soapbox-pub/soapbox!3323
This commit is contained in:
Alex Gleason 2025-01-31 18:59:31 +00:00
commit 101017feba
1 changed files with 18 additions and 9 deletions

View File

@ -191,10 +191,12 @@ const Video: React.FC<IVideo> = ({
};
const handleTimeUpdate = () => {
if (video.current) {
setCurrentTime(Math.floor(video.current.currentTime));
setDuration(Math.floor(video.current.duration));
}
if (!video.current) return;
const { duration, currentTime } = video.current;
setCurrentTime(Math.floor(currentTime));
setDuration(Number.isNaN(duration) || (duration === Infinity) ? 0 : Math.floor(duration));
};
const handleVolumeMouseDown: React.MouseEventHandler = e => {
@ -480,7 +482,11 @@ const Video: React.FC<IVideo> = ({
const playerStyle: React.CSSProperties = {};
const startTimeout = () => {
timeoutRef.current = setTimeout(() => setHovered(false), 1000);
if (timeoutRef.current) clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => {
setHovered(false);
timeoutRef.current = null;
}, 1000);
};
if (inline && containerWidth) {
@ -649,9 +655,13 @@ const Video: React.FC<IVideo> = ({
<span>
<span className='text-sm font-medium text-white/75'>{formatTime(currentTime)}</span>
{/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
<span className='mx-1.5 inline-block text-sm font-medium text-white/75'>/</span>
<span className='text-sm font-medium text-white/75'>{formatTime(duration)}</span>
{duration > 0 && (
<>
{/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
<span className='mx-1.5 inline-block text-sm font-medium text-white/75'>/</span>
<span className='text-sm font-medium text-white/75'>{formatTime(duration)}</span>
</>
)}
</span>
{link && (
@ -660,7 +670,6 @@ const Video: React.FC<IVideo> = ({
</span>
)}
</div>
<div className='flex min-w-[30px] flex-auto items-center truncate text-[16px]'>
<button
type='button'