2024-09-19 21:28:00 +02:00
|
|
|
import { useEffect, useRef, useState, useMemo } from '@wordpress/element';
|
2024-09-05 21:17:36 +02:00
|
|
|
import Fastlane from '../../../../ppcp-axo/resources/js/Connection/Fastlane';
|
|
|
|
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
|
2024-09-19 21:28:00 +02:00
|
|
|
import { useDeleteEmptyKeys } from './useDeleteEmptyKeys';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-12 14:37:22 +02:00
|
|
|
const useFastlaneSdk = ( axoConfig, ppcpConfig ) => {
|
2024-09-05 21:17:36 +02:00
|
|
|
const [ fastlaneSdk, setFastlaneSdk ] = useState( null );
|
2024-09-11 22:58:13 +02:00
|
|
|
const initializingRef = useRef( false );
|
|
|
|
const configRef = useRef( { axoConfig, ppcpConfig } );
|
2024-09-19 21:28:00 +02:00
|
|
|
const deleteEmptyKeys = useDeleteEmptyKeys();
|
|
|
|
|
|
|
|
const styleOptions = useMemo( () => {
|
|
|
|
return deleteEmptyKeys( configRef.current.axoConfig.style_options );
|
|
|
|
}, [ deleteEmptyKeys ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const initFastlane = async () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
if ( initializingRef.current || fastlaneSdk ) {
|
2024-09-05 21:17:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
initializingRef.current = true;
|
|
|
|
log( 'Init Fastlane' );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
try {
|
|
|
|
const fastlane = new Fastlane();
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
if ( configRef.current.axoConfig.environment.is_sandbox ) {
|
|
|
|
window.localStorage.setItem( 'axoEnv', 'sandbox' );
|
|
|
|
}
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
await fastlane.connect( {
|
|
|
|
locale: configRef.current.ppcpConfig.locale,
|
2024-09-19 21:28:00 +02:00
|
|
|
styles: styleOptions,
|
2024-09-11 22:58:13 +02:00
|
|
|
} );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
fastlane.setLocale( 'en_us' );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
setFastlaneSdk( fastlane );
|
|
|
|
} catch ( error ) {
|
|
|
|
console.error( 'Failed to initialize Fastlane:', error );
|
|
|
|
} finally {
|
|
|
|
initializingRef.current = false;
|
|
|
|
}
|
2024-09-05 21:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
initFastlane();
|
2024-09-19 21:28:00 +02:00
|
|
|
}, [ fastlaneSdk, styleOptions ] );
|
2024-09-11 22:58:13 +02:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
configRef.current = { axoConfig, ppcpConfig };
|
|
|
|
}, [ axoConfig, ppcpConfig ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
return fastlaneSdk;
|
|
|
|
};
|
|
|
|
|
2024-09-12 14:37:22 +02:00
|
|
|
export default useFastlaneSdk;
|