| 1 |
import { createDomainUrlLink } from '@assist/lib/domains'; |
| 2 |
import { safeParseJson } from '@shared/lib/parsing'; |
| 3 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 4 |
|
| 5 |
export const REGISTER_DOMAIN_ID = 'register-domain'; |
| 6 |
|
| 7 |
const { hostname } = window.location; |
| 8 |
const { devbuild, siteTitle, wpLanguage } = window.extSharedData; |
| 9 |
const { showPrimary, showSecondary, stagingSites, searchUrl } = |
| 10 |
window.extAgentData?.domainsSuggestionSettings || {}; |
| 11 |
|
| 12 |
const parsedDomains = safeParseJson(window.extSharedData.resourceData)?.domains; |
| 13 |
const domains = Array.isArray(parsedDomains) ? parsedDomains : []; |
| 14 |
export const domain = domains[0] || ''; |
| 15 |
|
| 16 |
const isStagingDomain = (stagingSites || []).some((l) => |
| 17 |
hostname.toLowerCase().includes(l), |
| 18 |
); |
| 19 |
|
| 20 |
const domainByLanguage = (lang, urlList) => { |
| 21 |
try { |
| 22 |
const urls = JSON.parse(decodeEntities(urlList)); |
| 23 |
return urls?.[lang] ?? urls?.default ?? false; |
| 24 |
} catch (_e) { |
| 25 |
return decodeEntities(urlList) || false; |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
const domainSearchUrl = |
| 30 |
devbuild && !searchUrl |
| 31 |
? 'https://extendify.com?s={DOMAIN}' |
| 32 |
: domainByLanguage(wpLanguage, searchUrl); |
| 33 |
|
| 34 |
const canRecommendDomain = (enabled, requireStaging) => { |
| 35 |
if (devbuild) return true; |
| 36 |
if (!enabled) return false; |
| 37 |
if (!domain) return false; |
| 38 |
if (!siteTitle) return false; |
| 39 |
return requireStaging ? isStagingDomain : !isStagingDomain; |
| 40 |
}; |
| 41 |
|
| 42 |
const showPrimaryDomainRecommendationAgent = canRecommendDomain( |
| 43 |
showPrimary, |
| 44 |
true, |
| 45 |
); |
| 46 |
const showSecondaryDomainRecommendationAgent = canRecommendDomain( |
| 47 |
showSecondary, |
| 48 |
false, |
| 49 |
); |
| 50 |
|
| 51 |
export const domainType = showPrimaryDomainRecommendationAgent |
| 52 |
? 'primary' |
| 53 |
: 'secondary'; |
| 54 |
|
| 55 |
export const isDomainRegistrationActive = |
| 56 |
(showPrimaryDomainRecommendationAgent || |
| 57 |
showSecondaryDomainRecommendationAgent) && |
| 58 |
Boolean(domain) && |
| 59 |
Boolean(domainSearchUrl); |
| 60 |
|
| 61 |
const rawSuggestion = (window.extAgentData?.suggestions || []).find( |
| 62 |
(s) => s.id === REGISTER_DOMAIN_ID, |
| 63 |
); |
| 64 |
export const REGISTER_DOMAIN_MESSAGE = |
| 65 |
isDomainRegistrationActive && typeof rawSuggestion?.message === 'string' |
| 66 |
? rawSuggestion.message.replace(/\{\{domain\}\}/g, domain) |
| 67 |
: ''; |
| 68 |
|
| 69 |
export const enhanceDomainSuggestion = (suggestion) => { |
| 70 |
if (suggestion?.id !== REGISTER_DOMAIN_ID) return suggestion; |
| 71 |
if (!isDomainRegistrationActive) return null; |
| 72 |
return { |
| 73 |
...suggestion, |
| 74 |
// The BE marks this as an external link; fall back in case it hasn't yet. |
| 75 |
type: suggestion.type ?? 'external-link', |
| 76 |
message: REGISTER_DOMAIN_MESSAGE, |
| 77 |
url: createDomainUrlLink(domainSearchUrl, domain), |
| 78 |
tracking: { domain, position: 'agent-suggestion', type: domainType }, |
| 79 |
}; |
| 80 |
}; |
| 81 |
|