import { useQuery, useSuspenseQuery, useMutation } from '@tanstack/react-query'; import { Link } from '@tanstack/react-router'; import { Card, CardBody, __experimentalHStack as HStack, __experimentalVStack as VStack, __experimentalText as Text, Button, Tooltip, } from '@wordpress/components'; import { useDispatch } from '@wordpress/data'; import { DataForm } from '@wordpress/dataviews'; import { createInterpolateElement } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; import { useEffect, useState } from 'react'; import { siteBySlugQuery } from '../../app/queries/site'; import { siteEdgeCacheStatusQuery, siteEdgeCacheStatusMutation, siteEdgeCacheClearMutation, siteEdgeCacheLastClearedTimestampQuery, siteObjectCacheClearMutation, siteObjectCacheLastClearedTimestampQuery, } from '../../app/queries/site-cache'; import { ActionList } from '../../components/action-list'; import InlineSupportLink from '../../components/inline-support-link'; import Notice from '../../components/notice'; import PageLayout from '../../components/page-layout'; import { HostingFeatures } from '../../data/constants'; import { hasHostingFeature, hasPlanFeature } from '../../utils/site-features'; import HostingFeatureGatedWithCallout from '../hosting-feature-gated-with-callout'; import SettingsPageHeader from '../settings-page-header'; import { isEdgeCacheAvailable as getIsEdgeCacheAvailable } from './utils'; import type { Field } from '@wordpress/dataviews'; type CachingFormData = { active: boolean; }; const fields: Field< CachingFormData >[] = [ { id: 'active', label: __( 'Enable global edge caching for faster content delivery' ), Edit: 'checkbox', }, ]; const form = { type: 'regular' as const, fields: [ 'active' ], }; export default function CachingSettings( { siteSlug }: { siteSlug: string } ) { const { data: site } = useSuspenseQuery( siteBySlugQuery( siteSlug ) ); const canView = hasHostingFeature( site, HostingFeatures.CACHING ); const { data: isEdgeCacheActive } = useQuery( { ...siteEdgeCacheStatusQuery( site.ID ), enabled: canView, } ); const { data: lastEdgeCacheClearedTimestamp = 0 } = useQuery( { ...siteEdgeCacheLastClearedTimestampQuery( site.ID ), enabled: canView, } ); const { data: lastObjectCacheClearedTimestamp = 0 } = useQuery( { ...siteObjectCacheLastClearedTimestampQuery( site.ID ), enabled: canView, } ); const edgeCacheStatusMutation = useMutation( siteEdgeCacheStatusMutation( site.ID ) ); const edgeCacheClearMutation = useMutation( siteEdgeCacheClearMutation( site.ID ) ); const objectCacheClearMutation = useMutation( siteObjectCacheClearMutation( site.ID ) ); const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore ); const isEdgeCacheAvailable = site && getIsEdgeCacheAvailable( site ); const isEdgeCacheEnabled = isEdgeCacheAvailable && isEdgeCacheActive; const [ formData, setFormData ] = useState< CachingFormData >( { active: isEdgeCacheEnabled ?? false, } ); const isDirty = isEdgeCacheEnabled !== formData.active; const { isPending } = edgeCacheStatusMutation; const handleUpdateEdgeCacheStatus = ( e: React.FormEvent ) => { e.preventDefault(); edgeCacheStatusMutation.mutate( formData.active, { onSuccess: () => { createSuccessNotice( formData.active ? __( 'Global edge cache enabled.' ) : __( 'Global edge cache disabled.' ), { type: 'snackbar' } ); }, onError: () => { createErrorNotice( __( 'Failed to save global edge cache settings.' ), { type: 'snackbar', } ); }, } ); }; const handleClearEdgeCache = () => { edgeCacheClearMutation.mutate( undefined, { onSuccess: () => { createSuccessNotice( __( 'Global edge cache cleared.' ), { type: 'snackbar' } ); }, onError: () => { createErrorNotice( __( 'Failed to clear edge cache.' ), { type: 'snackbar' } ); }, } ); }; const handleClearObjectCache = () => { objectCacheClearMutation.mutate( 'Manually clearing again.', { onSuccess: () => { createSuccessNotice( __( 'Object cache cleared.' ), { type: 'snackbar' } ); }, onError: () => { createErrorNotice( __( 'Failed to clear object cache.' ), { type: 'snackbar' } ); }, } ); }; const [ isClearingAllCaches, setIsClearingAllCaches ] = useState( false ); useEffect( () => { if ( ! edgeCacheClearMutation.isPending && ! objectCacheClearMutation.isPending ) { setIsClearingAllCaches( false ); } }, [ edgeCacheClearMutation.isPending, objectCacheClearMutation.isPending ] ); const handleClearAllCaches = () => { if ( isEdgeCacheEnabled ) { handleClearEdgeCache(); } handleClearObjectCache(); setIsClearingAllCaches( true ); }; const renderForm = () => { if ( ! isEdgeCacheAvailable ) { return ( { __( 'Faster content delivery with global edge caching is available for public sites.' ) } { createInterpolateElement( __( 'Review site visibility settings.' ), { a: , } ) } ); } return (
data={ formData } fields={ fields } form={ form } onChange={ ( edits: Partial< CachingFormData > ) => { setFormData( ( data ) => ( { ...data, ...edits } ) ); } } />
); }; const renderActions = () => { const ONE_MINUTE_IN_MILLISECONDS = 60 * 1000; const isClearingEdgeCacheRateLimited = lastEdgeCacheClearedTimestamp > Date.now() - ONE_MINUTE_IN_MILLISECONDS; const isClearingObjectCacheRateLimited = lastObjectCacheClearedTimestamp > Date.now() - ONE_MINUTE_IN_MILLISECONDS; const isClearingAllCachesRateLimited = isClearingEdgeCacheRateLimited && isClearingObjectCacheRateLimited; const wrapMaybeDisabledButtonWithTooltip = ( isButtonDisabled: boolean, tooltipText: string, button: JSX.Element ) => { if ( ! isButtonDisabled ) { return button; } // We must wrap the disabled button in a div so that the hover event reaches the Tooltip component. return (
{ button }
); }; return ( { __( 'Clear' ) } ) } /> { __( 'Clear' ) } ) } /> { __( 'Clear all' ) } ) } /> ); }; const description = hasPlanFeature( site, HostingFeatures.CACHING ) ? createInterpolateElement( __( 'Manage your site’s server-side caching. Learn more' ), { link: , } ) : createInterpolateElement( sprintf( /* translators: %s: plan name. Eg. 'Personal' */ __( 'Caching is managed for you on the %s plan. The cache is cleared automatically as you make changes to your site. Learn more' ), site?.plan?.product_name_short ), { link: , } ); return ( } > { renderForm() } { renderActions() } ); }