File size: 2,536 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 |
import { WPCOM_FEATURES_MANAGE_PLUGINS } from '@automattic/calypso-products';
import { Button } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { useSelector, useDispatch } from 'react-redux';
import { ACTIVATE_PLUGIN, DEACTIVATE_PLUGIN } from 'calypso/lib/plugins/constants';
import { getSoftwareSlug } from 'calypso/lib/plugins/utils';
import { togglePluginActivation } from 'calypso/state/plugins/installed/actions';
import {
getPluginOnSite,
isPluginActionInProgress,
} from 'calypso/state/plugins/installed/selectors';
import { isMarketplaceProduct as isMarketplaceProductSelector } from 'calypso/state/products-list/selectors';
import isSiteAutomatedTransfer from 'calypso/state/selectors/is-site-automated-transfer';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedSite } from 'calypso/state/ui/selectors';
export const ActivationButton = ( { plugin, active } ) => {
const translate = useTranslate();
const dispatch = useDispatch();
const isMarketplaceProduct = useSelector( ( state ) =>
isMarketplaceProductSelector( state, plugin.slug )
);
const softwareSlug = getSoftwareSlug( plugin, isMarketplaceProduct );
// Site type
const selectedSite = useSelector( getSelectedSite );
const isJetpack = useSelector( ( state ) => isJetpackSite( state, selectedSite?.ID ) );
const isAtomic = useSelector( ( state ) => isSiteAutomatedTransfer( state, selectedSite?.ID ) );
const sitePlugin = useSelector( ( state ) =>
getPluginOnSite( state, selectedSite?.ID, softwareSlug )
);
const jetpackNonAtomic = isJetpack && ! isAtomic;
const hasManagePlugins = useSelector(
( state ) =>
siteHasFeature( state, selectedSite?.ID, WPCOM_FEATURES_MANAGE_PLUGINS ) || jetpackNonAtomic
);
const autoManagedPlugins = [ 'jetpack', 'vaultpress', 'akismet' ];
const isManagedPlugin = isAtomic && autoManagedPlugins.includes( softwareSlug );
const canManagePlugins = ( isJetpack && ! isAtomic ) || ( isAtomic && hasManagePlugins );
const activationInProgress = useSelector( ( state ) =>
isPluginActionInProgress( state, selectedSite?.ID, plugin.id, [
ACTIVATE_PLUGIN,
DEACTIVATE_PLUGIN,
] )
);
return (
<Button
onClick={ () => dispatch( togglePluginActivation( selectedSite?.ID, sitePlugin ) ) }
disabled={ activationInProgress || isManagedPlugin || ! canManagePlugins }
>
{ active ? translate( 'Deactivate' ) : translate( 'Activate' ) }
</Button>
);
};
|