File size: 3,777 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 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 (
<div className="host-selection">
<div className="host-selection__header">
<div className="host-selection__notice">{ introMessage }</div>
<h3>
{ translate( 'Select your website host for %(siteSlug)s', {
args: {
siteSlug,
},
} ) }
</h3>
{ providerGuessName && (
<p>
{ translate( 'It looks like your host may be %(providerGuessName)s', {
args: { providerGuessName },
} ) }
</p>
) }
</div>
<div className="host-selection__list">
{ hostsToShow.map( ( { id, name } ) => (
<a
className={
isLoading ? 'host-selection__list-item-placeholder' : 'host-selection__list-item'
}
key={ id }
href={ `${ settingsPath( siteSlug ) }?host=${ id }` }
onClick={ () => recordHostSelectionEvent( id ) }
>
<span>{ name }</span>
<div className="host-selection__list-item-badge-and-icon">
{ guess === id && (
<Badge>
{ isMobile
? translate( 'Our guess' )
: translate( 'If we had to guess your host, this would be it' ) }
</Badge>
) }
<Gridicon icon="chevron-right" />
</div>
</a>
) ) }
<a
className={
isLoading ? 'host-selection__list-item-placeholder' : 'host-selection__list-item'
}
href={ `${ settingsPath( siteSlug ) }?host=generic` }
onClick={ () => recordHostSelectionEvent( 'generic' ) }
>
{ isMobile
? translate( 'My host is not listed here' )
: translate(
'I don’t know / my host is not listed here / I have my server credentials'
) }
<Gridicon icon="chevron-right" />
</a>
</div>
</div>
);
};
export default HostSelection;
|