File size: 4,870 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { Title } from '@automattic/onboarding';
import { useEffect } from '@wordpress/element';
import { useTranslate } from 'i18n-calypso';
import React, { useRef, useState } from 'react';
import { useAnalyzeUrlQuery } from 'calypso/data/site-profiler/use-analyze-url-query';
import { isSupportedImporterEngine } from 'calypso/lib/importer/importer-config';
import { triggerMigrationStartingEvent } from 'calypso/my-sites/migrate/helpers';
import { useSelector } from 'calypso/state';
import { getCurrentUser, getCurrentUserCountryCode } from 'calypso/state/current-user/selectors';
import ScanningStep from '../scanning';
import { GoToStep } from '../types';
import CaptureInput from './capture-input';
import type { OnInputEnter, OnInputChange } from './types';
import type { FunctionComponent } from 'react';
import './style.scss';
interface Props {
hasError?: boolean;
onInputEnter: OnInputEnter;
onInputChange?: OnInputChange;
onDontHaveSiteAddressClick?: () => void;
}
export const Capture: FunctionComponent< Props > = ( props ) => {
const translate = useTranslate();
const { onInputEnter, onInputChange, onDontHaveSiteAddressClick, hasError } = props;
return (
<>
<div className="import__heading import__heading-center">
<Title>{ translate( 'Where will you import from?' ) }</Title>
</div>
<div className="import__capture-container">
<CaptureInput
onInputEnter={ onInputEnter }
onInputChange={ onInputChange }
onDontHaveSiteAddressClick={ onDontHaveSiteAddressClick }
hasError={ hasError }
/>
</div>
</>
);
};
type StepProps = {
initialUrl?: string;
goToStep: GoToStep;
onValidFormSubmit?: ( dependencies: Record< string, unknown > ) => void;
onImportListClick?: () => void;
};
const trackEventName = 'calypso_signup_step_start';
const trackEventParams = {
flow: 'importer',
step: 'capture',
};
export const CaptureStep: React.FunctionComponent< StepProps > = ( {
initialUrl = '',
goToStep,
onValidFormSubmit,
onImportListClick,
} ) => {
const currentUser = useSelector( getCurrentUser );
const detectedCountryCode = useSelector( getCurrentUserCountryCode );
const isStartingPointEventTriggeredRef = useRef( false );
const [ url, setUrl ] = useState( initialUrl );
const {
data: urlData,
isFetching: isAnalyzing,
error: analyzerError,
isFetchedAfterMount,
} = useAnalyzeUrlQuery( url );
const showCapture = ! isAnalyzing || ( initialUrl && isFetchedAfterMount );
useEffect( () => {
if ( window && window.hj ) {
window.hj( 'trigger', 'importer_capture_step_2' );
}
}, [ detectedCountryCode ] );
const decideStepRedirect = () => {
if ( ! urlData ) {
return;
}
const stepName = 'ready';
let stepSectionName;
switch ( urlData.platform ) {
case 'unknown':
stepSectionName = 'not';
break;
case 'wordpress':
stepSectionName = urlData.platform_data?.is_wpcom ? 'wpcom' : 'preview';
break;
default:
if ( ! isSupportedImporterEngine( urlData.platform ) ) {
stepSectionName = 'not';
} else {
stepSectionName = 'preview';
}
break;
}
goToStep( stepName, stepSectionName, { fromUrl: url } );
};
const recordScanningEvent = () => {
if ( ! isAnalyzing ) {
return;
}
recordTracksEvent( trackEventName, {
...trackEventParams,
action: 'scanning',
} );
};
const recordScanningErrorEvent = () => {
if ( ! analyzerError ) {
return;
}
recordTracksEvent( trackEventName, {
...trackEventParams,
action: 'scanning-error',
error: JSON.stringify( analyzerError ),
} );
};
const recordCaptureScreen = () => {
recordTracksEvent( trackEventName, trackEventParams );
};
const recordMigrationStartingPointEvent = () => {
if ( currentUser && currentUser.ID && ! isStartingPointEventTriggeredRef.current ) {
isStartingPointEventTriggeredRef.current = true;
triggerMigrationStartingEvent( currentUser );
}
};
const onDontHaveSiteAddressClick = () => {
onImportListClick ? onImportListClick() : goToStep( 'list' );
};
/**
↓ Effects
*/
useEffect( recordScanningEvent, [ isAnalyzing ] );
useEffect( recordScanningErrorEvent, [ analyzerError ] );
useEffect( recordMigrationStartingPointEvent, [ currentUser ] );
useEffect( recordCaptureScreen, [] );
useEffect( () => decideStepRedirect(), [ urlData ] );
return (
<>
{ showCapture && (
<Capture
onInputEnter={ ( url ) => {
onValidFormSubmit ? onValidFormSubmit( { url } ) : setUrl( url );
} }
onDontHaveSiteAddressClick={ onDontHaveSiteAddressClick }
hasError={ !! analyzerError }
onInputChange={ () => {
// resets the error when the user starts typing again
setUrl( '' );
} }
/>
) }
{ isAnalyzing && <ScanningStep /> }
</>
);
};
export default CaptureStep;
|