import { Button, Notice, Spinner } from '@wordpress/components'; import { useRefEffect } from '@wordpress/compose'; import { useRef, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { normalizePlayerOptions, previewHeight } from './options.js'; import { playerAssets, playerForDocument } from './player-bridge.js'; import { loadRecording, validateInitialDimensions } from './recording-loader.js'; import { recordingErrorMessage } from './recording-errors.js'; export default function Preview( { source, attributes, attempt, onRetry, onRecordingSize } ) { const [ status, setStatus ] = useState( 'loading' ); const [ problem, setProblem ] = useState( '' ); const onRecordingSizeRef = useRef( onRecordingSize ); onRecordingSizeRef.current = onRecordingSize; const optionsJSON = JSON.stringify( normalizePlayerOptions( attributes, { preview: true } ) ); const playerRef = useRefEffect( ( container ) => { let disposed = false; let failed = false; let player; let readinessTimer; let deadline; const canvasWindow = container.ownerDocument.defaultView; const request = new canvasWindow.AbortController(); const reportSize = ( size ) => { if ( ! disposed ) onRecordingSizeRef.current?.( source, size ); }; setStatus( 'loading' ); setProblem( '' ); reportSize( null ); const fail = ( message ) => { if ( disposed || failed ) return; failed = true; request.abort(); canvasWindow.clearInterval( readinessTimer ); canvasWindow.clearTimeout( deadline ); reportSize( null ); setProblem( message ); setStatus( 'error' ); }; const ready = () => { if ( disposed || failed ) return; canvasWindow.clearInterval( readinessTimer ); canvasWindow.clearTimeout( deadline ); setStatus( 'ready' ); }; deadline = canvasWindow.setTimeout( () => fail( recordingErrorMessage( { code: 'timeout' } ) ), 30000 ); playerForDocument( container.ownerDocument, playerAssets() ).then( ( api ) => { if ( disposed || failed ) return; try { validateInitialDimensions( JSON.parse( optionsJSON ) ); } catch ( error ) { fail( recordingErrorMessage( error ) ); return; } player = api.create( { data: () => loadRecording( source, { fetchImpl: canvasWindow.fetch.bind( canvasWindow ), signal: request.signal, options: JSON.parse( optionsJSON ), onMetadata: ( size ) => { if ( ! failed ) reportSize( size ); }, } ).catch( ( error ) => { const message = recordingErrorMessage( error ); if ( message ) fail( message ); throw error; } ), }, container, JSON.parse( optionsJSON ) ); player.addEventListener( 'error', () => fail( __( 'Unable to load the recording. Check the URL, file format, and the source server’s CORS permissions.', 'wp-asciiplayer' ) ) ); player.addEventListener( 'playing', ready ); // getDuration is public API; a mounted wrapper alone does not mean the // recording loaded successfully. Works across supported player versions. const checkReady = () => { const duration = player.getDuration(); if ( typeof duration === 'number' && Number.isFinite( duration ) ) ready(); }; readinessTimer = canvasWindow.setInterval( checkReady, 150 ); checkReady(); } ).catch( () => fail( __( 'The local player could not start. Try again, or check that the plugin’s built assets are installed.', 'wp-asciiplayer' ) ) ); return () => { disposed = true; request.abort(); canvasWindow.clearInterval( readinessTimer ); canvasWindow.clearTimeout( deadline ); if ( player ) player.dispose(); container.replaceChildren(); }; }, [ source, optionsJSON, attempt ] ); return (
{ problem }