2023-04-28 07:30:52 +00:00
|
|
|
/**
|
2024-03-26 02:28:51 +00:00
|
|
|
* This script configures the XState V4 development tools in the
|
2023-04-28 07:30:52 +00:00
|
|
|
* development environment. It should not be included in production.
|
|
|
|
*
|
|
|
|
* To enable the XState inspector, open the browser console and type:
|
|
|
|
* localStorage.setItem("xstate_inspect", "true")
|
|
|
|
*/
|
|
|
|
|
2024-03-26 02:28:51 +00:00
|
|
|
async function enableXStateV4Inspect() {
|
2023-04-28 07:30:52 +00:00
|
|
|
const { inspect } = await import( '@xstate/inspect' );
|
|
|
|
const { Interpreter } = await import( 'xstate' );
|
|
|
|
// configure the XState inspector to open in a new tab
|
|
|
|
inspect( {
|
|
|
|
url: 'https://stately.ai/viz?inspect',
|
|
|
|
iframe: false,
|
|
|
|
} );
|
|
|
|
// configure all XServices to use the inspector
|
|
|
|
Interpreter.defaultOptions.devTools = true;
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(
|
2024-03-26 02:28:51 +00:00
|
|
|
'Devtools: XState V4 inspector enabled for WooCommerce Admin. To disable, type localStorage.setItem("xstate_inspect", "false")'
|
2023-04-28 07:30:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-26 02:28:51 +00:00
|
|
|
/**
|
|
|
|
* The below is specific to XState V5
|
|
|
|
*/
|
|
|
|
let XStateV5Inspect;
|
|
|
|
async function enableXStateV5Inspect() {
|
|
|
|
const { createBrowserInspector } = await import( '@statelyai/inspect' );
|
|
|
|
XStateV5Inspect = createBrowserInspector;
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(
|
|
|
|
'Devtools: XState V5 inspector enabled for WooCommerce Admin. To disable, type localStorage.setItem("xstateV5_inspect", "false")'
|
|
|
|
);
|
2023-04-28 07:30:52 +00:00
|
|
|
}
|
2024-03-26 02:28:51 +00:00
|
|
|
|
|
|
|
const isXStateInspectEnabled =
|
|
|
|
window.localStorage.getItem( 'xstate_inspect' ) === 'true';
|
|
|
|
const isXStateV5InspectEnabled =
|
|
|
|
window.localStorage.getItem( 'xstateV5_inspect' ) === 'true';
|
|
|
|
const isDevelopmentEnvironment = process.env.NODE_ENV === 'development';
|
|
|
|
|
|
|
|
let versionEnabled; // not enabled
|
|
|
|
if ( isDevelopmentEnvironment && isXStateInspectEnabled ) {
|
|
|
|
// XState V5 inspector malfunctions when V4 is also enabled
|
|
|
|
if ( isXStateV5InspectEnabled ) {
|
|
|
|
versionEnabled = 'V5';
|
|
|
|
enableXStateV5Inspect();
|
|
|
|
} else {
|
|
|
|
versionEnabled = 'V4';
|
|
|
|
enableXStateV4Inspect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useXStateInspect = ( machineVersion ) => {
|
|
|
|
let xstateV5Inspector;
|
|
|
|
if ( isXStateV5InspectEnabled && machineVersion === 'V5' ) {
|
|
|
|
xstateV5Inspector = XStateV5Inspect().inspect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
versionEnabled,
|
|
|
|
xstateV5Inspector,
|
|
|
|
};
|
|
|
|
};
|