File size: 6,402 Bytes
1e92f2d |
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 |
import {
WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS,
WPCOM_FEATURES_MANAGE_PLUGINS,
} from '@automattic/calypso-products';
import { Button } from '@automattic/components';
import { useBreakpoint } from '@automattic/viewport-react';
import { Icon, upload } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useEffect, useMemo } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import NavigationHeader from 'calypso/components/navigation-header';
import { useLocalizedPlugins, useServerEffect } from 'calypso/my-sites/plugins/utils';
import { recordTracksEvent, recordGoogleEvent } from 'calypso/state/analytics/actions';
import { appendBreadcrumb, resetBreadcrumbs } from 'calypso/state/breadcrumb/actions';
import { getBreadcrumbs } from 'calypso/state/breadcrumb/selectors';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import getPreviousRoute from 'calypso/state/selectors/get-previous-route';
import isAtomicSite from 'calypso/state/selectors/is-site-automated-transfer';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { getSiteAdminUrl, isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import './style.scss';
const UploadPluginButton = ( { isMobile, siteSlug, hasUploadPlugins } ) => {
const dispatch = useDispatch();
const translate = useTranslate();
if ( ! hasUploadPlugins ) {
return null;
}
const uploadUrl = '/plugins/upload' + ( siteSlug ? '/' + siteSlug : '' );
const handleUploadPluginButtonClick = () => {
dispatch( recordTracksEvent( 'calypso_click_plugin_upload' ) );
dispatch( recordGoogleEvent( 'Plugins', 'Clicked Plugin Upload Link' ) );
};
return (
<Button
className="plugins-browser__button"
onClick={ handleUploadPluginButtonClick }
href={ uploadUrl }
>
<Icon className="plugins-browser__button-icon" icon={ upload } width={ 18 } height={ 18 } />
{ ! isMobile && (
<span className="plugins-browser__button-text">{ translate( 'Upload' ) }</span>
) }
</Button>
);
};
const ManageButton = ( {
shouldShowManageButton,
siteAdminUrl,
siteSlug,
jetpackNonAtomic,
hasManagePlugins,
} ) => {
const translate = useTranslate();
if ( ! shouldShowManageButton ) {
return null;
}
const site = siteSlug ? '/' + siteSlug : '';
// When no site is selected eg `/plugins` or when Jetpack is self hosted
// or if the site does not have the manage plugins feature show the
// Calypso Plugins Manage page.
// In any other case, redirect to current site WP Admin.
const managePluginsDestination =
! siteAdminUrl || jetpackNonAtomic || ! hasManagePlugins
? `/plugins/manage${ site }`
: `${ siteAdminUrl }plugins.php`;
return (
<Button className="plugins-browser__button" href={ managePluginsDestination }>
<span className="plugins-browser__button-text">{ translate( 'Installed plugins' ) }</span>
</Button>
);
};
const PluginsNavigationHeader = ( { navigationHeaderRef, categoryName, category, search } ) => {
const dispatch = useDispatch();
const translate = useTranslate();
const isLoggedIn = useSelector( isUserLoggedIn );
const selectedSite = useSelector( getSelectedSite );
const jetpackNonAtomic = useSelector(
( state ) =>
isJetpackSite( state, selectedSite?.ID ) && ! isAtomicSite( state, selectedSite?.ID )
);
const siteAdminUrl = useSelector( ( state ) => getSiteAdminUrl( state, selectedSite?.ID ) );
const isJetpack = useSelector( ( state ) => isJetpackSite( state, selectedSite?.ID ) );
const isMobile = useBreakpoint( '<960px' );
const hasInstallPurchasedPlugins = useSelector( ( state ) =>
siteHasFeature( state, selectedSite?.ID, WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS )
);
const hasManagePlugins = useSelector( ( state ) =>
siteHasFeature( state, selectedSite?.ID, WPCOM_FEATURES_MANAGE_PLUGINS )
);
const shouldShowManageButton = useMemo( () => {
return jetpackNonAtomic || ( isJetpack && ( hasInstallPurchasedPlugins || hasManagePlugins ) );
}, [ jetpackNonAtomic, isJetpack, hasInstallPurchasedPlugins, hasManagePlugins ] );
const { localizePath } = useLocalizedPlugins();
const setBreadcrumbs = ( breadcrumbs = [] ) => {
const pluginsBreadcrumb = {
label: translate( 'Plugins' ),
href: localizePath( `/plugins/${ selectedSite?.slug || '' }` ),
id: 'plugins',
};
if ( breadcrumbs?.length === 0 || ( ! category && ! search ) ) {
dispatch( resetBreadcrumbs() );
dispatch( appendBreadcrumb( pluginsBreadcrumb ) );
}
if ( category ) {
resetBreadcrumbs();
dispatch( appendBreadcrumb( pluginsBreadcrumb ) );
dispatch(
appendBreadcrumb( {
label: categoryName,
href: localizePath( `/plugins/browse/${ category }/${ selectedSite?.slug || '' }` ),
id: 'category',
} )
);
}
if ( search ) {
dispatch( resetBreadcrumbs() );
dispatch( appendBreadcrumb( pluginsBreadcrumb ) );
dispatch(
appendBreadcrumb( {
label: translate( 'Search Results' ),
href: localizePath( `/plugins/${ selectedSite?.slug || '' }?s=${ search }` ),
id: 'plugins-search',
} )
);
}
};
const previousRoute = useSelector( getPreviousRoute );
useEffect( () => {
/* If translatations change, reset and update the breadcrumbs */
if ( ! previousRoute ) {
setBreadcrumbs();
}
}, [ translate ] );
useServerEffect( () => {
setBreadcrumbs();
} );
/* We need to get the breadcrumbs after the server has set them */
const breadcrumbs = useSelector( getBreadcrumbs );
useEffect( () => {
setBreadcrumbs( breadcrumbs );
}, [ selectedSite?.slug, search, category, categoryName, dispatch, localizePath ] );
return (
<NavigationHeader
className="plugins-navigation-header"
compactBreadcrumb={ isMobile }
ref={ navigationHeaderRef }
title={ translate( 'Plugins {{wbr}}{{/wbr}}marketplace', {
components: { wbr: <wbr /> },
} ) }
loggedIn={ isLoggedIn }
>
<ManageButton
shouldShowManageButton={ shouldShowManageButton }
siteAdminUrl={ siteAdminUrl }
siteSlug={ selectedSite?.slug }
jetpackNonAtomic={ jetpackNonAtomic }
hasManagePlugins={ hasManagePlugins }
/>
<UploadPluginButton
isMobile={ isMobile }
siteSlug={ selectedSite?.slug }
hasUploadPlugins={ !! selectedSite }
/>
</NavigationHeader>
);
};
export default PluginsNavigationHeader;
|