import { Badge } from '@automattic/ui'; import { useQuery } from '@tanstack/react-query'; import { Link } from '@tanstack/react-router'; import { __experimentalText as Text, __experimentalHStack as HStack, __experimentalVStack as VStack, ExternalLink, } from '@wordpress/components'; import { useResizeObserver } from '@wordpress/compose'; import { __, sprintf } from '@wordpress/i18n'; import { useInView } from 'react-intersection-observer'; import { useAnalytics } from '../../app/analytics'; import { useAuth } from '../../app/auth'; import { siteLatestAtomicTransferQuery } from '../../app/queries/site-atomic-transfers'; import { siteLastBackupQuery } from '../../app/queries/site-backups'; import { siteMediaStorageQuery } from '../../app/queries/site-media-storage'; import { sitePHPVersionQuery } from '../../app/queries/site-php-version'; import { siteEngagementStatsQuery } from '../../app/queries/site-stats'; import { siteUptimeQuery } from '../../app/queries/site-uptime'; import ComponentViewTracker from '../../components/component-view-tracker'; import { TextBlur } from '../../components/text-blur'; import TimeSince from '../../components/time-since'; import { DotcomFeatures, HostingFeatures, JetpackModules } from '../../data/constants'; import { isAtomicTransferInProgress } from '../../utils/site-atomic-transfers'; import { hasHostingFeature, hasJetpackModule, hasPlanFeature } from '../../utils/site-features'; import { getSiteStatus, getSiteStatusLabel } from '../../utils/site-status'; import { isSelfHostedJetpackConnected, isP2 } from '../../utils/site-types'; import { canManageSite } from '../features'; import { isSitePlanTrial } from '../plans'; import SiteIcon from '../site-icon'; import SitePreview from '../site-preview'; import { JetpackLogo } from './jetpack-logo'; import type { AtomicTransferStatus, Site } from '../../data/types'; import './style.scss'; function IneligibleIndicator() { return -; } function LoadingIndicator( { label }: { label: string } ) { return { label }; } function getSiteManagementUrl( site: Site ) { if ( canManageSite( site ) ) { return `/sites/${ site.slug }`; } return site.options?.admin_url; } const titleFieldTextOverflowStyles = { overflowX: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', } as const; export function Name( { site, value }: { site: Site; value: string } ) { const renderBadge = () => { if ( site.is_wpcom_staging_site ) { return { __( 'Staging' ) }; } if ( isSitePlanTrial( site ) ) { return { __( 'Trial' ) }; } if ( isP2( site ) ) { return { __( 'P2' ) }; } return null; }; return ( { site.is_deleted ? ( { value } ) : ( { value } ) } { renderBadge() } ); } export function URL( { site, value }: { site: Site; value: string } ) { return site.is_deleted ? ( { value } ) : ( { value } ); } export function SiteIconLink( { site }: { site: Site } ) { return ( ); } export function Preview( { site }: { site: Site } ) { const [ resizeListener, { width } ] = useResizeObserver(); const { is_deleted, is_private, URL: url } = site; // If the site is a private A8C site, X-Frame-Options is set to same // origin. const iframeDisabled = is_deleted || ( site.is_a8c && is_private ); return ( { resizeListener } { iframeDisabled && (
) } { width && ! iframeDisabled && ( ) } ); } export function EngagementStat( { site, type, }: { site: Site; type: 'visitors' | 'views' | 'likes'; } ) { const { ref, inView } = useInView( { triggerOnce: true, fallbackInView: true } ); const isEligible = ! site.is_deleted && ( ! site.jetpack || hasJetpackModule( site, JetpackModules.STATS ) ); const { data: stats, isLoading } = useQuery( { ...siteEngagementStatsQuery( site.ID ), enabled: isEligible && inView, } ); if ( ! isEligible ) { return ; } const renderContent = () => { if ( isLoading ) { return ; } return stats?.currentData[ type ]; }; return { renderContent() }; } export function LastBackup( { site }: { site: Site } ) { const { ref, inView } = useInView( { triggerOnce: true, fallbackInView: true } ); const isEligible = hasHostingFeature( site, HostingFeatures.BACKUPS ); const { data: lastBackup, isLoading, isError, } = useQuery( { ...siteLastBackupQuery( site.ID ), enabled: isEligible && inView, } ); if ( ! isEligible ) { return ; } const renderContent = () => { if ( isLoading ) { return ; } if ( ! lastBackup || isError ) { return ; } return ; }; return { renderContent() }; } export function Uptime( { site }: { site: Site } ) { const { ref, inView } = useInView( { triggerOnce: true, fallbackInView: true } ); const isEligible = hasJetpackModule( site, JetpackModules.MONITOR ); const { data: uptime, isLoading } = useQuery( { ...siteUptimeQuery( site.ID, 'week' ), enabled: isEligible && inView, } ); if ( ! isEligible ) { return ; } const renderContent = () => { if ( isLoading ) { return ; } return uptime ? `${ uptime }%` : ; }; return { renderContent() }; } export function PHPVersion( { site }: { site: Site } ) { const isEligible = hasHostingFeature( site, HostingFeatures.PHP ); const { ref, inView } = useInView( { triggerOnce: true, fallbackInView: true, } ); const { data, isLoading } = useQuery( { ...sitePHPVersionQuery( site.ID ), enabled: isEligible && inView, } ); if ( ! isEligible ) { return ; } return { ! isLoading ? data : }; } export function MediaStorage( { site }: { site: Site } ) { const { ref, inView } = useInView( { triggerOnce: true, fallbackInView: true, } ); const { data: mediaStorage, isLoading } = useQuery( { ...siteMediaStorageQuery( site.ID ), enabled: inView, } ); const value = mediaStorage ? ( `${ Math.round( ( mediaStorage.storage_used_bytes / mediaStorage.max_storage_bytes ) * 1000 ) / 10 }%` ) : ( ); return { ! isLoading ? value : }; } function SiteLaunchNag( { site }: { site: Site } ) { const { recordTracksEvent } = useAnalytics(); // TODO: We have to fix the obscured focus ring issue as the dataview's field value container // uses `overflow:hidden` to prevent any of the fields from overflowing. return ( <> { recordTracksEvent( 'calypso_dashboard_sites_site_launch_nag_click' ); } } > { __( 'Finish setup' ) } ); } function PlanRenewNag( { site, source }: { site: Site; source: string } ) { const { user } = useAuth(); const { recordTracksEvent } = useAnalytics(); if ( site.site_owner !== user.ID ) { return null; } const isTrial = isSitePlanTrial( site ); return ( <> { recordTracksEvent( 'calypso_dashboard_sites_plan_renew_nag_click', { product_slug: site.plan?.product_slug, source, } ); } } > { isTrial ? __( 'Upgrade' ) : __( 'Renew plan' ) } ); } function WithHostingFeaturesQuery( { site, children, }: { site: Site; children: ( transferStatus?: AtomicTransferStatus ) => React.ReactNode; } ) { const { ref, inView } = useInView( { triggerOnce: true, fallbackInView: true, } ); const { data } = useQuery( { ...siteLatestAtomicTransferQuery( site.ID ), enabled: inView, } ); return { children( data?.status ) }; } export function Status( { site }: { site: Site } ) { const status = getSiteStatus( site ); const label = getSiteStatusLabel( site ); if ( status === 'deleted' ) { return { label }; } if ( status === 'difm_lite_in_progress' ) { return { label }; } if ( status === 'migration_pending' ) { return { label }; } if ( status === 'migration_started' ) { return { label }; } if ( site.plan?.expired ) { return ( { __( 'Plan expired' ) } ); } const renderBasicStatus = () => { if ( site.launch_status === 'unlaunched' ) { return ; } return label; }; if ( hasPlanFeature( site, DotcomFeatures.ATOMIC ) && ! site.is_wpcom_atomic ) { return ( { ( transferStatus ) => { if ( transferStatus ) { if ( transferStatus === 'error' ) { return __( 'Error activating hosting features' ); } if ( isAtomicTransferInProgress( transferStatus ) ) { return __( 'Activating hosting features…' ); } } return renderBasicStatus(); } } ); } return renderBasicStatus(); } export function Plan( { site }: { site: Site } ) { if ( site.is_wpcom_staging_site ) { // translator: this is the label of a staging site. return __( 'Staging' ); } if ( isSelfHostedJetpackConnected( site ) ) { if ( ! site.jetpack ) { return ; } return ( { site.plan?.product_name_short } ); } if ( site.plan?.expired ) { return ( { sprintf( /* translators: %s: plan name */ __( '%s-expired' ), site.plan?.product_name_short ) } ); } return site.plan?.product_name_short; }