|
|
import { doesStringResembleDomain } from '@automattic/onboarding'; |
|
|
import { translate } from 'i18n-calypso'; |
|
|
import { untrailingslashit } from 'calypso/lib/route'; |
|
|
import { isUserLoggedIn } from 'calypso/state/current-user/selectors'; |
|
|
import { getSelectedSite } from 'calypso/state/ui/selectors'; |
|
|
import type { Context } from '@automattic/calypso-router'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getProductSlugFromContext( context: Context ): string | undefined { |
|
|
const { params, store, pathname } = context; |
|
|
const { domainOrProduct, product, productSlug } = params; |
|
|
const state = store.getState(); |
|
|
const selectedSite = getSelectedSite( state ); |
|
|
const isGiftPurchase = pathname.includes( '/gift/' ); |
|
|
|
|
|
|
|
|
if ( isContextJetpackSitelessCheckout( context ) ) { |
|
|
return productSlug; |
|
|
} |
|
|
|
|
|
|
|
|
if ( isGiftPurchase ) { |
|
|
return product; |
|
|
} |
|
|
|
|
|
if ( ! domainOrProduct && ! product ) { |
|
|
return ''; |
|
|
} |
|
|
|
|
|
|
|
|
if ( domainOrProduct && selectedSite?.slug && selectedSite.slug === domainOrProduct ) { |
|
|
return product || ''; |
|
|
} |
|
|
|
|
|
if ( product && selectedSite?.slug && selectedSite.slug === product ) { |
|
|
return domainOrProduct || ''; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! selectedSite?.slug ) { |
|
|
return domainOrProduct || ''; |
|
|
} |
|
|
|
|
|
|
|
|
if ( domainOrProduct ) { |
|
|
const isDomain = doesStringResembleDomain( domainOrProduct ); |
|
|
if ( isDomain && product ) { |
|
|
return product; |
|
|
} |
|
|
if ( ! isDomain ) { |
|
|
return domainOrProduct; |
|
|
} |
|
|
} |
|
|
|
|
|
if ( product ) { |
|
|
const isDomain = doesStringResembleDomain( product ); |
|
|
if ( isDomain && domainOrProduct ) { |
|
|
return domainOrProduct; |
|
|
} |
|
|
if ( ! isDomain ) { |
|
|
return product; |
|
|
} |
|
|
} |
|
|
|
|
|
return ''; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function addHttpIfMissing( inputUrl: string, httpsIsDefault = true ): string { |
|
|
const scheme = httpsIsDefault ? 'https' : 'http'; |
|
|
let url = inputUrl.trim().toLowerCase(); |
|
|
|
|
|
if ( url && url.substr( 0, 4 ) !== 'http' ) { |
|
|
url = `${ scheme }://` + url; |
|
|
} |
|
|
return untrailingslashit( url ); |
|
|
} |
|
|
|
|
|
export function isContextJetpackSitelessCheckout( context: Context ): boolean { |
|
|
const hasJetpackPurchaseToken = Boolean( context.query.purchasetoken ); |
|
|
const hasJetpackPurchaseNonce = Boolean( context.query.purchaseNonce ); |
|
|
const isUserComingFromLoginForm = context.query?.flow === 'coming_from_login'; |
|
|
const isUserComingFromPlansPage = [ 'jetpack-plans', 'jetpack-connect-plans' ].includes( |
|
|
context.query?.source |
|
|
); |
|
|
const state = context.store.getState(); |
|
|
const isLoggedOut = ! isUserLoggedIn( state ); |
|
|
|
|
|
if ( ! context.pathname.includes( '/checkout/jetpack' ) ) { |
|
|
return false; |
|
|
} |
|
|
if ( ! isLoggedOut && ! isUserComingFromLoginForm && ! isUserComingFromPlansPage ) { |
|
|
return false; |
|
|
} |
|
|
if ( ! hasJetpackPurchaseToken && ! hasJetpackPurchaseNonce ) { |
|
|
return false; |
|
|
} |
|
|
return true; |
|
|
} |
|
|
|
|
|
export function isContextSourceMyJetpack( context: Context ): boolean { |
|
|
return context.query?.source === 'my-jetpack'; |
|
|
} |
|
|
|
|
|
export function getAffiliateCouponLabel(): string { |
|
|
|
|
|
return translate( 'Exclusive Offer Applied' ); |
|
|
} |
|
|
|