import config from '@automattic/calypso-config';
import { Button } from '@automattic/components';
import { protect, akismet } from '@automattic/components/src/icons';
import { formatNumberCompact } from '@automattic/number-formatters';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { useState, FunctionComponent } from 'react';
import wpcom from 'calypso/lib/wp';
import useModuleDataQuery from '../hooks/use-module-data-query';
import canCurrentUser from '../lib/selectors/can-current-user';
import './modules.scss';
interface ModuleCardProps {
icon: JSX.Element;
title: string;
value: number;
error: string;
activateProduct: () => Promise< void >;
isLoading: boolean;
isError: boolean;
canManageModule: boolean;
className?: string;
manageUrl?: string;
}
interface ProtectModuleProps {
siteId: number;
}
interface ModulesProps extends ProtectModuleProps {
adminBaseUrl: null | string;
}
interface AkismetModuleProps extends ProtectModuleProps {
manageUrl: string;
}
const ModuleCard: FunctionComponent< ModuleCardProps > = ( {
icon,
title,
value,
error,
activateProduct,
manageUrl,
canManageModule,
isLoading,
isError,
className = null,
} ) => {
const translate = useTranslate();
const [ disabled, setDisabled ] = useState( false );
const onActivateProduct = () => {
setDisabled( true );
activateProduct().catch( () => setDisabled( false ) );
};
return (
{ icon }
{ title }
{ isLoading &&
-
}
{ ! isLoading && (
<>
{ ( ! isError || ! canManageModule ) && (
{ formatNumberCompact( value ) }
) }
{ isError && canManageModule && (
{ error === 'not_active' && (
) }
{ error === 'not_installed' && (
) }
{ error === 'invalid_key' && (
{ translate( 'Manage Akismet key' ) }
) }
{ ! [ 'not_active', 'not_installed', 'invalid_key' ].includes( error ) && (
{ translate( 'An error occurred.' ) }
) }
) }
>
) }
);
};
const AkismetModule: FunctionComponent< AkismetModuleProps > = ( { siteId, manageUrl } ) => {
const translate = useTranslate();
const {
data: akismetData,
isLoading: isAkismetLoading,
refetch: refetchAkismetData,
isError: isAkismetError,
error: akismetError,
} = useModuleDataQuery( 'akismet' );
// The function installs Akismet plugin if not exists.
const activateProduct = ( productSlug: string ) => () => {
return wpcom.req
.post( {
apiNamespace: 'my-jetpack/v1',
path: `/site/products/${ productSlug }`,
} )
.then( refetchAkismetData );
};
return (
);
};
const ProtectModule: FunctionComponent< ProtectModuleProps > = ( { siteId } ) => {
const translate = useTranslate();
const {
data: protectData,
isLoading: isProtectLoading,
refetch: refetchProtectData,
isError: isProtectError,
error: protectError,
} = useModuleDataQuery( 'protect' );
const activateModule = ( module: string ) => () => {
return wpcom.req
.post( { path: '/settings', apiNamespace: 'jetpack/v4' }, { [ module ]: true } )
.then( refetchProtectData );
};
return (
);
};
export default function Modules( { siteId, adminBaseUrl }: ModulesProps ) {
const isWPAdminAndNotSimpleSite = config.isEnabled( 'is_running_in_jetpack_site' );
// Akismet and Protect modules are not available on Simple sites.
if ( ! isWPAdminAndNotSimpleSite ) {
return null;
}
return (
);
}