HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/linde/wp-content/plugins/better-wp-security/core/packages/ui/src/rive/index.js
/* global itsecWebpackPublicPath */
/**
 * External dependencies
 */
import { useRive } from '@rive-app/react-canvas';

/**
 * WordPress dependencies
 */
import { useEffect, createContext, useState, useCallback, useContext } from '@wordpress/element';

/**
 * @typedef {import('@rive-app/react-canvas').UseRiveParameters} UseRiveParameters
 * @typedef {import('@rive-app/react-canvas').UseRiveOptions} UseRiveOptions
 * @typedef {import('@rive-app/react-canvas').RiveState} RiveState
 * @typedef {import('react')} React
 */

/**
 * @type {{preloaded: Record<string, ArrayBuffer|null|undefined>}}
 */
const defaultContext = {
	preloaded: {},
};

const Context = createContext( defaultContext );

/**
 * Provides Rive graphics to downstream components.
 *
 * @param {Object}          props
 * @param {string[]}        props.preload  Names of graphics to preload.
 * @param {React.ReactNode} props.children
 */
export function RiveGraphicProvider( { preload, children } ) {
	const [ context, setContext ] = useState( {
		preloaded: {},
	} );

	const updateState = useCallback( ( name, buffer ) => {
		setContext( ( current ) => ( {
			...current,
			preloaded: {
				...current.preloaded,
				[ name ]: buffer,
			},
		} ) );
	}, [ setContext ] );

	useEffect( () => {
		for ( const name of preload ) {
			if ( context.preloaded.hasOwnProperty( name ) ) {
				continue;
			}

			updateState( name, undefined );
			fetch( getSource( name ) )
				.then( ( response ) => response.arrayBuffer() )
				.then( ( buffer ) => updateState( name, buffer ) )
				.catch( ( error ) => {
					// eslint-disable-next-line no-console
					console.error( `[Solid Security] Could not load rive graphic '${ name }': ${ error }` );
					updateState( name, null );
				} );
		}
	}, [ preload, context.preloaded, updateState ] );

	return (
		<Context.Provider value={ context }>{ children }</Context.Provider>
	);
}

/**
 * Loads a Rive graphic.
 *
 * If the file has been preloaded by useRiveGraphicPreloader(), it will be available
 * immediately. Otherwise, Rive will fetch it.
 *
 * @param {string}            name      The graphic's name, without its extension.
 * @param {UseRiveParameters} [params]  Additional parameters.
 * @param {UseRiveOptions}    [options] Additional options.
 * @return {RiveState} The loaded Rive graphic.
 */
export function usePreloadedRiveGraphic( name, params, options ) {
	const { preloaded } = useContext( Context );

	if ( preloaded[ name ] ) {
		params.buffer = preloaded[ name ];
	} else {
		params.src = getSource( name );
	}

	return useRive( params, options );
}

function getSource( name ) {
	return `${ itsecWebpackPublicPath }../core/img/rive/${ name }.riv`;
}