File size: 2,878 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 |
import { useDebounce } from 'use-debounce';
import { useIsWpOrgSite } from './use-is-wporg-site';
import { useUserSites } from './use-user-sites';
import { useWpcomSite } from './use-wpcom-site';
import type { AnalysisReport } from '../types';
// a simple way to check if a string is host to save on API calls
function isHost( string: string | undefined ) {
if ( string ) {
return string.length > 4 && Boolean( string?.match( /\w{2,}\.\w{2,32}/ ) );
}
return false;
}
function urlMatches( trustedURL: string, userInputUrl: string | undefined ) {
if ( ! trustedURL || ! userInputUrl ) {
return false;
}
const normalizedInputUrl = userInputUrl.trim().toLowerCase();
if ( trustedURL === normalizedInputUrl ) {
return true;
}
try {
const trustedURLObject = new URL( trustedURL );
if ( trustedURLObject.host === normalizedInputUrl ) {
return true;
}
const normalizedInputUrlObject = new URL( normalizedInputUrl );
if ( trustedURLObject.host === normalizedInputUrlObject.host ) {
return true;
}
} catch ( _e ) {
// couldn't build URL object
return false;
}
}
/**
* Analyses a site to determine whether its a WPCOM site, and if yes, it would fetch and return the site information (SiteDetails).
* @param userId the user ID
* @param siteURL the site URL
* @param enabled whether the query is enabled
*/
export function useSiteAnalysis(
userId: number | string,
siteURL: string | undefined,
enabled: boolean
): AnalysisReport {
const [ debouncedSiteUrl ] = useDebounce( siteURL, 500 );
const isEnabled = isHost( debouncedSiteUrl ) && enabled;
const { data: userSites, isLoading: userSitesLoading } = useUserSites( userId, isEnabled );
const { data: wpcomSite, isLoading: wpcomSiteLoading } = useWpcomSite(
debouncedSiteUrl,
isEnabled
);
const { data: isWporg, isLoading: wpOrgSiteLoading } = useIsWpOrgSite(
debouncedSiteUrl,
isEnabled
);
if ( ! isEnabled ) {
return {
result: 'DISABLED',
siteURL,
isWpcom: false,
};
}
const usersOwned = Boolean(
userSites?.sites.find( ( s ) => urlMatches( s.URL, debouncedSiteUrl ) )
);
if ( usersOwned ) {
return { site: wpcomSite, result: 'OWNED_BY_USER', siteURL, isWpcom: true };
} else if ( wpcomSite ) {
// use the wpcomSite response URL instead of user input to
// double check if the wpcom site belongs to the user before dismissing
if ( userSites?.sites.find( ( s ) => urlMatches( s.URL, wpcomSite.URL ) ) ) {
return { site: wpcomSite, result: 'OWNED_BY_USER', siteURL, isWpcom: true };
}
return { site: wpcomSite, result: 'NOT_OWNED_BY_USER', siteURL, isWpcom: true };
} else if ( isWporg ) {
return { result: 'WPORG', siteURL, isWpcom: false };
}
const isLoading = [ userSitesLoading, wpOrgSiteLoading, wpcomSiteLoading ].some( Boolean );
return {
result: isLoading ? 'LOADING' : 'UNKNOWN',
siteURL,
isWpcom: false,
};
}
|