File size: 4,730 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 |
import { WRITE_FLOW } from '@automattic/onboarding';
import { useSelect } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';
import { translate } from 'i18n-calypso';
import { useEffect } from 'react';
import { useFlowLocale } from 'calypso/landing/stepper/hooks/use-flow-locale';
import { useSite } from 'calypso/landing/stepper/hooks/use-site';
import { getStepFromURL } from 'calypso/landing/stepper/utils/get-flow-from-url';
import { skipLaunchpad } from 'calypso/landing/stepper/utils/skip-launchpad';
import { triggerGuidesForStep } from 'calypso/lib/guides/trigger-guides-for-step';
import { shouldShowLaunchpadFirst } from 'calypso/state/selectors/should-show-launchpad-first';
import { useSiteIdParam } from '../../../hooks/use-site-id-param';
import { useSiteSlug } from '../../../hooks/use-site-slug';
import { USER_STORE } from '../../../stores';
import { getLoginUrl } from '../../../utils/path';
import { STEPS } from '../../internals/steps';
import {
AssertConditionResult,
AssertConditionState,
Flow,
ProvidedDependencies,
} from '../../internals/types';
import type { UserSelect } from '@automattic/data-stores';
const WRITE_FLOW_STEPS = [ STEPS.LAUNCHPAD, STEPS.SUBSCRIBERS, STEPS.PROCESSING ];
const write: Flow = {
name: WRITE_FLOW,
get title() {
return translate( 'Write' );
},
isSignupFlow: false,
useSteps() {
return WRITE_FLOW_STEPS;
},
useTracksEventProps() {
const site = useSite();
const step = getStepFromURL();
if ( site && shouldShowLaunchpadFirst( site ) && step === 'launchpad' ) {
//prevent track events from firing until we're sure we won't redirect away from Launchpad
return {
isLoading: true,
eventsProperties: {},
};
}
return {
isLoading: false,
eventsProperties: {},
};
},
useStepNavigation( _currentStep, navigate ) {
const flowName = this.name;
const siteId = useSiteIdParam();
const siteSlug = useSiteSlug();
triggerGuidesForStep( flowName, _currentStep );
const submit = ( providedDependencies: ProvidedDependencies = {} ) => {
switch ( _currentStep ) {
case 'processing':
if ( providedDependencies?.goToHome && providedDependencies?.siteSlug ) {
return window.location.replace(
addQueryArgs( `/home/${ siteId ?? providedDependencies?.siteSlug }`, {
celebrateLaunch: true,
launchpadComplete: true,
} )
);
}
return navigate( 'launchpad' );
case 'launchpad': {
return navigate( 'processing' );
}
case 'subscribers': {
return navigate( 'launchpad' );
}
}
};
const goNext = async () => {
switch ( _currentStep ) {
case 'launchpad':
skipLaunchpad( {
siteId,
siteSlug,
} );
return;
default:
return navigate( 'freeSetup' );
}
};
const goToStep = ( step: string ) => {
navigate( step );
};
return { goNext, goToStep, submit };
},
useAssertConditions(): AssertConditionResult {
const userIsLoggedIn = useSelect(
( select ) => ( select( USER_STORE ) as UserSelect ).isCurrentUserLoggedIn(),
[]
);
let result: AssertConditionResult = { state: AssertConditionState.SUCCESS };
const queryParams = new URLSearchParams( window.location.search );
const flowName = this.name;
const locale = useFlowLocale();
const flags = queryParams.get( 'flags' );
const siteSlug = queryParams.get( 'siteSlug' );
const getStartUrl = () => {
let hasFlowParams = false;
const flowParams = new URLSearchParams();
if ( siteSlug ) {
flowParams.set( 'siteSlug', siteSlug );
hasFlowParams = true;
}
if ( locale && locale !== 'en' ) {
flowParams.set( 'locale', locale );
hasFlowParams = true;
}
const redirectTarget =
window?.location?.pathname +
( hasFlowParams ? encodeURIComponent( '?' + flowParams.toString() ) : '' );
const logInUrl = getLoginUrl( {
variationName: flowName,
redirectTo: redirectTarget,
locale,
} );
return logInUrl + ( flags ? `&flags=${ flags }` : '' );
};
// Despite sending a CHECKING state, this function gets called again with the
// /setup/write/launchpad route which has no locale in the path so we need to
// redirect off of the first render.
// This effects both /setup/write/<locale> starting points and /setup/write/launchpad/<locale> urls.
// The double call also hapens on urls without locale.
useEffect( () => {
if ( ! userIsLoggedIn ) {
const logInUrl = getStartUrl();
window.location.assign( logInUrl );
}
}, [] );
if ( ! userIsLoggedIn ) {
result = {
state: AssertConditionState.FAILURE,
message: 'write-flow requires a logged in user',
};
}
return result;
},
};
export default write;
|