Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/use connection to use my jetpack connection hook #38721

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { AdminPage, Container, Col } from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { ActivationScreen } from '@automattic/jetpack-licensing';
import React, { useCallback, useState, useMemo } from 'react';
/*
Expand All @@ -12,6 +11,7 @@ import { QUERY_LICENSES_KEY } from '../../data/constants';
import useJetpackApiQuery from '../../data/use-jetpack-api-query';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import useAnalytics from '../../hooks/use-analytics';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import GoBackLink from '../go-back-link';

/**
Expand All @@ -25,7 +25,7 @@ export default function AddLicenseScreen() {
name: QUERY_LICENSES_KEY,
queryFn: async api => ( await api.getUserLicenses() )?.items,
} );
const { userConnectionData } = useConnection();
const { userConnectionData } = useMyJetpackConnection();
const [ hasActivatedLicense, setHasActivatedLicense ] = useState( false );

// They might not have a display name set in wpcom, so fall back to wpcom login or local username.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { Text } from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { __ } from '@wordpress/i18n';
import { useCallback, useEffect } from 'react';
/**
Expand All @@ -14,6 +13,7 @@ import useActivate from '../../data/products/use-activate';
import useInstallStandalonePlugin from '../../data/products/use-install-standalone-plugin';
import useProduct from '../../data/products/use-product';
import useAnalytics from '../../hooks/use-analytics';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import useMyJetpackNavigate from '../../hooks/use-my-jetpack-navigate';
import ProductCard from '../product-card';
import type { AdditionalAction, SecondaryAction } from '../product-card/types';
Expand All @@ -22,10 +22,10 @@ import type { FC, ReactNode } from 'react';
interface ConnectedProductCardProps {
admin: boolean;
recommendation: boolean;
slug: string;
slug: JetpackModule;
children: ReactNode;
isDataLoading?: boolean;
Description?: React.JSX.Element | ( () => null );
Description?: FC;
additionalActions?: AdditionalAction[];
secondaryAction?: SecondaryAction;
upgradeInInterstitial?: boolean;
Expand All @@ -48,7 +48,7 @@ const ConnectedProductCard: FC< ConnectedProductCardProps > = ( {
onMouseEnter,
onMouseLeave,
} ) => {
const { isRegistered, isUserConnected } = useConnection();
const { isRegistered, isUserConnected } = useMyJetpackConnection();
const { recordEvent } = useAnalytics();

const { install: installStandalonePlugin, isPending: isInstalling } =
Expand All @@ -74,7 +74,7 @@ const ConnectedProductCard: FC< ConnectedProductCardProps > = ( {
return;
}

activate();
activate( {} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm creating a separate maintenance task to fix that 😓

Copy link
Contributor Author

@CodeyGuyDylan CodeyGuyDylan Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I looked at that on this PR but it seemed complex and didn't want to include too much 😅

}, [
activate,
isRegistered,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Button, getRedirectUrl, H3, Text } from '@automattic/jetpack-components';
import {
ManageConnectionDialog,
useConnection,
CONNECTION_STORE_ID,
} from '@automattic/jetpack-connection';
import { ManageConnectionDialog, CONNECTION_STORE_ID } from '@automattic/jetpack-connection';
import { useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { Icon, info, check, lockOutline } from '@wordpress/icons';
Expand All @@ -13,6 +9,7 @@ import { useAllProducts } from '../../data/products/use-product';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import getProductSlugsThatRequireUserConnection from '../../data/utils/get-product-slugs-that-require-user-connection';
import useAnalytics from '../../hooks/use-analytics';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import cloud from './cloud.svg';
import emptyAvatar from './empty-avatar.svg';
import jetpackGray from './jetpack-gray.svg';
Expand Down Expand Up @@ -173,13 +170,8 @@ const ConnectionStatusCard: ConnectionStatusCardType = ( {
context,
onConnectUser = null,
} ) => {
const { isRegistered, isUserConnected, userConnectionData } = useConnection( {
apiRoot,
apiNonce,
const { isRegistered, isUserConnected, userConnectionData } = useMyJetpackConnection( {
redirectUri,
skipUserConnection: false,
autoTrigger: false,
from: 'my-jetpack',
} );

const { recordEvent } = useAnalytics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import Card from '../card';
import ActionButton from './action-button';
import PriceComponent from './pricing-component';
import RecommendationActions from './recommendation-actions';
import SecondaryButton, { type SecondaryButtonProps } from './secondary-button';
import SecondaryButton from './secondary-button';
import Status from './status';
import styles from './style.module.scss';
import type { AdditionalAction, SecondaryAction } from './types';
import type { InstallCallback } from '../../data/products/use-install-standalone-plugin';
import type { FC, MouseEventHandler, ReactNode } from 'react';

export type ProductCardProps = {
Expand All @@ -25,13 +27,13 @@ export type ProductCardProps = {
isManageDisabled?: boolean;
onActivate?: () => void;
slug: JetpackModule;
additionalActions?: SecondaryButtonProps[];
additionalActions?: AdditionalAction[];
upgradeInInterstitial?: boolean;
primaryActionOverride?: Record< string, { href?: string; label?: string } >;
secondaryAction?: Record< string, SecondaryButtonProps & { positionFirst?: boolean } >;
onInstallStandalone?: () => void;
primaryActionOverride?: AdditionalAction;
secondaryAction?: SecondaryAction;
onInstallStandalone?: InstallCallback;
onActivateStandalone?: () => void;
status: keyof typeof PRODUCT_STATUSES;
status: ProductStatus;
onMouseEnter?: MouseEventHandler< HTMLButtonElement >;
onMouseLeave?: MouseEventHandler< HTMLButtonElement >;
};
Expand Down Expand Up @@ -135,7 +137,7 @@ const ProductCard: FC< ProductCardProps > = props => {
recordEvent( 'jetpack_myjetpack_product_card_install_standalone_plugin_click', {
product: slug,
} );
onInstallStandalone();
onInstallStandalone( {} );
}, [ slug, onInstallStandalone, recordEvent ] );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import styles from './style.module.scss';
import type { FC } from 'react';

interface StatusProps {
status: keyof typeof PRODUCT_STATUSES;
status: ProductStatus;
isFetching: boolean;
isInstallingStandalone: boolean;
isOwned: boolean;
}

type StatusStateFunction = ( status: keyof typeof PRODUCT_STATUSES, isOwned: boolean ) => string;
type StatusStateFunction = ( status: ProductStatus, isOwned: boolean ) => string;

const getStatusLabel: StatusStateFunction = ( status, isOwned ) => {
switch ( status ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Text,
TermsOfService,
} from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { createInterpolateElement } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import clsx from 'clsx';
Expand All @@ -23,6 +22,7 @@ import useProduct from '../../data/products/use-product';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import useAnalytics from '../../hooks/use-analytics';
import { useGoBack } from '../../hooks/use-go-back';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import useMyJetpackNavigate from '../../hooks/use-my-jetpack-navigate';
import GoBackLink from '../go-back-link';
import ProductDetailCard from '../product-detail-card';
Expand Down Expand Up @@ -80,9 +80,9 @@ export default function ProductInterstitial( {
const { recordEvent } = useAnalytics();
const { onClickGoBack } = useGoBack( { slug } );
const { myJetpackCheckoutUri = '' } = getMyJetpackWindowInitialState();
const { siteIsRegistering, handleRegisterSite } = useConnection( {
const { siteIsRegistering, handleRegisterSite } = useMyJetpackConnection( {
skipUserConnection: true,
redirectUri: detail.postActivationUrl ? detail.postActivationUrl : null,
redirectUri: detail.postActivationUrl ?? null,
} );
const showBundledTOS = ! hideTOS && !! bundle;
const productName = detail?.title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* External dependencies
*/
import { useConnection } from '@automattic/jetpack-connection';
import debugFactory from 'debug';
import { useCallback } from 'react';
/**
* Internal dependencies
*/
import ProductInterstitial from '../';
import useProduct from '../../../data/products/use-product';
import useMyJetpackConnection from '../../../hooks/use-my-jetpack-connection';
import jetpackAiImage from '../jetpack-ai.png';
import styles from './style.module.scss';

Expand All @@ -23,7 +23,7 @@ export default function JetpackAiInterstitial() {
const { detail } = useProduct( slug );
debug( detail );

const { userConnectionData } = useConnection();
const { userConnectionData } = useMyJetpackConnection();
const { currentUser } = userConnectionData;
const { wpcomUser } = currentUser;
const userId = currentUser?.id || 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getRedirectUrl,
Notice,
} from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { Button, Card, ExternalLink } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { Icon, plus, help, check } from '@wordpress/icons';
Expand All @@ -23,6 +22,7 @@ import { useCallback, useState, useEffect } from 'react';
import useProduct from '../../../data/products/use-product';
import useAnalytics from '../../../hooks/use-analytics';
import { useGoBack } from '../../../hooks/use-go-back';
import useMyJetpackConnection from '../../../hooks/use-my-jetpack-connection';
import useMyJetpackNavigate from '../../../hooks/use-my-jetpack-navigate';
import GoBackLink from '../../go-back-link';
import styles from './style.module.scss';
Expand All @@ -38,7 +38,7 @@ export default function () {
const { detail } = useProduct( 'jetpack-ai' );
const { description, aiAssistantFeature } = detail;
const [ showNotice, setShowNotice ] = useState( false );
const { isRegistered } = useConnection();
const { isRegistered } = useMyJetpackConnection();

const videoTitleContentGeneration = __(
'Generate and edit content faster with Jetpack AI Assistant',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useConnection } from '@automattic/jetpack-connection';
import { GoldenTokenModal } from '@automattic/jetpack-licensing';
import { __ } from '@wordpress/i18n';
import React from 'react';
import { QUERY_PURCHASES_KEY, REST_API_SITE_PURCHASES_ENDPOINT } from '../../data/constants';
import useSimpleQuery from '../../data/use-simple-query';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import { includesLifetimePurchase } from '../../utils/is-lifetime-purchase';

/**
Expand All @@ -12,7 +11,7 @@ import { includesLifetimePurchase } from '../../utils/is-lifetime-purchase';
* @returns {object} The RedeemTokenScreen component.
*/
export default function RedeemTokenScreen() {
const { userConnectionData } = useConnection();
const { userConnectionData } = useMyJetpackConnection();
// They might not have a display name set in wpcom, so fall back to wpcom login or local username.
const displayName =
userConnectionData?.currentUser?.wpcomUser?.display_name ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Container, Col, Button, Text } from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { __ } from '@wordpress/i18n';
import { close } from '@wordpress/icons';
import { useEffect, useCallback, useState } from 'react';
import { MyJetpackRoutes } from '../../constants';
import useWelcomeBanner from '../../data/welcome-banner/use-welcome-banner';
import useAnalytics from '../../hooks/use-analytics';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import useMyJetpackNavigate from '../../hooks/use-my-jetpack-navigate';
import { CardWrapper } from '../card';
import styles from './style.module.scss';
Expand All @@ -18,7 +18,7 @@ import styles from './style.module.scss';
const WelcomeBanner = () => {
const { recordEvent } = useAnalytics();
const { isWelcomeBannerVisible, dismissWelcomeBanner } = useWelcomeBanner();
const { isRegistered, isUserConnected } = useConnection();
const { isRegistered, isUserConnected } = useMyJetpackConnection();
const navigateToConnectionPage = useMyJetpackNavigate( MyJetpackRoutes.Connection );
const [ bannerVisible, setBannerVisible ] = useState( isWelcomeBannerVisible );
const shouldDisplayConnectionButton = ! isRegistered || ! isUserConnected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ import { REST_API_SITE_PRODUCTS_ENDPOINT } from '../constants';
import { QUERY_INSTALL_PRODUCT_KEY } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
import useProduct from './use-product';
import type { APIFetchOptionsWithQueryParams } from '../use-simple-mutation';
import type { UseMutateFunction } from '@tanstack/react-query';

const useInstallStandalonePlugin = ( productId: string ) => {
export type InstallCallback = UseMutateFunction<
void,
Error,
APIFetchOptionsWithQueryParams,
unknown
>;

type UseInstallStandalonePluginFunction = ( productId: string ) => {
install: InstallCallback;
isPending: boolean;
};

const useInstallStandalonePlugin: UseInstallStandalonePluginFunction = productId => {
const { detail, refetch } = useProduct( productId );

const { mutate: install, isPending } = useSimpleMutation( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useFetchingErrorNotice } from './notices/use-fetching-error-notice';
import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
import type { APIFetchOptions } from '@wordpress/api-fetch';

type APIFetchOptionsWithQueryParams = APIFetchOptions & {
export type APIFetchOptionsWithQueryParams = APIFetchOptions & {
queryParams?: Record< string, string | Array< string > | object >;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Update uses of useConnection to useMyJetpackConnection and improve typing in a few places
16 changes: 15 additions & 1 deletion projects/packages/my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ declare module '@wordpress/components';
declare module '@wordpress/compose';
declare module '@wordpress/icons';
declare module '@automattic/jetpack-connection';
declare module '@wordpress/url';

type ProductStatus =
| 'active'
| 'inactive'
| 'module_disabled'
| 'site_connection_error'
| 'plugin_absent'
| 'plugin_absent_with_plan'
| 'needs_plan'
| 'needs_activation'
| 'needs_first_site_connection'
| 'user_connection_error'
| 'can_upgrade';

type JetpackModule =
| 'anti-spam'
Expand Down Expand Up @@ -186,7 +200,7 @@ interface Window {
is_standalone_installed: boolean;
is_standalone_active: boolean;
};
status: string;
status: ProductStatus;
supported_products: string[];
tiers: string[];
title: string;
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-my-jetpack",
"version": "4.32.0",
"version": "4.32.1-alpha",
"description": "WP Admin page with information and configuration shared among all Jetpack stand-alone plugins",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/my-jetpack/#readme",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Initializer {
*
* @var string
*/
const PACKAGE_VERSION = '4.32.0';
const PACKAGE_VERSION = '4.32.1-alpha';

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down
Loading