|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { isEnabled } from '@automattic/calypso-config'; |
|
|
import { Store } from 'redux'; |
|
|
import { isUserLoggedIn } from 'calypso/state/current-user/selectors'; |
|
|
import getSitesItems from 'calypso/state/selectors/get-sites-items'; |
|
|
import type { SiteDetails } from '@automattic/data-stores'; |
|
|
|
|
|
|
|
|
const INVALID_URL = 'https://__domain__.invalid'; |
|
|
|
|
|
type Host = string; |
|
|
|
|
|
let reduxStore: Store; |
|
|
|
|
|
export function logmeinUrl( url: string, redirectTo = '' ): string { |
|
|
|
|
|
if ( ! isEnabled( 'logmein' ) ) { |
|
|
return url; |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! reduxStore ) { |
|
|
return url; |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! isUserLoggedIn( reduxStore.getState() ) ) { |
|
|
return url; |
|
|
} |
|
|
|
|
|
const newurl = new URL( String( url ), INVALID_URL ); |
|
|
|
|
|
|
|
|
if ( newurl.origin === INVALID_URL ) { |
|
|
return url; |
|
|
} |
|
|
|
|
|
const sites = Object.values( getSitesItems( reduxStore.getState() ) ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isValid = sites.some( ( site ) => { |
|
|
return ( |
|
|
new URL( site.URL ?? '', INVALID_URL ).host === newurl.host && isValidLogmeinSite( site ) |
|
|
); |
|
|
} ); |
|
|
if ( ! isValid ) { |
|
|
return url; |
|
|
} |
|
|
|
|
|
|
|
|
newurl.searchParams.set( 'logmein', 'direct' ); |
|
|
redirectTo && newurl.searchParams.set( 'redirect_to', redirectTo ); |
|
|
|
|
|
return newurl.toString(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isValidLogmeinSite( site: SiteDetails ): boolean { |
|
|
return Boolean( |
|
|
! site.is_vip && |
|
|
! site.jetpack && |
|
|
! site.options?.is_automated_transfer && |
|
|
! site.options?.is_domain_only && |
|
|
! site.options?.is_redirect && |
|
|
! site.options?.is_wpcom_store && |
|
|
site.options?.is_mapped_domain |
|
|
); |
|
|
} |
|
|
|
|
|
export function setLogmeinReduxStore( store: Store ): void { |
|
|
reduxStore = store; |
|
|
} |
|
|
|
|
|
export function attachLogmein( store: Store ): void { |
|
|
setLogmeinReduxStore( store ); |
|
|
document.addEventListener( 'click', logmeinOnClick, false ); |
|
|
document.addEventListener( 'auxclick', logmeinOnAuxClick, false ); |
|
|
document.addEventListener( 'contextmenu', logmeinOnRightClick, false ); |
|
|
} |
|
|
|
|
|
const seen: Record< Host, boolean > = {}; |
|
|
export function logmeinOnClick( event: MouseEvent ): void { |
|
|
const link = ( event.target as HTMLElement ).closest( 'a' ); |
|
|
|
|
|
if ( link && link.href ) { |
|
|
|
|
|
const host = new URL( String( link.href ), INVALID_URL ).host; |
|
|
if ( ! seen[ host ] ) { |
|
|
link.href = logmeinUrl( link.href ); |
|
|
seen[ host ] = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
export function logmeinOnRightClick( event: MouseEvent ): void { |
|
|
const link = ( event.target as HTMLElement ).closest( 'a' ); |
|
|
|
|
|
|
|
|
|
|
|
if ( link && link.href ) { |
|
|
link.href = logmeinUrl( link.href ); |
|
|
} |
|
|
} |
|
|
|
|
|
export function logmeinOnAuxClick( event: MouseEvent ): void { |
|
|
|
|
|
|
|
|
if ( event.button === 2 ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
logmeinOnClick( event ); |
|
|
} |
|
|
|