import { Button, FormLabel } from '@automattic/components'; import { CALYPSO_CONTACT } from '@automattic/urls'; import { Icon } from '@wordpress/components'; import { info } from '@wordpress/icons'; import { useTranslate } from 'i18n-calypso'; import { useState } from 'react'; import FormSelect from 'calypso/components/forms/form-select'; import InlineSupportLink from 'calypso/components/inline-support-link'; import { useLocalizedMoment } from 'calypso/components/localized-moment'; import { PanelCard, PanelCardDescription, PanelCardHeading } from 'calypso/components/panel'; import { useEdgeCacheDefensiveModeMutation, useEdgeCacheDefensiveModeQuery, } from 'calypso/data/hosting/use-cache'; import { EdgeCacheLoadingPlaceholder } from 'calypso/sites/settings/performance/edge-cache-loading-placeholder'; import { useDispatch, useSelector } from 'calypso/state'; import { recordTracksEvent } from 'calypso/state/analytics/actions'; import { getSelectedSiteId } from 'calypso/state/ui/selectors'; import './defensive-mode-form.scss'; type DefensiveModeFormProps = { disabled?: boolean; }; export default function DefensiveModeForm( { disabled }: DefensiveModeFormProps ) { const translate = useTranslate(); const dispatch = useDispatch(); const moment = useLocalizedMoment(); const availableTtls = [ { label: translate( '1 hour' ), value: 3600, }, { label: translate( '12 hours' ), value: 43200, }, { label: translate( '24 hours' ), value: 86400, }, { label: translate( '2 days' ), value: 172800, }, { label: translate( '7 days' ), value: 604800, }, ]; const siteId = useSelector( getSelectedSiteId ); const [ ttl, setTtl ] = useState( availableTtls[ 0 ].value ); const { data: defensiveModeData, isLoading: isLoadingDefensiveMode } = useEdgeCacheDefensiveModeQuery( siteId ); const { mutate, isPending: isEnabling } = useEdgeCacheDefensiveModeMutation( siteId ); const enabledUntil = moment.unix( defensiveModeData?.enabled_until ?? 0 ).local(); return ( { translate( 'Defensive mode', { comment: 'Defensive mode is a feature to protect against DDoS attacks.', textOnly: true, } ) } { translate( 'Extra protection against spam bots and attacks. Visitors will see a quick loading page while we run additional security checks. {{a}}Learn more{{/a}}', { comment: 'Explains what "Defensive mode" is. Defensive mode is a feature to protect against DDoS attacks.', components: { a: , }, } ) } { isLoadingDefensiveMode && } { ! isLoadingDefensiveMode && defensiveModeData?.enabled && ( <>
{ translate( '{{b}}Defensive mode is enabled{{/b}} until %(date)s.', { args: { date: enabledUntil.format( 'LLL' ), }, comment: 'Defensive mode is a feature to protect against DDoS attacks.', components: { b: , }, } ) }
{ ! defensiveModeData.enabled_by_a11n && ( ) } { defensiveModeData.enabled_by_a11n && (

{ translate( 'Defensive mode was enabled on your behalf to protect your site.', { comment: 'Defensive mode is a feature to protect against DDoS attacks.' } ) }

{ translate( 'Please {{a}}contact support{{/a}} if you need to disable defensive mode.', { comment: 'Defensive mode is a feature to protect against DDoS attacks. This text is dislayed when defensive mode was enabled on behalf of users.', components: { a: , }, } ) }

) } ) } { ! isLoadingDefensiveMode && ! defensiveModeData?.enabled && ( <> { translate( 'Duration' ) } setTtl( Number( event.currentTarget.value ) ) } value={ ttl } > { availableTtls.map( ( option ) => ( ) ) } ) } ); }