Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | 2x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 78x 39x 39x 39x 39x 39x 39x 2x 2x 2x 39x 39x 39x 39x 39x 12x | import {
CONNECTION_FROM,
ConnectionErrorDetails,
getReconnectErrorMessage,
initConnectionStore,
ManageConnectionDialog,
useConnectionErrorNotice,
} from '@automattic/jetpack-connection';
import { getMyJetpackUrl } from '@automattic/jetpack-script-data';
import { Button } from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { Icon, error as errorIcon } from '@wordpress/icons';
import { Button as UIButton, Stack, Text } from '@wordpress/ui';
import clsx from 'clsx';
import { useCallback, useMemo, useState } from 'react';
import { useAllProducts } from '../../data/products/use-all-products';
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 useConnectionErrorTracking from '../../hooks/use-connection-error-tracking';
import { canManageConnection, useConnectionState } from '../../hooks/use-connection-state';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import { assignLocation } from '../../hooks/use-notification-watcher/assignLocation';
import { getConnectUrl } from '../../utils/connect-url';
import { leavePageFor } from '../../utils/reload-page';
import { ConnectionOwnerInfo } from './connection-owner-info';
import { ConnectionStatus } from './connection-status';
import styles from './styles.module.scss';
import type { ConnectionStatusCardType } from './types';
import type { MouseEvent } from 'react';
const ConnectionStatusCard: ConnectionStatusCardType = ( {
apiRoot,
apiNonce,
redirectUri = null,
title = __( 'Connection', 'jetpack-my-jetpack' ),
onDisconnected,
onUnlinked,
connectedPlugins,
connectedSiteId,
} ) => {
// An older shared jetpack-connection script may lack CONNECTION_FROM: its links then send `my-jetpack`.
const connectFrom = CONNECTION_FROM?.myJetpackConnectionCard ?? 'my-jetpack';
const { isRegistered, isUserConnected, userConnectionData } = useMyJetpackConnection( {
redirectUri,
} );
const { lifecycleStats } = getMyJetpackWindowInitialState();
const { recordEvent } = useAnalytics();
const [ isManageConnectionDialogOpen, setIsManageConnectionDialogOpen ] = useState( false );
const connectionStore = initConnectionStore();
const { setConnectionStatus } = useDispatch( connectionStore );
const { brokenModules } = lifecycleStats || {};
const { data: products, isLoading, isError } = useAllProducts();
const hasProductsThatRequireUserConnection = useMemo( () => {
Iif ( isLoading || isError ) {
return false;
}
return getProductSlugsThatRequireUserConnection( products ).length > 0;
}, [ isLoading, isError, products ] );
const hasUserConnectionBrokenModules = brokenModules?.needs_user_connection.length > 0;
const tracksEventData = useMemo( () => {
return {
user_connection_broken_modules: brokenModules?.needs_user_connection.join( ', ' ),
site_connection_broken_modules: brokenModules?.needs_site_connection.join( ', ' ),
};
}, [ brokenModules ] );
/**
* Open the Manage Connection Dialog, and register the connection type and the
* control that opened it as part of the Tracks event recorded.
*/
const openManageConnectionDialog = useCallback(
( connectionType: string, trigger: 'heading' | 'action' ) => ( e: MouseEvent ) => {
e && e.preventDefault();
recordEvent( 'jetpack_myjetpack_connection_manage_dialog_click', {
...tracksEventData,
connection_type: connectionType,
trigger,
} );
setIsManageConnectionDialogOpen( true );
},
[ recordEvent, setIsManageConnectionDialogOpen, tracksEventData ]
);
/**
* Open the Manage Site Connection Dialog from the status heading.
*/
const openManageSiteConnectionFromHeading = openManageConnectionDialog( 'site', 'heading' );
/**
* Open the Manage Site Connection Dialog from the named action beneath a fault.
*/
const openManageSiteConnectionFromAction = openManageConnectionDialog( 'site', 'action' );
/**
* Close the Manage Connection Dialog.
*/
const closeManageConnectionDialog = useCallback(
( e?: MouseEvent< HTMLButtonElement > ) => {
e && e.preventDefault();
setIsManageConnectionDialogOpen( false );
},
[ setIsManageConnectionDialogOpen ]
);
const onDisconnectedCallback = useCallback(
( e?: MouseEvent< HTMLButtonElement > ) => {
e && e.preventDefault();
setConnectionStatus( { isActive: false, isRegistered: false, isUserConnected: false } );
onDisconnected?.();
},
[ onDisconnected, setConnectionStatus ]
);
const onUnlinkedCallback = useCallback(
( e?: MouseEvent< HTMLButtonElement > ) => {
e && e.preventDefault();
setConnectionStatus( { isUserConnected: false } );
onUnlinked?.();
},
[ onUnlinked, setConnectionStatus ]
);
const handleConnectUser = useCallback(
( e: MouseEvent< HTMLButtonElement > ) => {
e && e.preventDefault();
recordEvent( 'jetpack_myjetpack_connection_connect_user_click', {
...tracksEventData,
from: connectFrom,
} );
leavePageFor( getConnectUrl( connectFrom ) );
},
[ connectFrom, recordEvent, tracksEventData ]
);
// The same package data the My Jetpack connection error notice runs on, so the
// card describes a live error in the notice's words and offers its CTAs, rather
// than a second account of the same fault.
const connectionErrorTrackingCallback = useConnectionErrorTracking();
const {
hasConnectionError,
connectionErrorMessage,
errorGroups,
showSupportLink,
actions,
restoreConnectionError,
trackNoticeLinkClick,
trackSupportLinkClick,
} = useConnectionErrorNotice( {
trackingCallback: connectionErrorTrackingCallback,
trackingContext: 'my-jetpack',
trackingPlacement: 'connection_card',
navigate: assignLocation,
reconnectFrom: CONNECTION_FROM?.myJetpackConnectionCardReconnect,
reconnectRedirectUrl: getMyJetpackUrl(),
} );
// Show the connection itself during Safe Mode and offline mode.
const state = useConnectionState( { skipSafeMode: true, skipOfflineMode: true } );
const allowDisconnect = canManageConnection(
isUserConnected,
!! userConnectionData.currentUser?.isMaster
);
return (
<section className={ styles[ 'connection-status-card' ] }>
<h3>{ title }</h3>
<ConnectionStatus
status={ state.status }
showAvatar={ hasProductsThatRequireUserConnection || hasUserConnectionBrokenModules }
/>
<Stack
render={ <section /> }
direction="column"
gap={ state.isDiagnosis ? 'sm' : undefined }
className={ styles[ 'connection-state' ] }
>
<h4>
{ ( hasConnectionError || ! isRegistered ) && (
<Icon
className={ clsx( styles[ 'state-icon' ], styles[ state.status ] ) }
icon={ errorIcon }
size={ 24 }
/>
) }
{ /* While the label diagnoses a fault it is text only: a chevron there would
promise a fix and open the disconnect dialog. Manage connection moves
down into the named actions instead. */ }
{ state.isDiagnosis ? (
state.label
) : (
<Button
variant="tertiary"
onClick={ openManageSiteConnectionFromHeading }
disabled={ ! allowDisconnect }
>
{ state.label }
{ allowDisconnect && (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
role="presentation"
>
<path
d="M10.6004 6L9.40039 7L14.0004 12L9.40039 17L10.6004 18L16.0004 12L10.6004 6Z"
fill="currentColor"
/>
</svg>
) }
</Button>
) }
</h4>
{ state.isDiagnosis ? (
<Stack direction="column" gap="md">
<Stack direction="column" gap="xs" className={ styles.details }>
{ restoreConnectionError && (
<Text variant="body-sm">
{ getReconnectErrorMessage( restoreConnectionError ) }
</Text>
) }
{ /* The card's body copy is a step below a notice's, so the shared
description is asked for at the card's size. An older shared
jetpack-connection script may predate the component. */ }
{ typeof ConnectionErrorDetails === 'function' ? (
<ConnectionErrorDetails
variant="body-sm"
errorGroups={ errorGroups }
showSupportLink={ showSupportLink }
onNoticeLinkClick={ trackNoticeLinkClick }
onSupportLinkClick={ trackSupportLinkClick }
/>
) : (
<Text variant="body-sm">{ connectionErrorMessage }</Text>
) }
</Stack>
<Stack direction="column" align="start" gap="sm">
{ actions.map( ( action, index ) => (
<UIButton
key={ action.label }
variant={ index === 0 ? 'solid' : 'outline' }
onClick={ action.onClick }
loading={ action.isLoading }
loadingAnnouncement={
action.loadingText || __( 'Loading…', 'jetpack-my-jetpack' )
}
>
{ action.label }
</UIButton>
) ) }
{ allowDisconnect ? (
<Button variant="link" onClick={ openManageSiteConnectionFromAction }>
{ __( 'Manage connection', 'jetpack-my-jetpack' ) }
</Button>
) : null }
</Stack>
</Stack>
) : (
<div>
<Text variant="body-md" className={ styles.description }>
{ state.description }
</Text>
<Stack direction="row" wrap="wrap" align="center" gap="lg">
{ state.action === 'CONNECT_USER' ? (
<Button variant="link" onClick={ handleConnectUser }>
{ __( 'Connect my account', 'jetpack-my-jetpack' ) }
</Button>
) : null }
</Stack>
</div>
) }
</Stack>
<ConnectionOwnerInfo />
<ManageConnectionDialog
apiRoot={ apiRoot }
apiNonce={ apiNonce }
onDisconnected={ onDisconnectedCallback }
onUnlinked={ onUnlinkedCallback }
connectedPlugins={ connectedPlugins }
connectedSiteId={ connectedSiteId }
connectedUser={ userConnectionData }
isOpen={ isManageConnectionDialogOpen }
onClose={ closeManageConnectionDialog }
context="my-jetpack"
placement="connection_card"
/>
</section>
);
};
export default ConnectionStatusCard;
|