woocommerce/plugins/woocommerce-admin/client/utils/index.js

126 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { useEffect } from '@wordpress/element';
export * from './plugins';
export * from './slot-fill-ordering';
/**
* Get the URL params.
*
* @param {string} locationSearch - Querystring part of a URL, including the question mark (?).
* @return {Object} - URL params.
*/
export function getUrlParams( locationSearch ) {
if ( locationSearch ) {
return locationSearch
.substr( 1 )
.split( '&' )
.reduce( ( params, query ) => {
const chunks = query.split( '=' );
const key = chunks[ 0 ];
let value = decodeURIComponent( chunks[ 1 ] );
value = isNaN( Number( value ) ) ? value : Number( value );
return ( params[ key ] = value ), params;
}, {} );
}
return {};
}
/**
* Get the current screen name.
*
* @return {string} - Screen name.
*/
export function getScreenName() {
let screenName = '';
const {
page,
path,
post_type: postType,
} = getUrlParams( window.location.search );
if ( page ) {
const currentPage = page === 'wc-admin' ? 'home_screen' : page;
screenName = path
? path.replace( /\//g, '_' ).substring( 1 )
: currentPage;
} else if ( postType ) {
screenName = postType;
}
return screenName;
}
/**
* Similar to filter, but return two arrays separated by a partitioner function
*
* @param {Array} arr - Original array of values.
* @param {Function} partitioner - Function to return truthy/falsy values to separate items in array.
*
* @return {Array} - Array of two arrays, first including truthy values, and second including falsy.
*/
export const sift = ( arr, partitioner ) =>
arr.reduce(
( all, curr ) => {
all[ !! partitioner( curr ) ? 0 : 1 ].push( curr );
return all;
},
[ [], [] ]
);
const timeFrames = [
{ name: '0-2s', max: 2 },
{ name: '2-5s', max: 5 },
{ name: '5-10s', max: 10 },
{ name: '10-15s', max: 15 },
{ name: '15-20s', max: 20 },
{ name: '20-30s', max: 30 },
{ name: '30-60s', max: 60 },
{ name: '>60s' },
];
/**
* Returns time frame for a given time in milliseconds.
*
* @param {number} timeInMs - time in milliseconds
*
* @return {string} - Time frame.
*/
export const getTimeFrame = ( timeInMs ) => {
for ( const timeFrame of timeFrames ) {
if ( ! timeFrame.max ) {
return timeFrame.name;
}
if ( timeInMs < timeFrame.max * 1000 ) {
return timeFrame.name;
}
}
};
/**
* Goes into fullscreen mode when the component is loaded
*
* @param {string[]} classes - classes to add to document.body
*/
export const useFullScreen = ( classes ) => {
useEffect( () => {
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
const hasToolbarClass =
document.documentElement.classList.contains( 'wp-toolbar' );
document.body.classList.remove( 'woocommerce-admin-is-loading' );
document.body.classList.add( classes );
document.body.classList.add( 'woocommerce-admin-full-screen' );
document.body.classList.add( 'is-wp-toolbar-disabled' );
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
if ( hasToolbarClass ) {
document.documentElement.classList.remove( 'wp-toolbar' );
}
return () => {
document.body.classList.remove( classes );
document.body.classList.remove( 'woocommerce-admin-full-screen' );
document.body.classList.remove( 'is-wp-toolbar-disabled' );
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
if ( hasToolbarClass ) {
document.documentElement.classList.add( 'wp-toolbar' );
}
};
} );
};