import { Gridicon, Badge } from '@automattic/components'; import { useMobileBreakpoint } from '@automattic/viewport-react'; import { useQuery } from '@tanstack/react-query'; import { useTranslate } from 'i18n-calypso'; import { FunctionComponent, useMemo } from 'react'; import { settingsPath } from 'calypso/lib/jetpack/paths'; import wpcom from 'calypso/lib/wp'; import { useDispatch, useSelector } from 'calypso/state'; import { recordTracksEvent } from 'calypso/state/analytics/actions'; import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors'; import { getProviderNameFromId, topHosts, otherHosts } from '../host-info'; import useCredentialsIntroMessage from '../use-credentials-intro-message'; import type { SiteId } from 'calypso/types'; import './style.scss'; interface Guess { guess: string; } function useHostingProviderGuessQuery( siteId: SiteId ) { return useQuery( { queryKey: [ 'site-hosting-provider-guess', siteId ], queryFn: (): Promise< Guess > => wpcom.req.get( { path: `/sites/${ siteId }/hosting-provider`, apiNamespace: 'wpcom/v2', } ), enabled: !! siteId, meta: { persist: false }, select: ( data ) => data.guess, } ); } const HostSelection: FunctionComponent = () => { const isMobile = useMobileBreakpoint(); const siteId = useSelector( getSelectedSiteId ) as SiteId; const siteSlug = useSelector( getSelectedSiteSlug ) as string; const translate = useTranslate(); const dispatch = useDispatch(); const { isLoading, data: guess } = useHostingProviderGuessQuery( siteId ); const providerGuessName = getProviderNameFromId( guess ); const hostsToShow = useMemo( () => { for ( const host of otherHosts ) { if ( guess === host.id ) { return [ host, ...topHosts ]; } } return topHosts; }, [ guess ] ); const recordHostSelectionEvent = ( host: string ) => { dispatch( recordTracksEvent( 'calypso_jetpack_advanced_credentials_flow_host_select', { host, } ) ); }; const introMessage = useCredentialsIntroMessage( siteId ); return (
{ translate( 'It looks like your host may be %(providerGuessName)s', { args: { providerGuessName }, } ) }
) }