woocommerce/plugins/woocommerce-admin/client/core-profiler/pages/Plugins.tsx

271 lines
7.0 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { __, sprintf, _n } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import interpolateComponents from '@automattic/interpolate-components';
import { Link } from '@woocommerce/components';
import { Extension, ExtensionList } from '@woocommerce/data';
import { useState } from 'react';
/**
* Internal dependencies
*/
import { CoreProfilerStateMachineContext } from '../index';
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
import {
PluginsLearnMoreLinkClickedEvent,
PluginsInstallationRequestedEvent,
PluginsPageSkippedEvent,
} from '../events';
import { Heading } from '../components/heading/heading';
import { Navigation } from '../components/navigation/navigation';
import { PluginCard } from '../components/plugin-card/plugin-card';
import { getAdminSetting } from '~/utils/admin-settings';
const locale = ( getAdminSetting( 'locale' )?.siteLocale || 'en_US' ).replace(
'_',
'-'
);
const joinWithAnd = ( items: string[] ) => {
return new Intl.ListFormat( locale, {
style: 'long',
type: 'conjunction',
} ).formatToParts( items );
};
export const Plugins = ( {
context,
navigationProgress,
sendEvent,
}: {
context: Pick<
CoreProfilerStateMachineContext,
'pluginsAvailable' | 'pluginsInstallationErrors' | 'pluginsSelected'
>;
sendEvent: (
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
payload:
| PluginsInstallationRequestedEvent
| PluginsPageSkippedEvent
| PluginsLearnMoreLinkClickedEvent
) => void;
navigationProgress: number;
} ) => {
const [ selectedPlugins, setSelectedPlugins ] = useState<
ExtensionList[ 'plugins' ]
>(
context.pluginsAvailable.filter(
context.pluginsInstallationErrors.length
? ( plugin ) => context.pluginsSelected.includes( plugin.key )
: ( plugin ) => ! plugin.is_activated
)
);
const setSelectedPlugin = ( plugin: Extension ) => {
setSelectedPlugins(
selectedPlugins.some( ( item ) => item.key === plugin.key )
? selectedPlugins.filter( ( item ) => item.key !== plugin.key )
: [ ...selectedPlugins, plugin ]
);
};
const skipPluginsPage = () => {
return sendEvent( {
type: 'PLUGINS_PAGE_SKIPPED',
} );
};
const submitInstallationRequest = () => {
const selectedPluginSlugs = selectedPlugins.map( ( plugin ) =>
plugin.key.replace( ':alt', '' )
);
const pluginsShown: string[] = [];
const pluginsUnselected: string[] = [];
context.pluginsAvailable.forEach( ( plugin ) => {
const pluginSlug = plugin.key.replace( ':alt', '' );
pluginsShown.push( pluginSlug );
if (
! plugin.is_activated &&
! selectedPluginSlugs.includes( pluginSlug )
) {
pluginsUnselected.push( pluginSlug );
}
} );
return sendEvent( {
type: 'PLUGINS_INSTALLATION_REQUESTED',
payload: {
pluginsShown,
pluginsSelected: selectedPluginSlugs,
pluginsUnselected,
},
} );
};
const composeListFormatParts = ( part: {
type: string;
value: string;
} ) => {
if ( part.type === 'element' ) {
return '{{span}}' + part.value + '{{/span}}';
}
return part.value;
};
const errorMessage = context.pluginsInstallationErrors.length
? interpolateComponents( {
mixedString: sprintf(
// Translators: %s is a list of plugins that does not need to be translated
__(
'Oops! We encountered a problem while installing %s. {{link}}Please try again{{/link}}.',
'woocommerce'
),
joinWithAnd(
context.pluginsInstallationErrors.map(
( error ) => error.plugin
)
)
.map( composeListFormatParts )
.join( '' )
),
components: {
span: <span />,
link: (
<Button isLink onClick={ submitInstallationRequest } />
),
},
} )
: null;
const pluginsWithAgreement = selectedPlugins.filter( ( plugin ) =>
[
'jetpack',
'woocommerce-services:shipping',
'woocommerce-services:tax',
].includes( plugin.key )
);
return (
<div
className="woocommerce-profiler-plugins"
data-testid="core-profiler-plugins"
>
<Navigation
percentage={ navigationProgress }
onSkip={ skipPluginsPage }
/>
<div className="woocommerce-profiler-page__content woocommerce-profiler-plugins__content">
<Heading
className="woocommerce-profiler__stepper-heading"
title={ __(
'Get a boost with our free features',
'woocommerce'
) }
subTitle={ __(
'Enhance your store by installing these free business features. No commitment required you can remove them at any time.',
'woocommerce'
) }
/>
{ errorMessage && (
<p className="plugin-error">{ errorMessage }</p>
) }
<div className="woocommerce-profiler-plugins__list">
{ context.pluginsAvailable.map( ( plugin ) => {
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
const learnMoreLink = plugin.learn_more_link ? (
<Link
onClick={ ( e ) => {
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
sendEvent( {
type: 'PLUGINS_LEARN_MORE_LINK_CLICKED',
payload: {
plugin: plugin.key,
learnMoreLink:
plugin.learn_more_link ?? '',
},
} );
e.stopPropagation();
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
} }
href={ plugin.learn_more_link }
target="_blank"
type="external"
>
{ __( 'Learn More', 'woocommerce' ) }
</Link>
) : null;
return (
<PluginCard
key={ `checkbox-control-${ plugin.key }` }
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
installed={ plugin.is_activated }
onChange={ () => {
setSelectedPlugin( plugin );
} }
checked={
selectedPlugins.filter(
( item ) => item.key === plugin.key
).length > 0
}
icon={
plugin.image_url ? (
<img
src={ plugin.image_url }
alt={ plugin.key }
/>
) : null
}
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
title={ plugin.label }
description={ plugin.description }
Additional changes for the core profiler plugins page (#38616) * Add install-and-activate-plugins-async action to onboarding * Add label and learn_more_link types * Use label and learn_more_link * Fix type * Add changelog * Add changelog * Add install_priority -- this will be used in the core profiler * Sort selected plugins by install_priority for installation * Remove unused imports * dev: refactor installAndActivatePlugins to xstate * ts fixes * Sort plugins by install_priority * Make sure WooCommerce Shipping is always visible * Update free extension list content * Updated WC payment description * Updated logo images * Visual changes on the plugin page * Change CTA font size from 13px to 14px * Change spacing between the chebox and logo to 24px * Change heading font-weight to 500 * Fix css lint error * Fix gray-900 variable name * Hide learn more link on mobile view * Add back learn more link that was removed from rebase * Send pluginsAvailable to pluginInstallermachine * Use is_activated to determine plugin availability and install status * Update packages/js/data/src/onboarding/types.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/DefaultFreeExtensions.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add back recordTracksPluginsLearnMoreLinkClicked * Use install-and-activate-plugins-async when timer is up * Record plugin and link with learn more linked clicked event * Fix failign tests * Add comment for install_priority --------- Co-authored-by: rjchow <me@rjchow.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2023-06-13 22:03:03 +00:00
learnMoreLink={ learnMoreLink }
/>
);
} ) }
</div>
<div className="woocommerce-profiler-plugins-continue-button-container">
<Button
className="woocommerce-profiler-plugins-continue-button"
variant="primary"
onClick={
selectedPlugins.length
? submitInstallationRequest
: skipPluginsPage
}
>
{ __( 'Continue', 'woocommerce' ) }
</Button>
</div>
{ pluginsWithAgreement.length > 0 && (
<p className="woocommerce-profiler-plugins-jetpack-agreement">
{ interpolateComponents( {
mixedString: sprintf(
/* translators: %s: a list of plugins, e.g. Jetpack */
_n(
'By installing %s plugin for free you agree to our {{link}}Terms of Service{{/link}}.',
'By installing %s plugins for free you agree to our {{link}}Terms of Service{{/link}}.',
pluginsWithAgreement.length,
'woocommerce'
),
joinWithAnd(
pluginsWithAgreement.map(
( plugin ) => plugin.name
)
)
.map( composeListFormatParts )
.join( '' )
),
components: {
span: <span />,
link: (
<Link
href="https://wordpress.com/tos/"
target="_blank"
type="external"
/>
),
},
} ) }
</p>
) }
</div>
</div>
);
};