@automattic/jetpack-shared-extension-utils 0.17.3 → 0.17.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.17.5] - 2025-03-10
9
+ ### Added
10
+ - Social: Move useUpgradeFlow from Jetpack plugin to the shared package. [#41836]
11
+
12
+ ### Changed
13
+ - Update dependencies. [#42222]
14
+
15
+ ## [0.17.4] - 2025-03-03
16
+ ### Changed
17
+ - Update package dependencies. [#42163]
18
+
8
19
  ## [0.17.3] - 2025-02-24
9
20
  ### Changed
10
21
  - Update dependencies. [#40231]
@@ -540,6 +551,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
540
551
  ### Changed
541
552
  - Core: prepare utility for release
542
553
 
554
+ [0.17.5]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.4...0.17.5
555
+ [0.17.4]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.3...0.17.4
543
556
  [0.17.3]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.2...0.17.3
544
557
  [0.17.2]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.1...0.17.2
545
558
  [0.17.1]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.0...0.17.1
package/index.js CHANGED
@@ -25,3 +25,4 @@ export { default as getJetpackBlocksVariation } from './src/get-jetpack-blocks-v
25
25
  export * from './src/modules-state';
26
26
  export { default as isMyJetpackAvailable } from './src/is-my-jetpack-available';
27
27
  export * from './src/libs';
28
+ export { default as useUpgradeFlow } from './src/hooks/use-upgrade-flow';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/jetpack-shared-extension-utils",
3
- "version": "0.17.3",
3
+ "version": "0.17.5",
4
4
  "description": "Utility functions used by the block editor extensions",
5
5
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
6
6
  "bugs": {
@@ -20,18 +20,19 @@
20
20
  "dependencies": {
21
21
  "@automattic/color-studio": "4.0.0",
22
22
  "@automattic/jetpack-analytics": "^0.1.36",
23
- "@automattic/jetpack-base-styles": "^0.6.43",
24
- "@automattic/jetpack-components": "^0.67.1",
25
- "@automattic/jetpack-connection": "^0.37.0",
26
- "@wordpress/api-fetch": "7.17.0",
27
- "@wordpress/block-editor": "14.12.0",
28
- "@wordpress/components": "29.3.0",
29
- "@wordpress/compose": "7.17.0",
30
- "@wordpress/data": "10.17.0",
31
- "@wordpress/element": "6.17.0",
32
- "@wordpress/i18n": "5.17.0",
33
- "@wordpress/plugins": "7.17.0",
34
- "@wordpress/url": "4.17.0",
23
+ "@automattic/jetpack-base-styles": "^0.6.44",
24
+ "@automattic/jetpack-components": "^0.68.1",
25
+ "@automattic/jetpack-connection": "^0.39.0",
26
+ "@wordpress/api-fetch": "7.19.0",
27
+ "@wordpress/block-editor": "14.14.0",
28
+ "@wordpress/components": "29.5.0",
29
+ "@wordpress/compose": "7.19.0",
30
+ "@wordpress/data": "10.19.0",
31
+ "@wordpress/element": "6.19.0",
32
+ "@wordpress/hooks": "4.19.0",
33
+ "@wordpress/i18n": "5.19.0",
34
+ "@wordpress/plugins": "7.19.0",
35
+ "@wordpress/url": "4.19.0",
35
36
  "clsx": "2.1.1",
36
37
  "debug": "4.4.0",
37
38
  "lodash": "4.17.21"
@@ -45,7 +46,7 @@
45
46
  "@testing-library/dom": "10.4.0",
46
47
  "@testing-library/react": "16.2.0",
47
48
  "@testing-library/user-event": "14.6.1",
48
- "@wordpress/babel-plugin-import-jsx-pragma": "5.17.0",
49
+ "@wordpress/babel-plugin-import-jsx-pragma": "5.19.0",
49
50
  "babel-jest": "29.3.1",
50
51
  "jest": "29.7.0",
51
52
  "jest-environment-jsdom": "29.7.0",
@@ -24,7 +24,7 @@ function redirect( url, callback, shouldOpenNewWindow = false ) {
24
24
  * @param {Function} onRedirect - To handle the redirection.
25
25
  * @return {object} - Object containing properties to handle autosave and redirect.
26
26
  */
27
- export default function useAutosaveAndRedirect( redirectUrl, onRedirect = noop ) {
27
+ export default function useAutosaveAndRedirect( redirectUrl = null, onRedirect = noop ) {
28
28
  const [ isRedirecting, setIsRedirecting ] = useState( false );
29
29
 
30
30
  const { isAutosaveablePost, isDirtyPost, currentPost } = useSelect( select => {
@@ -95,7 +95,9 @@ export default function useAutosaveAndRedirect( redirectUrl, onRedirect = noop )
95
95
  setIsRedirecting( true );
96
96
 
97
97
  autosave( event ).then( () => {
98
- redirect( redirectUrl, onRedirect, isWidgetEditor );
98
+ if ( redirectUrl ) {
99
+ redirect( redirectUrl, onRedirect, isWidgetEditor );
100
+ }
99
101
  } );
100
102
  };
101
103
 
@@ -0,0 +1,84 @@
1
+ ## useUpgradeFlow hook
2
+
3
+ Use this hook when you need to implement a component that leads the user to the checkout page.
4
+
5
+ ### Usage
6
+
7
+ ```es6
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ import { useUpgradeFlow } from '@automattic/jetpack-shared-extension-utils';
12
+
13
+ const myUPgradeComponent = () => {
14
+ const [ checkoutUrl, goToCheckoutPage, isRedirecting ] = useUpgradeFlow( 'business-bundle' );
15
+ return (
16
+ <Button href={ checkoutUrl } onClick={ goToCheckoutPage } isBusy={ isRedirecting }>
17
+ CheckOut!
18
+ </Button>
19
+ );
20
+ };
21
+ ```
22
+
23
+ ### API
24
+
25
+ `const [ checkoutUrl, goToCheckoutPage, isRedirecting ] = useUpgradeFlow( planSlug, onRedirect );`
26
+
27
+ #### Arguments
28
+
29
+ The hook accepts two arguments.
30
+
31
+ - `planSlug` (`string`) - Slug of plan to purchase
32
+ - _(optional)_ `onRedirect` (`(string) => void`) - callback function that will
33
+ be run when the redirect process triggers. The new URL is passed.
34
+
35
+ ### Return Values
36
+
37
+ The hook returns an array with three items.
38
+
39
+ - `checkoutUrl` (`string`): The checkout URL. You can use this value to set the href of an anchor element.
40
+ - `goToCheckoutPage` (`(event) => void`): Callback to be used in an onClick event.
41
+
42
+ Redirects the user to the checkout URL, checking before whether the current
43
+ post/page/etc has changes to save. If so, it saves them before to redirect.
44
+
45
+ - `isRedirecting` (`bool`): If the component is in the process of redirecting the
46
+ user. It may be waiting for a save to complete before redirecting. Use
47
+ this to set a button as busy or in a loading state.
48
+
49
+ - `planData` (`object`) An object with full data about the plan.
50
+
51
+ This data is provided by the `wordpress-com/plan` store registered by the [upgrade-nudge](../upgrade-nudge/store.js) shared component, which is populated by the response of the `https://public-api.wordpress.com/rest/v1.5/plans` request.
52
+ The following is a quite accurate shape of the personal plan:
53
+
54
+ ```js
55
+ {
56
+ product_id: 2005,
57
+ product_name: "Jetpack Personal",
58
+ meta: null,
59
+ bd_slug: "jetpack-personal",
60
+ bd_variation_slug: "jetpack-personal-yearly",
61
+ sale_coupon_applied: false,
62
+ sale_coupon: null,
63
+ multi: 0,
64
+ cost: 39,
65
+ blog_id: null,
66
+ product_slug: "jetpack_personal",
67
+ description: "",
68
+ bill_period: 365,
69
+ product_type: "bundle",
70
+ available: "yes",
71
+ outer_slug: null,
72
+ extra: null,
73
+ capability: "manage_options",
74
+ product_name_short: "Personal",
75
+ icon: "https://s0.wordpress.com/i/store/plan-business.png",
76
+ icon_active: "https://s0.wordpress.com/i/store/plan-business-active.png",
77
+ bill_period_label: "per year",
78
+ price: "$39",
79
+ formatted_price: "$39",
80
+ raw_price: 39,
81
+ tagline: null,
82
+ currency_code: "USD"
83
+ }
84
+ ```
@@ -0,0 +1,57 @@
1
+ import { useSelect } from '@wordpress/data';
2
+ import { doAction, hasAction } from '@wordpress/hooks';
3
+ import { getUpgradeUrl } from '../../plan-utils';
4
+ import useAutosaveAndRedirect from '../use-autosave-and-redirect';
5
+
6
+ const noop = () => {};
7
+
8
+ const HOOK_OPEN_CHECKOUT_MODAL = 'a8c.wpcom-block-editor.openCheckoutModal';
9
+
10
+ /**
11
+ * Use this hook when you need to implement a component that leads the user to the checkout page.
12
+ * @param {string} planSlug - The plan slug to upgrade to.
13
+ * @param {Function} onRedirect - A callback function to be called when the user is redirected to the checkout page.
14
+ *
15
+ * @return {Array} - An array with the following elements:
16
+ */
17
+ export default function useUpgradeFlow( planSlug, onRedirect = noop ) {
18
+ const { checkoutUrl, planData } = useSelect(
19
+ select => {
20
+ const editorSelector = select( 'core/editor' );
21
+ const planSelector = select( 'wordpress-com/plans' );
22
+
23
+ const { id: postId, type: postType } = editorSelector.getCurrentPost();
24
+ const plan = planSelector && planSelector.getPlan( planSlug );
25
+
26
+ return {
27
+ checkoutUrl: getUpgradeUrl( { plan, planSlug, postId, postType } ),
28
+ planData: plan,
29
+ };
30
+ },
31
+ [ planSlug ]
32
+ );
33
+
34
+ const { autosave, autosaveAndRedirect, isRedirecting } = useAutosaveAndRedirect(
35
+ checkoutUrl,
36
+ onRedirect
37
+ );
38
+
39
+ const goToCheckoutPage = async event => {
40
+ event.preventDefault();
41
+
42
+ // If this action is available, the feature is enabled to open the checkout
43
+ // in a modal rather than redirect the user there, away from the editor.
44
+ if ( hasAction( HOOK_OPEN_CHECKOUT_MODAL ) ) {
45
+ event.preventDefault();
46
+
47
+ autosave( event );
48
+
49
+ doAction( HOOK_OPEN_CHECKOUT_MODAL, { products: [ planData ] } );
50
+ return;
51
+ }
52
+
53
+ autosaveAndRedirect( event );
54
+ };
55
+
56
+ return [ checkoutUrl, goToCheckoutPage, isRedirecting, planData ];
57
+ }