import { useQuery, useMutation } from '@tanstack/react-query'; import { __experimentalHStack as HStack, Button, Dropdown, MenuGroup, MenuItem, Spinner, } from '@wordpress/components'; import { useDispatch } from '@wordpress/data'; import { sprintf, __ } from '@wordpress/i18n'; import { Icon, chevronDownSmall, plus } from '@wordpress/icons'; import { store as noticesStore } from '@wordpress/notices'; import { siteByIdQuery } from '../../app/queries/site'; import { stagingSiteCreateMutation } from '../../app/queries/site-staging-sites'; import { production, staging } from '../../components/icons'; import RouterLinkMenuItem from '../../components/router-link-menu-item'; import { canManageSite, canCreateStagingSite } from '../features'; import type { Site } from '../../data/types'; type EnvironmentType = 'production' | 'staging'; const Environment = ( { env }: { env: EnvironmentType } ) => { if ( env === 'staging' ) { return ( { __( 'Staging' ) } ); } return ( { __( 'Production' ) } ); }; const CurrentEnvironment = ( { site }: { site: Site } ) => { if ( site.is_wpcom_staging_site ) { return ; } return ; }; const EnvironmentSwitcherDropdown = ( { currentSite, otherEnvironment, otherEnvironmentSite, onClose, }: { currentSite: Site; otherEnvironment: EnvironmentType; otherEnvironmentSite?: Site; onClose: () => void; } ) => { const productionSite = otherEnvironment === 'staging' ? currentSite : otherEnvironmentSite; const stagingSite = otherEnvironment === 'staging' ? otherEnvironmentSite : currentSite; const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore ); const mutation = useMutation( stagingSiteCreateMutation( productionSite?.ID ?? 0 ) ); const handleCreate = () => { mutation.mutate( undefined, { onSuccess: () => { createSuccessNotice( __( 'Staging site created.' ), { type: 'snackbar' } ); }, onError: ( error: Error ) => { createErrorNotice( sprintf( // translators: "reason" is why adding the staging site failed. __( 'Failed to create staging site: %(reason)s' ), { reason: error.message } ), { type: 'snackbar', } ); }, } ); }; // TODO: Handle upsell. const handleUpsell = () => {}; return ( { productionSite && canManageSite( productionSite ) && ( ) } { stagingSite && canManageSite( stagingSite ) && ( ) } { otherEnvironment === 'staging' && productionSite && ! stagingSite && ( { mutation.isPending ? ( <> { __( 'Creating staging site…' ) } ) : ( <> { __( 'Add staging site' ) } ) } ) } ); }; const EnvironmentSwitcher = ( { site }: { site: Site } ) => { const hasStagingSite = ! site.is_wpcom_staging_site && site?.options?.wpcom_staging_blog_ids?.length; const otherEnvironment = site.is_wpcom_staging_site ? 'production' : 'staging'; const otherEnvironmentSiteId = site.is_wpcom_staging_site ? site.options?.wpcom_production_blog_id : site.options?.wpcom_staging_blog_ids?.[ 0 ]; const { data: otherEnvironmentSite } = useQuery( { ...siteByIdQuery( otherEnvironmentSiteId ?? 0 ), enabled: !! otherEnvironmentSiteId, } ); return ( { const canToggle = ( otherEnvironment === 'staging' && ! hasStagingSite ) || ( otherEnvironmentSite && canManageSite( otherEnvironmentSite ) ); return ( ); } } renderContent={ ( { onClose } ) => ( ) } /> ); }; export default EnvironmentSwitcher;