@automattic/jetpack-connection 0.30.3 → 0.30.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
@@ -2,6 +2,14 @@
2
2
 
3
3
  ### This is a list detailing changes for the Jetpack RNA Connection Component releases.
4
4
 
5
+ ## [0.30.5] - 2023-10-23
6
+ ### Added
7
+ - Added getCalypsoOrigin() helper function. [#33257]
8
+
9
+ ## [0.30.4] - 2023-10-19
10
+ ### Changed
11
+ - Updated package dependencies. [#33687]
12
+
5
13
  ## [0.30.3] - 2023-10-17
6
14
  ### Changed
7
15
  - Updated package dependencies. [#33646]
@@ -644,6 +652,8 @@
644
652
  - `Main` and `ConnectUser` components added.
645
653
  - `JetpackRestApiClient` API client added.
646
654
 
655
+ [0.30.5]: https://github.com/Automattic/jetpack-connection-js/compare/v0.30.4...v0.30.5
656
+ [0.30.4]: https://github.com/Automattic/jetpack-connection-js/compare/v0.30.3...v0.30.4
647
657
  [0.30.3]: https://github.com/Automattic/jetpack-connection-js/compare/v0.30.2...v0.30.3
648
658
  [0.30.2]: https://github.com/Automattic/jetpack-connection-js/compare/v0.30.1...v0.30.2
649
659
  [0.30.1]: https://github.com/Automattic/jetpack-connection-js/compare/v0.30.0...v0.30.1
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Get the Calypso origin based on the development environment.
3
+ *
4
+ * @returns {string} The Calypso url origin.
5
+ */
6
+ export default function getCalypsoOrigin() {
7
+ const calypsoEnv =
8
+ typeof window !== 'undefined' && window?.JP_CONNECTION_INITIAL_STATE?.calypsoEnv;
9
+
10
+ switch ( calypsoEnv ) {
11
+ case 'development':
12
+ return 'http://calypso.localhost:3000/';
13
+ case 'wpcalypso':
14
+ return 'https://wpcalypso.wordpress.com/';
15
+ case 'horizon':
16
+ return 'https://horizon.wordpress.com/';
17
+ default:
18
+ return 'https://wordpress.com/';
19
+ }
20
+ }
@@ -1,8 +1,8 @@
1
1
  import restApi from '@automattic/jetpack-api';
2
- import { getProductCheckoutUrl } from '@automattic/jetpack-components';
2
+ import { getCalypsoOrigin } from '@automattic/jetpack-connection';
3
3
  import { useDispatch } from '@wordpress/data';
4
4
  import debugFactory from 'debug';
5
- import { useEffect, useState } from 'react';
5
+ import { useEffect, useState, useMemo } from 'react';
6
6
  import useConnection from '../../components/use-connection';
7
7
  import { STORE_ID } from '../../state/store.jsx';
8
8
 
@@ -14,6 +14,8 @@ const {
14
14
  apiNonce,
15
15
  siteSuffix: defaultSiteSuffix,
16
16
  } = window?.JP_CONNECTION_INITIAL_STATE ? window.JP_CONNECTION_INITIAL_STATE : {};
17
+ const defaultAdminUrl =
18
+ typeof window !== 'undefined' ? window?.myJetpackInitialState?.adminUrl : null;
17
19
 
18
20
  /**
19
21
  * Custom hook that performs the needed steps
@@ -23,6 +25,8 @@ const {
23
25
  * @param {string} props.productSlug - The WordPress product slug.
24
26
  * @param {string} props.redirectUrl - The URI to redirect to after checkout.
25
27
  * @param {string} [props.siteSuffix] - The site suffix.
28
+ * @param {string} [props.adminUrl] - The site wp-admin url.
29
+ * @param {boolean} props.connectAfterCheckout - Whether or not to conect after checkout if not connected (default false - connect before).
26
30
  * @param {Function} props.siteProductAvailabilityHandler - The function used to check whether the site already has the requested product. This will be checked after registration and the checkout page will be skipped if the promise returned resloves true.
27
31
  * @param {Function} props.from - The plugin slug initiated the flow.
28
32
  * @returns {Function} - The useEffect hook.
@@ -31,6 +35,8 @@ export default function useProductCheckoutWorkflow( {
31
35
  productSlug,
32
36
  redirectUrl,
33
37
  siteSuffix = defaultSiteSuffix,
38
+ adminUrl = defaultAdminUrl,
39
+ connectAfterCheckout = false,
34
40
  siteProductAvailabilityHandler = null,
35
41
  from,
36
42
  } = {} ) {
@@ -46,15 +52,55 @@ export default function useProductCheckoutWorkflow( {
46
52
  from,
47
53
  } );
48
54
 
49
- // Build the checkout URL.
50
- const checkoutProductUrl = getProductCheckoutUrl(
51
- productSlug,
55
+ const checkoutUrl = useMemo( () => {
56
+ const origin = getCalypsoOrigin();
57
+ const shouldConnectAfterCheckout =
58
+ ( ! isRegistered || ! isUserConnected ) && connectAfterCheckout;
59
+
60
+ const checkoutPath = shouldConnectAfterCheckout
61
+ ? 'checkout/jetpack/'
62
+ : `checkout/${ siteSuffix }/`;
63
+
64
+ const productCheckoutUrl = new URL( `${ origin }${ checkoutPath }${ productSlug }` );
65
+
66
+ if ( shouldConnectAfterCheckout ) {
67
+ productCheckoutUrl.searchParams.set( 'connect_after_checkout', true );
68
+ productCheckoutUrl.searchParams.set( 'admin_url', adminUrl );
69
+ /**
70
+ * `from_site_slug` is the Jetpack site slug (siteSuffix) passed 'from the site' via url
71
+ * query arg (into Calypso), for use cases when there is not a site in context (such
72
+ * as when Jetpack is not connected or the user is not logged in) but we need to know
73
+ * the site we're working with). As opposed to Calypso's use of `siteSlug`
74
+ * which is the site slug present when the site is in context (ie- the site is available
75
+ * in State, such as when the site is connected and the user is logged in).
76
+ */
77
+ productCheckoutUrl.searchParams.set( 'from_site_slug', siteSuffix );
78
+ } else {
79
+ productCheckoutUrl.searchParams.set( 'site', siteSuffix );
80
+ }
81
+
82
+ productCheckoutUrl.searchParams.set( 'source', from );
83
+ productCheckoutUrl.searchParams.set( 'redirect_to', redirectUrl );
84
+ if ( ! isUserConnected ) {
85
+ productCheckoutUrl.searchParams.set( 'unlinked', '1' );
86
+ }
87
+
88
+ return productCheckoutUrl;
89
+ }, [
90
+ connectAfterCheckout,
91
+ isRegistered,
52
92
  siteSuffix,
93
+ productSlug,
94
+ adminUrl,
95
+ from,
53
96
  redirectUrl,
54
- isUserConnected
55
- );
56
- debug( 'checkoutProductUrl is %s', checkoutProductUrl );
97
+ isUserConnected,
98
+ ] );
99
+
100
+ debug( 'isRegistered is %s', isRegistered );
57
101
  debug( 'isUserConnected is %s', isUserConnected );
102
+ debug( 'connectAfterCheckout is %s', connectAfterCheckout );
103
+ debug( 'checkoutUrl is %s', checkoutUrl );
58
104
 
59
105
  const handleAfterRegistration = () => {
60
106
  return Promise.resolve(
@@ -66,12 +112,18 @@ export default function useProductCheckoutWorkflow( {
66
112
  }
67
113
  debug(
68
114
  'handleAfterRegistration: Site does not have a product associated. Redirecting to checkout %s',
69
- checkoutProductUrl
115
+ checkoutUrl
70
116
  );
71
- window.location.href = checkoutProductUrl;
117
+ window.location.href = checkoutUrl;
72
118
  } );
73
119
  };
74
120
 
121
+ const connectAfterCheckoutFlow = () => {
122
+ debug( 'Redirecting to connectAfterCheckout flow: %s', checkoutUrl );
123
+
124
+ window.location.href = checkoutUrl;
125
+ };
126
+
75
127
  /**
76
128
  * Handler to run the checkout workflow.
77
129
  *
@@ -81,6 +133,11 @@ export default function useProductCheckoutWorkflow( {
81
133
  const run = event => {
82
134
  event && event.preventDefault();
83
135
  setCheckoutStarted( true );
136
+ // By default we will connect first prior to checkout unless `props.connectAfterCheckout`
137
+ // is set (true), in which we will connect after purchase is completed.
138
+ if ( connectAfterCheckout ) {
139
+ return connectAfterCheckoutFlow();
140
+ }
84
141
 
85
142
  if ( isRegistered ) {
86
143
  return handleAfterRegistration();
package/index.jsx CHANGED
@@ -34,6 +34,7 @@ export { default as ManageConnectionDialog } from './components/manage-connectio
34
34
  * Helpers.
35
35
  */
36
36
  export { default as thirdPartyCookiesFallbackHelper } from './helpers/third-party-cookies-fallback';
37
+ export { default as getCalypsoOrigin } from './helpers/get-calypso-origin';
37
38
 
38
39
  /**
39
40
  * Store
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/jetpack-connection",
3
- "version": "0.30.3",
3
+ "version": "0.30.5",
4
4
  "description": "Jetpack Connection Component",
5
5
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/connection/#readme",
6
6
  "bugs": {
@@ -15,22 +15,22 @@
15
15
  "license": "GPL-2.0-or-later",
16
16
  "dependencies": {
17
17
  "@automattic/jetpack-analytics": "^0.1.27",
18
- "@automattic/jetpack-api": "^0.16.3",
19
- "@automattic/jetpack-components": "^0.43.4",
18
+ "@automattic/jetpack-api": "^0.16.4",
19
+ "@automattic/jetpack-components": "^0.44.1",
20
20
  "@automattic/jetpack-config": "^0.1.21",
21
- "@wordpress/base-styles": "4.34.0",
22
- "@wordpress/browserslist-config": "5.26.0",
23
- "@wordpress/components": "25.9.0",
24
- "@wordpress/data": "9.13.0",
25
- "@wordpress/element": "5.20.0",
26
- "@wordpress/i18n": "4.43.0",
27
- "@wordpress/icons": "9.34.0",
21
+ "@wordpress/base-styles": "4.35.0",
22
+ "@wordpress/browserslist-config": "5.27.0",
23
+ "@wordpress/components": "25.10.0",
24
+ "@wordpress/data": "9.14.0",
25
+ "@wordpress/element": "5.21.0",
26
+ "@wordpress/i18n": "4.44.0",
27
+ "@wordpress/icons": "9.35.0",
28
28
  "classnames": "2.3.2",
29
29
  "debug": "4.3.4",
30
30
  "prop-types": "^15.7.2"
31
31
  },
32
32
  "devDependencies": {
33
- "@automattic/jetpack-base-styles": "^0.6.10",
33
+ "@automattic/jetpack-base-styles": "^0.6.11",
34
34
  "@babel/core": "7.23.2",
35
35
  "@babel/preset-react": "7.22.15",
36
36
  "@storybook/addon-actions": "7.4.6",