mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 07:59:45 +00:00
fix(webui): reload video player and reset state when switching videos in viewer
This commit is contained in:
@@ -35,6 +35,7 @@ export function useFramePlayer(
|
||||
|
||||
// Track playing state of the HTML video element
|
||||
useEffect(() => {
|
||||
setCurrentFrame(0)
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import VideoViewer from './video-viewer'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
|
||||
const videojsMock = vi.fn((...args: unknown[]) => {
|
||||
void args
|
||||
return {
|
||||
on: vi.fn(),
|
||||
volume: vi.fn(),
|
||||
duration: vi.fn(() => 10),
|
||||
currentTime: vi.fn(() => 0),
|
||||
bufferedEnd: vi.fn(() => 0),
|
||||
playbackRate: vi.fn(() => 1),
|
||||
ready: vi.fn(),
|
||||
dispose: vi.fn(),
|
||||
isDisposed: vi.fn(() => false),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('video.js', () => ({
|
||||
default: (...args: unknown[]) => videojsMock(...args),
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/components/drawing-canvas', () => ({
|
||||
default: () => <div data-testid="drawing-canvas" />,
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/paraglide/messages.js', () => ({
|
||||
m: new Proxy({}, { get: () => () => '' }),
|
||||
}))
|
||||
|
||||
describe('VideoViewer', () => {
|
||||
beforeEach(() => {
|
||||
videojsMock.mockClear()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
it('reloads video player with new video source when file prop changes without unmounting', () => {
|
||||
const videoA: AssetInfo = {
|
||||
id: 'video-a',
|
||||
name: 'video-a.mp4',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
metadata: {
|
||||
originalWidth: 1280,
|
||||
originalHeight: 720,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
},
|
||||
videoTranscodes: [
|
||||
{
|
||||
resolution: '720p',
|
||||
url: 'https://cdn.example.com/videoA-720p.mp4',
|
||||
width: 1280,
|
||||
height: 720,
|
||||
},
|
||||
],
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const videoB: AssetInfo = {
|
||||
id: 'video-b',
|
||||
name: 'video-b.mp4',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 25,
|
||||
frameRate: 30,
|
||||
totalFrames: 750,
|
||||
},
|
||||
videoTranscodes: [
|
||||
{
|
||||
resolution: '1080p',
|
||||
url: 'https://cdn.example.com/videoB-1080p.mp4',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
},
|
||||
],
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const { rerender } = render(<VideoViewer file={videoA} />)
|
||||
|
||||
// Initial render should initialize videojs with video A URL
|
||||
expect(videojsMock).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
sources: [
|
||||
{
|
||||
src: 'https://cdn.example.com/videoA-720p.mp4',
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
// Rerender with video B (simulating selecting another video from the carousel)
|
||||
rerender(<VideoViewer file={videoB} />)
|
||||
|
||||
// VideoJS should be re-initialized with video B's URL
|
||||
expect(videojsMock).toHaveBeenLastCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
sources: [
|
||||
{
|
||||
src: 'https://cdn.example.com/videoB-1080p.mp4',
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -128,7 +128,20 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||
|
||||
useEffect(() => {
|
||||
const res = getInitialResolution()
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
isPlaying: false,
|
||||
progress: 0,
|
||||
currentTime: 0,
|
||||
duration: data.media?.metadata?.duration || 0,
|
||||
currentResolution: res?.resolution ?? '',
|
||||
currentSrc: res?.url ?? '',
|
||||
}))
|
||||
setHasManuallyZoomed(false)
|
||||
setIsPlayerReady(false)
|
||||
setBuffered(0)
|
||||
lastProcessedStartTimeRef.current = null
|
||||
}, [data.id])
|
||||
|
||||
const vidW = data.media?.metadata?.originalWidth || 1920
|
||||
@@ -252,6 +265,17 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
useEffect(() => {
|
||||
if (!videoContainerRef.current) return
|
||||
|
||||
const initialRes = getInitialResolution()
|
||||
const targetSrc = initialRes?.url ?? ''
|
||||
if (!targetSrc) return
|
||||
|
||||
// Clean up previous player if exists
|
||||
if (playerRef.current && !playerRef.current.isDisposed()) {
|
||||
playerRef.current.dispose()
|
||||
playerRef.current = null
|
||||
videoRef.current = null
|
||||
}
|
||||
|
||||
// 1. Create the video element manually
|
||||
// This avoids conflicts with React Strict Mode where the DOM might not match React's virtual DOM expectations after Video.js modifies it.
|
||||
const videoElement = document.createElement('video-js')
|
||||
@@ -272,7 +296,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
playsinline: true,
|
||||
sources: [
|
||||
{
|
||||
src: state.currentSrc,
|
||||
src: targetSrc,
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
@@ -352,7 +376,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
videoRef.current = null
|
||||
}
|
||||
}
|
||||
}, [data])
|
||||
}, [data.id])
|
||||
|
||||
// Handle changes to startTime (e.g., clicking different chunks in search results)
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user