'use client'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import './index.less'; import { CaretRightOutlined, CompressOutlined, DownloadOutlined, ExpandOutlined, ExportOutlined, FontSizeOutlined, PauseOutlined, ThunderboltOutlined, } from '@ant-design/icons'; import { App as AntdApp, Button, Dropdown, Progress, Switch, Tooltip, } from 'antd'; import GlobalPerspectiveIcon from '../../icons/global-perspective.svg'; import PlayerSettingIcon from '../../icons/player-setting.svg'; import { type PlaybackSpeedType, useGlobalPreference } from '../../store/store'; import type { ReportDownloadHandler } from '../../types'; import { notifyError } from '../../utils'; import type { AnimationScript } from '../../utils/replay-scripts'; import { shouldRestartPlaybackFromBeginning } from './playback-controls'; import { triggerReportDownload } from './report-download'; import { StepsTimeline } from './scenes/StepScene'; import { exportBrandedVideo } from './scenes/export-branded-video'; import { calculateFrameMap } from './scenes/frame-calculator'; import type { FrameMap, ScriptFrame } from './scenes/frame-calculator'; import { getPlaybackFrameState } from './scenes/playback-frame'; import { useFramePlayer } from './use-frame-player'; export type PlayerPresentation = 'default' | 'timeline'; function deriveTaskId( scriptFrames: ScriptFrame[], stepsFrame: number, ): string | null { let taskId: string | null = null; for (const sf of scriptFrames) { if (sf.durationInFrames === 0) { if (sf.startFrame <= stepsFrame) { taskId = sf.taskId ?? taskId; } continue; } if (stepsFrame < sf.startFrame) break; taskId = sf.taskId ?? taskId; } return taskId; } function formatTime(frame: number, fps: number): string { const totalSeconds = Math.floor(frame / fps); const m = Math.floor(totalSeconds / 60); const s = totalSeconds % 60; return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`; } export function Player(props?: { replayScripts?: AnimationScript[]; imageWidth?: number; imageHeight?: number; reportFileContent?: string | null; reportUrl?: string | null; reportFormat?: 'single-html' | 'html-and-external-assets'; key?: string | number; fitMode?: 'width' | 'height'; autoZoom?: boolean; canDownloadReport?: boolean; onDownloadReport?: ReportDownloadHandler; onTaskChange?: (taskId: string | null) => void; /** Start playback automatically on mount. Defaults to true. */ autoPlay?: boolean; /** Hide the bottom playback control bar entirely. Defaults to false. */ hideControls?: boolean; /** Hide the fullscreen control for hosts that cannot support it. */ hideFullscreenControl?: boolean; presentation?: PlayerPresentation; }) { const { message } = AntdApp.useApp(); const { autoZoom, setAutoZoom, playbackSpeed, setPlaybackSpeed, subtitleEnabled, setSubtitleEnabled, } = useGlobalPreference(); useEffect(() => { if (props?.autoZoom !== undefined) { setAutoZoom(props.autoZoom); } }, [props?.autoZoom, setAutoZoom]); const scripts = props?.replayScripts; const frameMap = useMemo(() => { if (!scripts || scripts.length === 0) return null; return calculateFrameMap(scripts, { imageWidth: props?.imageWidth, imageHeight: props?.imageHeight, }); }, [props?.imageHeight, props?.imageWidth, scripts]); const containerRef = useRef(null); const renderLayerRef = useRef(null); const lastTaskIdRef = useRef(null); const [containerSize, setContainerSize] = useState({ width: 0, height: 0 }); // Observe render layer size to compute scale factor useEffect(() => { const el = renderLayerRef.current; if (!el) return; const ro = new ResizeObserver((entries) => { for (const entry of entries) { const { width, height } = entry.contentRect; setContainerSize((prev) => prev.width === width && prev.height === height ? prev : { width, height }, ); } }); ro.observe(el); return () => ro.disconnect(); }, []); const player = useFramePlayer({ durationInFrames: Math.max(frameMap?.totalDurationInFrames ?? 1, 1), fps: frameMap?.fps ?? 30, autoPlay: props?.autoPlay ?? true, loop: false, playbackRate: playbackSpeed, }); // The last frame that contains real content (skip the trailing End card). // Used as the denominator for the seek bar so the knob can reach 100% when // playback finishes — otherwise it parks at ~80–90% inside the End card. const effectiveEndFrame = useMemo(() => { if (!frameMap) return 0; for (let i = frameMap.scriptFrames.length - 1; i >= 0; i--) { const sf = frameMap.scriptFrames[i]; if (sf.taskId) return sf.startFrame + sf.durationInFrames - 1; } return Math.max(0, frameMap.totalDurationInFrames - 1); }, [frameMap]); const handlePlaybackToggle = useCallback(() => { if (player.playing) { player.pause(); return; } if ( shouldRestartPlaybackFromBeginning(player.currentFrame, effectiveEndFrame) ) { player.seekTo(0); } player.play(); }, [ effectiveEndFrame, player.currentFrame, player.pause, player.play, player.playing, player.seekTo, ]); // When playback stops, seek to the last frame with a taskId (skip the End card) useEffect(() => { if (!frameMap || player.playing) return; if (player.currentFrame < frameMap.totalDurationInFrames - 1) return; if (effectiveEndFrame > 0) player.seekTo(effectiveEndFrame); }, [ effectiveEndFrame, frameMap, player.currentFrame, player.playing, player.seekTo, ]); // Sync taskId to parent: report current task while playing, clear on stop useEffect(() => { if (!frameMap || !props?.onTaskChange) return; if (!player.playing) { if (lastTaskIdRef.current !== null) { lastTaskIdRef.current = null; props.onTaskChange(null); } return; } const taskId = deriveTaskId(frameMap.scriptFrames, player.currentFrame); if (taskId !== lastTaskIdRef.current) { lastTaskIdRef.current = taskId; props.onTaskChange(taskId); } }, [frameMap, props?.onTaskChange, player.currentFrame, player.playing]); const currentFrameState = useMemo(() => { if (!frameMap) return null; return getPlaybackFrameState(frameMap, player.currentFrame); }, [frameMap, player.currentFrame]); const handleDownloadReport = useCallback(async () => { if (props?.reportFormat === 'html-and-external-assets' && props.reportUrl) { globalThis.open(props.reportUrl, '_blank', 'noopener,noreferrer'); return; } try { let content = props?.reportFileContent; if (!content && props?.reportUrl) { const response = await fetch(props.reportUrl); if (!response.ok) { throw new Error(`Report request failed (${response.status})`); } content = await response.text(); } if (!content) return; await triggerReportDownload({ content, onDownloadReport: props?.onDownloadReport, }); } catch (error) { notifyError(error, { title: 'Failed to download report' }); } }, [ props?.onDownloadReport, props?.reportFileContent, props?.reportFormat, props?.reportUrl, ]); const subtitle = useMemo(() => { if (!currentFrameState) return null; if (!currentFrameState.title && !currentFrameState.subTitle) return null; return { title: currentFrameState.title, subTitle: currentFrameState.subTitle, }; }, [currentFrameState]); // Export video state (declared up here so the controls auto-hide logic can // see it and keep the control bar visible while an export is in progress — // otherwise the bar fades out, the settings dropdown trigger goes // pointer-events:none, and the dropdown closes mid-export). const [isExporting, setIsExporting] = useState(false); const [exportProgress, setExportProgress] = useState(0); const exportInFlightRef = useRef(false); // Controls auto-hide const [controlsVisible, setControlsVisible] = useState(true); const hideTimerRef = useRef | null>(null); const showControls = useCallback(() => { setControlsVisible(true); if (hideTimerRef.current) clearTimeout(hideTimerRef.current); if (isExporting) return; hideTimerRef.current = setTimeout(() => setControlsVisible(false), 3000); }, [isExporting]); const onMouseEnter = useCallback(() => { setControlsVisible(true); if (hideTimerRef.current) clearTimeout(hideTimerRef.current); }, []); const onMouseLeave = useCallback(() => { if (hideTimerRef.current) clearTimeout(hideTimerRef.current); if (isExporting) return; hideTimerRef.current = setTimeout(() => setControlsVisible(false), 1000); }, [isExporting]); useEffect(() => { if (!isExporting) return; setControlsVisible(true); if (hideTimerRef.current) { clearTimeout(hideTimerRef.current); hideTimerRef.current = null; } }, [isExporting]); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { const tag = (e.target as HTMLElement)?.tagName; if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return; if (e.code === 'Space') { e.preventDefault(); handlePlaybackToggle(); } }; document.addEventListener('keydown', handleKeyDown); return () => document.removeEventListener('keydown', handleKeyDown); }, [handlePlaybackToggle]); // Seek bar drag const seekBarRef = useRef(null); const handleSeekPointerDown = useCallback( (e: React.PointerEvent) => { if (!frameMap || !seekBarRef.current) return; const bar = seekBarRef.current; bar.setPointerCapture(e.pointerId); const seek = (clientX: number) => { const rect = bar.getBoundingClientRect(); const ratio = Math.max( 0, Math.min(1, (clientX - rect.left) / rect.width), ); player.seekTo(Math.round(ratio * effectiveEndFrame)); }; seek(e.clientX); const onMove = (ev: PointerEvent) => seek(ev.clientX); const onUp = () => { bar.removeEventListener('pointermove', onMove); bar.removeEventListener('pointerup', onUp); }; bar.addEventListener('pointermove', onMove); bar.addEventListener('pointerup', onUp); }, [frameMap, player, effectiveEndFrame], ); // Fullscreen const [isFullscreen, setIsFullscreen] = useState(false); const toggleFullscreen = useCallback(() => { const el = containerRef.current; if (!el) return; if (!document.fullscreenElement) { el.requestFullscreen().then(() => setIsFullscreen(true)); } else { document.exitFullscreen().then(() => setIsFullscreen(false)); } }, []); useEffect(() => { const handler = () => setIsFullscreen(!!document.fullscreenElement); document.addEventListener('fullscreenchange', handler); return () => document.removeEventListener('fullscreenchange', handler); }, []); // Export video const handleExportVideo = useCallback(async () => { if (!frameMap || exportInFlightRef.current) return; exportInFlightRef.current = true; setIsExporting(true); setExportProgress(0); try { await exportBrandedVideo( frameMap, { autoZoom, }, (pct) => setExportProgress(Math.round(pct * 100)), ); message.success('Video exported'); } catch (e) { console.error('Export failed:', e); notifyError(e, { title: 'Video export failed' }); } finally { exportInFlightRef.current = false; setIsExporting(false); setExportProgress(0); } }, [autoZoom, frameMap]); // Compute chapter markers const chapterMarkers = useMemo(() => { if (!frameMap || effectiveEndFrame <= 0) return []; const markers: { percent: number; title: string; frame: number }[] = []; for (const sf of frameMap.scriptFrames) { if ( (sf.type !== 'img' && sf.type !== 'insight') || sf.durationInFrames === 0 ) continue; const globalFrame = sf.startFrame; const percent = (globalFrame / effectiveEndFrame) * 100; if (percent > 1 && percent < 99) { const parts = [sf.title, sf.subTitle].filter(Boolean); markers.push({ percent, title: parts.length > 0 ? parts.join(': ') : `Chapter ${markers.length + 1}`, frame: globalFrame, }); } } return markers; }, [frameMap, effectiveEndFrame]); const reportFileContent = props?.reportFileContent ?? null; const hasReport = Boolean(reportFileContent || props?.reportUrl); const reportActionLabel = props?.reportFormat === 'html-and-external-assets' ? 'Open report' : 'Download report'; const canDownloadReport = props?.canDownloadReport !== false; const presentation = props?.presentation ?? 'default'; // If no scripts, fall back to a Download-report empty state when a report is // available (e.g. Stop was pressed before any task finished). Otherwise hide // the Player entirely instead of rendering an empty bordered box. if (!scripts || scripts.length === 0 || !frameMap) { if (hasReport && canDownloadReport) { return (
No replay available
); } return null; } const compositionWidth = currentFrameState?.imageWidth || frameMap.imageWidth; const compositionHeight = currentFrameState?.imageHeight || frameMap.imageHeight; const isPortraitCanvas = compositionHeight > compositionWidth; const seekPercent = effectiveEndFrame > 0 ? Math.min(100, (player.currentFrame / effectiveEndFrame) * 100) : 0; return (
{/* Render layer — renders at native resolution, scaled to fit & centered */}
{(() => { const scale = containerSize.width > 0 && containerSize.height > 0 ? Math.min( containerSize.width / compositionWidth, containerSize.height / compositionHeight, ) : 1; return (
); })()}
{/* Subtitle — rendered in display coordinates, outside scaled content */} {subtitleEnabled && subtitle && (
{subtitle.title && ( {subtitle.title} )} {subtitle.subTitle && ( {subtitle.subTitle} )}
)} {/* Control bar */} {!props?.hideControls && (
e.stopPropagation()} >
{player.playing ? : }
{formatTime( Math.min(player.currentFrame, effectiveEndFrame), frameMap.fps, )}{' '} / {formatTime(effectiveEndFrame + 1, frameMap.fps)}
{chapterMarkers.map((marker) => (
{ e.stopPropagation(); player.seekTo(marker.frame); }} /> ))}
{/* Custom controls */}
{hasReport && canDownloadReport ? (
{ void handleDownloadReport(); }} >
) : null} (
{/* Export video */}
{isExporting ? ( ) : ( )} {isExporting ? `Exporting ${exportProgress}%` : 'Export video'}
{/* Focus on cursor toggle */}
Focus on cursor
setAutoZoom(checked)} />
{/* Subtitle toggle */}
Subtitle
setSubtitleEnabled(checked)} />
{/* Playback speed */}
Playback speed
{([0.5, 1, 1.5, 2] as PlaybackSpeedType[]).map((speed) => (
setPlaybackSpeed(speed)} style={{ height: '32px', lineHeight: '32px', padding: '0 8px 0 28px', fontSize: '14px', cursor: 'pointer', borderRadius: '4px', }} className={`player-speed-option${playbackSpeed === speed ? ' active' : ''}`} > {speed}x
))}
)} menu={{ items: [] }} >
{!props?.hideFullscreenControl && (
{isFullscreen ? : }
)}
)}
); }