|
|
import { useRef } from 'react'; |
|
|
import { shouldUseStepContainerV2 } from 'calypso/landing/stepper/declarative-flow/helpers/should-use-step-container-v2'; |
|
|
import { DEFAULT_FLOW, getFlowFromURL } from 'calypso/landing/stepper/utils/get-flow-from-url'; |
|
|
import { isWpMobileApp } from 'calypso/lib/mobile-app'; |
|
|
|
|
|
let lastScrollPosition = 0; |
|
|
let sidebarTop = 0; |
|
|
let pinnedSidebarTop = true; |
|
|
let pinnedSidebarBottom = false; |
|
|
let ticking = false; |
|
|
|
|
|
export function shouldLoadInlineHelp( sectionName: string, currentRoute: string ) { |
|
|
if ( isWpMobileApp() ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
const exemptedSections = [ 'jetpack-connect', 'devdocs', 'help', 'home' ]; |
|
|
const exemptedRoutes = [ '/log-in/jetpack' ]; |
|
|
const exemptedRoutesStartingWith = [ |
|
|
'/start/p2', |
|
|
'/start/setup-site', |
|
|
'/start/newsletter', |
|
|
'/plugins/domain', |
|
|
'/plugins/marketplace/setup', |
|
|
]; |
|
|
|
|
|
return ( |
|
|
! exemptedSections.includes( sectionName ) && |
|
|
! exemptedRoutes.includes( currentRoute ) && |
|
|
! exemptedRoutesStartingWith.some( ( startsWithString ) => |
|
|
currentRoute.startsWith( startsWithString ) |
|
|
) |
|
|
); |
|
|
} |
|
|
|
|
|
export const handleScroll = ( event: React.UIEvent< HTMLElement > ): void => { |
|
|
|
|
|
if ( ticking ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const windowHeight = window.innerHeight; |
|
|
const contentEl = document.getElementById( 'content' ); |
|
|
const contentHeight = contentEl?.scrollHeight; |
|
|
const secondaryEl = document.getElementById( 'secondary' ); |
|
|
const secondaryElHeight = secondaryEl?.scrollHeight; |
|
|
const masterbarHeight = document.getElementById( 'header' )?.getBoundingClientRect().height; |
|
|
|
|
|
|
|
|
|
|
|
if ( contentEl && contentHeight && masterbarHeight && secondaryElHeight ) { |
|
|
if ( contentHeight < secondaryElHeight ) { |
|
|
|
|
|
contentEl.style.minHeight = secondaryElHeight + masterbarHeight + 'px'; |
|
|
} |
|
|
|
|
|
if ( |
|
|
windowHeight >= secondaryElHeight + masterbarHeight && |
|
|
contentEl && |
|
|
secondaryEl && |
|
|
( contentEl.style.minHeight !== 'initial' || secondaryEl.style.position ) |
|
|
) { |
|
|
|
|
|
contentEl.style.minHeight = 'initial'; |
|
|
|
|
|
secondaryEl.removeAttribute( 'style' ); |
|
|
} |
|
|
} |
|
|
|
|
|
if ( |
|
|
secondaryEl && |
|
|
secondaryElHeight !== undefined && |
|
|
masterbarHeight !== undefined && |
|
|
( secondaryElHeight + masterbarHeight > windowHeight || 'resize' === event.type ) |
|
|
) { |
|
|
|
|
|
window.requestAnimationFrame( function () { |
|
|
const maxScroll = secondaryElHeight + masterbarHeight - windowHeight; |
|
|
const scrollY = -document.body.getBoundingClientRect().top; |
|
|
|
|
|
|
|
|
if ( scrollY < 0 ) { |
|
|
|
|
|
if ( ! pinnedSidebarTop ) { |
|
|
pinnedSidebarTop = true; |
|
|
pinnedSidebarBottom = false; |
|
|
secondaryEl.style.position = 'fixed'; |
|
|
secondaryEl.style.top = '0'; |
|
|
secondaryEl.style.bottom = '0'; |
|
|
} |
|
|
|
|
|
ticking = false; |
|
|
return; |
|
|
} else if ( scrollY + windowHeight > document.body.scrollHeight - 1 ) { |
|
|
|
|
|
if ( ! pinnedSidebarBottom ) { |
|
|
pinnedSidebarBottom = true; |
|
|
pinnedSidebarTop = false; |
|
|
|
|
|
secondaryEl.style.position = 'fixed'; |
|
|
secondaryEl.style.top = 'inherit'; |
|
|
secondaryEl.style.bottom = '0'; |
|
|
} |
|
|
|
|
|
ticking = false; |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( scrollY >= lastScrollPosition ) { |
|
|
|
|
|
|
|
|
if ( pinnedSidebarTop ) { |
|
|
pinnedSidebarTop = false; |
|
|
sidebarTop = masterbarHeight; |
|
|
|
|
|
if ( scrollY > maxScroll ) { |
|
|
|
|
|
sidebarTop += scrollY; |
|
|
} |
|
|
|
|
|
secondaryEl.style.position = 'absolute'; |
|
|
secondaryEl.style.top = `${ sidebarTop }px`; |
|
|
secondaryEl.style.bottom = 'inherit'; |
|
|
} else if ( |
|
|
! pinnedSidebarBottom && |
|
|
scrollY + masterbarHeight > maxScroll + secondaryEl.offsetTop |
|
|
) { |
|
|
|
|
|
pinnedSidebarBottom = true; |
|
|
|
|
|
secondaryEl.style.position = 'fixed'; |
|
|
secondaryEl.style.top = 'inherit'; |
|
|
secondaryEl.style.bottom = '0'; |
|
|
} |
|
|
} else if ( scrollY < lastScrollPosition ) { |
|
|
|
|
|
|
|
|
|
|
|
if ( pinnedSidebarBottom ) { |
|
|
pinnedSidebarBottom = false; |
|
|
|
|
|
|
|
|
sidebarTop = Math.max( 0, scrollY + masterbarHeight - maxScroll ); |
|
|
if ( contentHeight === secondaryElHeight + masterbarHeight ) { |
|
|
|
|
|
|
|
|
|
|
|
sidebarTop = masterbarHeight; |
|
|
} |
|
|
|
|
|
secondaryEl.style.position = 'absolute'; |
|
|
secondaryEl.style.top = `${ sidebarTop }px`; |
|
|
secondaryEl.style.bottom = 'inherit'; |
|
|
} else if ( ! pinnedSidebarTop && scrollY + masterbarHeight < sidebarTop ) { |
|
|
|
|
|
pinnedSidebarTop = true; |
|
|
sidebarTop = masterbarHeight; |
|
|
|
|
|
secondaryEl.style.position = 'fixed'; |
|
|
secondaryEl.style.top = `${ sidebarTop }px`; |
|
|
secondaryEl.style.bottom = 'inherit'; |
|
|
} |
|
|
} |
|
|
|
|
|
lastScrollPosition = scrollY; |
|
|
|
|
|
ticking = false; |
|
|
} ); |
|
|
ticking = true; |
|
|
} |
|
|
}; |
|
|
|
|
|
const isRedirectingToStepContainerV2Flow = ( redirectTo: string ) => { |
|
|
const { pathname, search } = new URL( redirectTo, 'http://example.com' ); |
|
|
|
|
|
return shouldUseStepContainerV2( getFlowFromURL( pathname, search ) ); |
|
|
}; |
|
|
|
|
|
const isMarketplaceThankYouRedirect = ( redirectTo: string ) => { |
|
|
const { pathname, searchParams } = new URL( redirectTo, 'http://example.com' ); |
|
|
|
|
|
return pathname.startsWith( '/marketplace' ) && searchParams.has( 'onboarding' ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isInStepContainerV2FlowContext = ( pathname: string, query: string ) => { |
|
|
if ( pathname.startsWith( '/setup' ) ) { |
|
|
return shouldUseStepContainerV2( getFlowFromURL( pathname, query ) || DEFAULT_FLOW ); |
|
|
} |
|
|
|
|
|
if ( pathname.startsWith( '/checkout' ) ) { |
|
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams( query ); |
|
|
const redirectTo = params.get( 'redirect_to' ) ?? ''; |
|
|
const cancelTo = params.get( 'cancel_to' ) ?? ''; |
|
|
|
|
|
if ( isRedirectingToStepContainerV2Flow( redirectTo ) ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
if ( isRedirectingToStepContainerV2Flow( cancelTo ) ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
if ( isMarketplaceThankYouRedirect( redirectTo ) ) { |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
if ( pathname.startsWith( '/marketplace' ) ) { |
|
|
const params = new URLSearchParams( query ); |
|
|
|
|
|
if ( params.has( 'onboarding' ) ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
const redirectTo = params.get( 'redirect_to' ) ?? ''; |
|
|
|
|
|
return isMarketplaceThankYouRedirect( redirectTo ); |
|
|
} |
|
|
|
|
|
return false; |
|
|
}; |
|
|
|
|
|
export const useInitialIsInStepContainerV2FlowContext = () => { |
|
|
const ref = useRef( |
|
|
isInStepContainerV2FlowContext( window.location.pathname, window.location.search ) |
|
|
); |
|
|
|
|
|
return ref.current; |
|
|
}; |
|
|
|