import config from '@automattic/calypso-config'; import { isEcommercePlan } from '@automattic/calypso-products'; import page from '@automattic/calypso-router'; import { Badge } from '@automattic/ui'; import { Button } from '@wordpress/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import PropTypes from 'prop-types'; import { parse } from 'qs'; import { Component } from 'react'; import { connect } from 'react-redux'; import ReaderIcon from 'calypso/assets/icons/reader/reader-icon'; import AsyncLoad from 'calypso/components/async-load'; import Gravatar from 'calypso/components/gravatar'; import wpcom from 'calypso/lib/wp'; import { domainManagementList } from 'calypso/my-sites/domains/paths'; import { preload } from 'calypso/sections-helper'; import { siteUsesWpAdminInterface } from 'calypso/sites-dashboard/utils'; import { requestAdminMenu } from 'calypso/state/admin-menu/actions'; import { getAdminMenu } from 'calypso/state/admin-menu/selectors'; import { recordTracksEvent } from 'calypso/state/analytics/actions'; import { redirectToLogout } from 'calypso/state/current-user/actions'; import { getCurrentUser, getCurrentUserSiteCount } from 'calypso/state/current-user/selectors'; import { savePreference } from 'calypso/state/preferences/actions'; import getCurrentRoute from 'calypso/state/selectors/get-current-route'; import getEditorUrl from 'calypso/state/selectors/get-editor-url'; import getPreviousRoute from 'calypso/state/selectors/get-previous-route'; import getPrimarySiteId from 'calypso/state/selectors/get-primary-site-id'; import getSiteMigrationStatus from 'calypso/state/selectors/get-site-migration-status'; import hasGravatarDomainQueryParam from 'calypso/state/selectors/has-gravatar-domain-query-param'; import isDomainOnlySite from 'calypso/state/selectors/is-domain-only-site'; import isNotificationsOpen from 'calypso/state/selectors/is-notifications-open'; import isAtomicSite from 'calypso/state/selectors/is-site-automated-transfer'; import isSiteMigrationActiveRoute from 'calypso/state/selectors/is-site-migration-active-route'; import isSiteMigrationInProgress from 'calypso/state/selectors/is-site-migration-in-progress'; import isSiteP2Hub from 'calypso/state/selectors/is-site-p2-hub'; import isSiteWPForTeams from 'calypso/state/selectors/is-site-wpforteams'; import getIsUnlaunchedSite from 'calypso/state/selectors/is-unlaunched-site'; import { updateSiteMigrationMeta } from 'calypso/state/sites/actions'; import { launchSiteOrRedirectToLaunchSignupFlow } from 'calypso/state/sites/launch/actions'; import { isTrialSite } from 'calypso/state/sites/plans/selectors'; import { getSiteSlug, isJetpackSite, getSitePlanSlug, getSitePlanName, getSiteTitle, getSiteUrl, getSiteAdminUrl, getSiteHomeUrl, getSite, } from 'calypso/state/sites/selectors'; import canCurrentUserManageSiteOptions from 'calypso/state/sites/selectors/can-current-user-manage-site-options'; import getSiteOption from 'calypso/state/sites/selectors/get-site-option'; import isSimpleSite from 'calypso/state/sites/selectors/is-simple-site'; import { isSupportSession } from 'calypso/state/support/selectors'; import { activateNextLayoutFocus, setNextLayoutFocus } from 'calypso/state/ui/layout-focus/actions'; import { getCurrentLayoutFocus } from 'calypso/state/ui/layout-focus/selectors'; import { getMostRecentlySelectedSiteId, getSectionGroup } from 'calypso/state/ui/selectors'; import Item from './item'; import Masterbar from './masterbar'; import Notifications from './masterbar-notifications/notifications-button'; class MasterbarLoggedIn extends Component { static propTypes = { user: PropTypes.object.isRequired, domainOnlySite: PropTypes.bool, section: PropTypes.oneOfType( [ PropTypes.string, PropTypes.bool ] ), setNextLayoutFocus: PropTypes.func.isRequired, currentLayoutFocus: PropTypes.string, siteSlug: PropTypes.string, isEcommerce: PropTypes.bool, isCheckout: PropTypes.bool, isCheckoutPending: PropTypes.bool, isCheckoutFailed: PropTypes.bool, loadHelpCenterIcon: PropTypes.bool, isGlobalSidebarVisible: PropTypes.bool, isGravatarDomain: PropTypes.bool, }; handleLayoutFocus = ( currentSection ) => { if ( currentSection !== this.props.section ) { // When current section is not focused then open the sidebar. this.props.setNextLayoutFocus( 'sidebar' ); } else { // When current section is focused then open or close the sidebar depending on current state. 'sidebar' === this.props.currentLayoutFocus ? this.props.setNextLayoutFocus( 'content' ) : this.props.setNextLayoutFocus( 'sidebar' ); } }; componentDidMount() { // Give a chance to direct URLs to open the sidebar on page load ( eg by clicking 'me' in wp-admin ). const qryString = parse( document.location.search.replace( /^\?/, '' ) ); if ( qryString?.openSidebar === 'true' ) { this.props.setNextLayoutFocus( 'sidebar' ); } } handleToggleMobileMenu = () => { this.props.recordTracksEvent( 'calypso_masterbar_menu_clicked' ); this.handleLayoutFocus( this.props.section ); this.props.activateNextLayoutFocus(); }; clickMySites = () => { this.props.recordTracksEvent( 'calypso_masterbar_my_sites_clicked' ); /** * Site Migration: Reset a failed migration when clicking on My Sites * * If the site migration has failed, clicking on My Sites sends the customer in a loop * until they click the Try Again button on the migration screen. * * This code makes it possible to reset the failed migration state when clicking My Sites too. */ const { migrationStatus, siteId } = this.props; if ( siteId && migrationStatus === 'error' ) { /** * Reset the in-memory site lock for the currently selected site */ this.props.updateSiteMigrationMeta( siteId, 'inactive', null, null ); /** * Reset the migration on the backend */ wpcom.req .post( { path: `/sites/${ siteId }/reset-migration`, apiNamespace: 'wpcom/v2', } ) .catch( () => {} ); } }; clickReader = () => { this.props.recordTracksEvent( 'calypso_masterbar_reader_clicked' ); }; clickMe = () => { this.props.recordTracksEvent( 'calypso_masterbar_me_clicked' ); window.scrollTo( 0, 0 ); }; preloadMySites = () => { preload( this.props.domainOnlySite ? 'domains' : 'stats' ); }; preloadReader = () => { preload( 'reader' ); }; preloadMe = () => { preload( 'me' ); }; goToCheckout = ( siteId ) => { this.props.recordTracksEvent( 'calypso_masterbar_cart_go_to_checkout' ); page( `/checkout/${ siteId }` ); }; onRemoveCartProduct = ( uuid = 'coupon' ) => { this.props.recordTracksEvent( 'calypso_masterbar_cart_remove_product', { uuid } ); }; isActive = ( section, ignoreNotifications = false ) => { if ( ignoreNotifications ) { return section === this.props.section; } return section === this.props.section && ! this.props.isNotificationsShowing; }; isMySitesActive = () => { const { isGlobalSidebarVisible, section } = this.props; return isGlobalSidebarVisible && ( 'sites' === section || 'sites-dashboard' === section ); }; isSidebarOpen = () => { return 'sidebar' === this.props.currentLayoutFocus; }; wordpressIcon = () => { // WP icon replacement for "horizon" environment if ( config( 'hostname' ) === 'horizon.wordpress.com' ) { return 'my-sites-horizon'; } return ( ); }; /** * Hamburger menu used by the global nav. * In nav unification, the menu is openned with the Sites button */ renderSidebarMobileMenu() { const { translate } = this.props; return ( } onClick={ this.handleToggleMobileMenu } isActive={ this.isSidebarOpen() } className="masterbar__item-sidebar-menu" tooltip={ translate( 'Menu' ) } /> ); } // will render as back button on mobile and in editor renderMySites() { const { domainOnlySite, siteSlug, translate, section, currentRoute, isGlobalSidebarVisible, siteAdminUrl, } = this.props; const mySitesUrl = domainOnlySite ? domainManagementList( siteSlug, currentRoute, true ) : '/sites'; const icon = this.wordpressIcon(); if ( ! siteSlug && section === 'sites-dashboard' ) { // we are the /sites page but there is no site. Disable the home link return ; } const subItems = isGlobalSidebarVisible ? null : [ [ { label: translate( 'Sites' ), url: '/sites', }, { label: translate( 'Domains' ), url: '/domains/manage', }, ], ...( this.props.isSimpleSite ? [] : [ [ { label: translate( 'About WordPress' ), url: `${ siteAdminUrl }about.php`, }, { label: translate( 'Get Involved' ), url: `${ siteAdminUrl }contribute.php`, }, ], ] ), ]; return ( ); } renderCheckout() { const { isCheckoutPending, isCheckoutFailed, previousPath, currentSelectedSiteSlug, isJetpackNotAtomic, title, loadHelpCenterIcon, isGravatarDomain, } = this.props; return ( ); } renderUpdatesMenu() { const { adminMenu } = this.props; if ( ! adminMenu ) { return null; } let updatesCount = 0; let updatesUrl = ''; for ( const menu of adminMenu ) { for ( const menuItem of menu.children || [] ) { if ( menuItem.slug === 'update-core-php' ) { updatesCount = menuItem.count; updatesUrl = menuItem.url; break; } } } if ( updatesCount ) { return ( } > { updatesCount } ); } return null; } renderCommentsMenu() { const { adminMenu } = this.props; if ( ! adminMenu ) { return null; } let commentsCount = 0; let commentsUrl = ''; for ( const menu of adminMenu ) { if ( menu.icon === 'dashicons-admin-comments' ) { commentsCount = menu.count || 0; commentsUrl = menu.url; break; } } return ( } > { commentsCount } ); } renderSiteBadges() { const { site, translate, isUnlaunchedSite, isTrial, isSiteP2, isP2Hub, isAtomicAndEditingToolkitDeactivated, } = this.props; if ( ! site ) { return null; } // Status badges const badges = []; // We show public coming soon badge only when the site is not private. const shouldShowPublicComingSoonSiteBadge = ! site.is_private && site.is_coming_soon && ! isAtomicAndEditingToolkitDeactivated && ! isTrial; // Cover the coming Soon v1 cases for sites still unlaunched and/or in Coming Soon private by default. // isPrivateAndUnlaunched means it is an unlaunched coming soon v1 site const isPrivateAndUnlaunched = site.is_private && isUnlaunchedSite; const shouldShowPrivateByDefaultComingSoonBadge = site.is_coming_soon || isPrivateAndUnlaunched; // P2 Badge if ( isSiteP2 && ! isP2Hub ) { badges.push( 'P2' ); } // Staging Badge if ( site?.is_wpcom_staging_site ) { badges.push( translate( 'Staging' ) ); } // Trial Badge if ( isTrial ) { badges.push( translate( 'Trial' ) ); } // P2 Workspace Badge if ( isP2Hub ) { badges.push( 'P2 Workspace' ); } // Private/Coming Soon Badge if ( site.is_private ) { badges.push( shouldShowPrivateByDefaultComingSoonBadge ? translate( 'Coming Soon' ) : translate( 'Private' ) ); } // Express Service Badge if ( site.options && site.options.is_difm_lite_in_progress ) { badges.push( translate( 'Express Service' ) ); } // Public Coming Soon Badge if ( shouldShowPublicComingSoonSiteBadge ) { badges.push( translate( 'Coming Soon' ) ); } // Redirect Badge if ( site.options && site.options.is_redirect ) { badges.push( translate( 'Redirect' ) ); } // Domain Badge if ( site.options && site.options.is_domain_only ) { badges.push( translate( 'Domain' ) ); } return badges.length > 0 ? badges.map( ( badge ) => ( { badge } ) ) : null; } renderSiteMenu() { const { siteSlug, translate, siteTitle, siteUrl, isClassicView, siteAdminUrl, siteHomeUrl, domainOnlySite, sitePlanName, site, } = this.props; // Only display when a site is selected and is not domain-only site. if ( ! siteSlug || domainOnlySite ) { return null; } const siteHomeOrAdminItem = isClassicView ? { label: translate( 'Dashboard' ), url: siteAdminUrl, } : { label: translate( 'My Home' ), url: siteHomeUrl, }; // Get site badges const siteBadges = this.renderSiteBadges(); // Create a site status item for the dropdown if we have badges const menuItems = [ [ { label: translate( 'Visit Site' ), url: siteUrl }, siteHomeOrAdminItem ], [ ...( ! site?.is_wpcom_staging_site ? [ { label: (
{ translate( 'Plan' ) }
{ sitePlanName }
), onClick: () => { this.props.recordTracksEvent( 'calypso_masterbar_plan_clicked' ); page( `/plans/${ siteSlug }` ); }, }, ] : [] ), { label: (
{ siteBadges && siteBadges.length > 0 && (
{ translate( 'Status' ) }
{ siteBadges }
) }
), }, ], ]; return ( } tipTarget="visit-site" subItems={ menuItems } > { siteTitle.length > 40 ? `${ siteTitle.substring( 0, 40 ) }\u2026` : siteTitle } ); } renderSiteActionMenu() { const { siteSlug, isClassicView, translate, siteAdminUrl, newPostUrl, newPageUrl, domainOnlySite, isMigrationInProgress, isEcommerce, hasNoSites, } = this.props; // Only display on site-specific pages. // domainOnlySite's still get currentSelectedSiteSlug, removing this check would require changing checks below. if ( domainOnlySite || isMigrationInProgress || isEcommerce || hasNoSites ) { return null; } let siteActions = []; if ( siteSlug ) { siteActions = [ { label: translate( 'Post' ), url: newPostUrl, }, { label: translate( 'Media' ), url: isClassicView ? `${ siteAdminUrl }media-new.php` : `/media/${ siteSlug }`, }, { label: translate( 'Page' ), url: newPageUrl, }, { label: translate( 'User' ), url: isClassicView ? `${ siteAdminUrl }user-new.php` : `/people/new/${ siteSlug }`, }, ]; } else { siteActions = [ { label: translate( 'Post' ), url: '/post', }, { label: translate( 'Media' ), url: '/media', }, { label: translate( 'Page' ), url: '/page', }, { label: translate( 'User' ), url: '/people/new', }, ]; } return ( } tooltip={ translate( 'New', { context: 'admin bar menu group label' } ) } tipTarget="new-menu" > { translate( 'New', { context: 'admin bar menu group label' } ) } ); } renderLaunchButton() { const { isA4ADevSite, isUnlaunchedSite, siteId, translate, isManageSiteOptionsEnabled } = this.props; if ( ! isUnlaunchedSite || ! isManageSiteOptionsEnabled || isA4ADevSite ) { return null; } return ( } onClick={ () => { this.props.recordTracksEvent( 'calypso_masterbar_launch_site' ); this.props.launchSiteOrRedirectToLaunchSignupFlow( siteId ); } } > { translate( 'Launch site' ) } ); } renderProfileMenu() { const { translate, user, isGlobalSidebarVisible, siteAdminUrl } = this.props; const profileActions = [ { label: (
{ user.display_name } { user.username } { isGlobalSidebarVisible ? translate( 'My Profile' ) : translate( 'Edit Profile' ) }
), url: isGlobalSidebarVisible ? '/me' : `${ siteAdminUrl }profile.php`, }, { label: translate( 'Log Out' ), onClick: () => this.props.redirectToLogout(), tooltip: translate( 'Log out of WordPress.com' ), className: 'logout-link', }, ]; const wpcomActions = [ { label: ( { translate( 'My {{wpcomIcon/}} WordPress.com Account', { components: { wpcomIcon: this.wordpressIcon(), }, } ) } ), url: '/me/account', className: 'wpcom-link', }, ]; return ( { translate( 'Howdy, %(display_name)s', { args: { display_name: user.display_name }, } ) } ); } renderReader() { const { translate } = this.props; return ( } onClick={ this.clickReader } isActive={ this.isActive( 'reader', true ) } tooltip={ translate( 'Read the blogs and topics you follow' ) } preloadSection={ this.preloadReader } hasGlobalBorderStyle > { translate( 'Reader' ) } ); } renderLanguageSwitcher() { if ( this.props.isSupportSession || config.isEnabled( 'quick-language-switcher' ) ) { return ; } return null; } renderCart() { const { siteSlug, siteId, sectionGroup } = this.props; // Only display the masterbar cart when we are viewing a site-specific page. if ( sectionGroup !== 'sites' ) { return null; } return ( ); } renderNotifications() { const { translate } = this.props; return ( { translate( 'Notifications', { comment: 'Toolbar, must be shorter than ~12 chars', } ) } ); } renderHelpCenter() { const { siteId, translate } = this.props; return ( ); } render() { const { isCheckout, isCheckoutPending, isCheckoutFailed, loadHelpCenterIcon } = this.props; // Checkout flow uses it's own version of the masterbar if ( isCheckout || isCheckoutPending || isCheckoutFailed ) { return this.renderCheckout(); } return (
{ this.renderSidebarMobileMenu() } { this.renderMySites() } { this.renderSiteMenu() } { this.renderUpdatesMenu() } { this.renderCommentsMenu() } { this.renderSiteActionMenu() } { this.renderLanguageSwitcher() } { this.renderLaunchButton() }
{ this.renderCart() } { this.renderReader() } { loadHelpCenterIcon && this.renderHelpCenter() } { this.renderNotifications() } { this.renderProfileMenu() }
); } } export default connect( ( state ) => { const sectionGroup = getSectionGroup( state ); // Falls back to using the user's primary site if no site has been selected // by the user yet const siteId = getMostRecentlySelectedSiteId( state ) || getPrimarySiteId( state ); const sitePlanSlug = getSitePlanSlug( state, siteId ); const isMigrationInProgress = isSiteMigrationInProgress( state, siteId ) || isSiteMigrationActiveRoute( state ); const siteCount = getCurrentUserSiteCount( state ) ?? 0; const site = getSite( state, siteId ); const isClassicView = site && siteUsesWpAdminInterface( site ); return { isManageSiteOptionsEnabled: canCurrentUserManageSiteOptions( state, siteId ), isNotificationsShowing: isNotificationsOpen( state ), isEcommerce: isEcommercePlan( sitePlanSlug ), isA4ADevSite: site?.is_a4a_dev_site, siteId: siteId, site: site, siteSlug: getSiteSlug( state, siteId ), sitePlanName: getSitePlanName( state, siteId ), siteTitle: getSiteTitle( state, siteId ), siteUrl: getSiteUrl( state, siteId ), siteAdminUrl: getSiteAdminUrl( state, siteId ), siteHomeUrl: getSiteHomeUrl( state, siteId ), adminMenu: getAdminMenu( state, siteId ), sectionGroup, domainOnlySite: isDomainOnlySite( state, siteId ), hasNoSites: siteCount === 0, user: getCurrentUser( state ), isSupportSession: isSupportSession( state ), isMigrationInProgress, migrationStatus: getSiteMigrationStatus( state, siteId ), isClassicView, currentSelectedSiteSlug: siteId ? getSiteSlug( state, siteId ) : undefined, previousPath: getPreviousRoute( state ), isSimpleSite: isSimpleSite( state, siteId ), isJetpackNotAtomic: isJetpackSite( state, siteId ) && ! isAtomicSite( state, siteId ), currentLayoutFocus: getCurrentLayoutFocus( state ), currentRoute: getCurrentRoute( state ), newPostUrl: getEditorUrl( state, siteId, null, 'post' ), newPageUrl: getEditorUrl( state, siteId, null, 'page' ), isUnlaunchedSite: getIsUnlaunchedSite( state, siteId ), isTrial: isTrialSite( state, siteId ), isSiteP2: isSiteWPForTeams( state, siteId ), isP2Hub: isSiteP2Hub( state, siteId ), isAtomicAndEditingToolkitDeactivated: isAtomicSite( state, siteId ) && getSiteOption( state, siteId, 'editing_toolkit_is_active' ) === false, isGravatarDomain: hasGravatarDomainQueryParam( state ), }; }, { setNextLayoutFocus, recordTracksEvent, updateSiteMigrationMeta, activateNextLayoutFocus, savePreference, requestAdminMenu, redirectToLogout, launchSiteOrRedirectToLaunchSignupFlow, } )( localize( MasterbarLoggedIn ) );