import { Button } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import {
SETTING_PRIMARY_DOMAIN,
INCOMING_DOMAIN_TRANSFER_STATUSES_IN_PROGRESS,
GDPR_POLICIES,
DOMAIN_EXPIRATION_AUCTION,
} from '@automattic/urls';
import moment from 'moment';
import { useMyDomainInputMode } from 'calypso/components/domains/connect-domain-step/constants';
import { isExpiringSoon } from 'calypso/lib/domains/utils/is-expiring-soon';
import { isRecentlyRegistered } from 'calypso/lib/domains/utils/is-recently-registered';
import { shouldRenderExpiringCreditCard, handleRenewNowClick } from 'calypso/lib/purchases';
import {
domainManagementEdit,
domainManagementEditContactInfo,
domainMappingSetup,
domainUseMyDomain,
} from 'calypso/my-sites/domains/paths';
import { transferStatus, type as domainTypes, gdprConsentStatus } from './constants';
import type { ResponseDomain } from './types';
import type { Purchase } from 'calypso/lib/purchases/types';
import type { CalypsoDispatch } from 'calypso/state/types';
import type { I18N, TranslateResult } from 'i18n-calypso';
type ResolveDomainStatusReturn =
| {
statusText: TranslateResult | TranslateResult[];
statusClass:
| 'status-error'
| 'status-warning'
| 'status-alert'
| 'status-success'
| 'status-neutral'
| 'status-premium';
status: TranslateResult;
icon: 'info' | 'verifying' | 'check_circle' | 'cached' | 'cloud_upload' | 'download_done';
listStatusWeight?: number;
noticeText?: TranslateResult | Array< TranslateResult > | null;
isDismissable?: boolean;
}
| Record< string, never >;
export type ResolveDomainStatusOptionsBag = {
isJetpackSite?: boolean | null;
isSiteAutomatedTransfer?: boolean | null;
isDomainOnlySite?: boolean | null;
siteSlug?: string | null;
currentRoute?: string | null;
getMappingErrors?: boolean | null;
dismissPreferences?: any;
isVipSite?: boolean | null;
};
export function resolveDomainStatus(
domain: ResponseDomain,
purchase: Purchase | null = null,
translate: I18N[ 'translate' ],
dispatch: CalypsoDispatch,
{
isJetpackSite = null,
isSiteAutomatedTransfer = null,
isDomainOnlySite = null,
siteSlug = null,
getMappingErrors = false,
currentRoute = null,
dismissPreferences = null,
isVipSite = false,
}: ResolveDomainStatusOptionsBag = {}
): ResolveDomainStatusReturn {
const transferOptions = {
components: {
strong: ,
a: (
),
},
args: {
transferFinishDate: moment.utc( domain.transferEndDate ).format( 'LL' ),
},
};
const mappingSetupComponents = {
strong: ,
a: ,
};
switch ( domain.type ) {
case domainTypes.MAPPED:
if ( isExpiringSoon( domain, 30 ) ) {
const expiresMessage =
null !== domain.bundledPlanSubscriptionId
? translate(
'Domain connection expires with your plan on {{strong}}%(expiryDate)s{{/strong}}',
{
args: { expiryDate: moment.utc( domain.expiry ).format( 'LL' ) },
components: { strong: },
}
)
: translate( 'Domain connection expires in {{strong}}%(days)s{{/strong}}', {
args: { days: moment.utc( domain.expiry ).fromNow( true ) },
components: { strong: },
} );
let noticeText = null;
if ( ! isVipSite && ! domain.pointsToWpcom ) {
noticeText = translate(
"We noticed that something wasn't updated correctly. Please try {{a}}this setup{{/a}} again.",
{ components: mappingSetupComponents }
);
}
let status = translate( 'Active' );
if ( ! domain.autoRenewing ) {
status = domain.bundledPlanSubscriptionId
? translate( 'Expires with your plan' )
: translate( 'Expiring soon' );
}
return {
statusText: expiresMessage,
statusClass: `status-${ domain.autoRenewing ? 'success' : 'error' }`,
status: status,
icon: 'info',
listStatusWeight: isExpiringSoon( domain, 7 ) ? 1000 : 800,
noticeText,
};
}
if ( getMappingErrors && siteSlug !== null && ! isVipSite ) {
const registrationDatePlus3Days = moment.utc( domain.registrationDate ).add( 3, 'days' );
const hasMappingError =
domain.type === domainTypes.MAPPED &&
! domain.pointsToWpcom &&
moment.utc().isAfter( registrationDatePlus3Days );
if ( hasMappingError ) {
return {
statusText: translate( 'Connection error' ),
statusClass: 'status-alert',
status: translate( 'Error' ),
icon: 'info',
noticeText: translate(
"We noticed that something wasn't updated correctly. Please try {{a}}this setup{{/a}} again.",
{ components: mappingSetupComponents }
),
listStatusWeight: 1000,
};
}
}
if (
! isVipSite &&
( ! isJetpackSite || isSiteAutomatedTransfer ) &&
! domain.pointsToWpcom
) {
return {
statusText: translate( 'Verifying' ),
statusClass: 'status-success',
status: translate( 'Verifying' ),
icon: 'verifying',
noticeText: translate(
'It can take between a few minutes to 72 hours to verify the connection. You can continue to work on your site, but {{strong}}%(domainName)s{{/strong}} won’t be reachable just yet. You can review the {{a}}setup instructions{{/a}} to ensure everything is correct.',
{
components: mappingSetupComponents,
args: {
domainName: domain.name,
},
}
),
listStatusWeight: 600,
};
}
return {
statusText: translate( 'Active' ),
statusClass: 'status-success',
status: translate( 'Active' ),
icon: 'check_circle',
};
case domainTypes.REGISTERED:
if ( domain.isMoveToNewSitePending ) {
return {
statusText: translate( 'Pending' ),
statusClass: 'status-warning',
status: translate( 'Pending' ),
icon: 'info',
noticeText: translate(
"This domain is being disconnected. It should be updated within a few minutes. Once the disconnect is complete, you'll be able to manage it {{a}}here{{/a}}.",
{
components: {
a: (
),
},
}
),
listStatusWeight: 400,
};
}
if ( domain.aftermarketAuction ) {
const statusMessage = translate( 'Expired', { context: 'domain status' } );
return {
statusText: statusMessage,
statusClass: 'status-warning',
status: statusMessage,
icon: 'info',
noticeText: translate(
'This domain expired more than 30 days ago and is no longer available to manage or renew. We may be able to restore it after {{strong}}%(aftermarketAuctionEnd)s{{/strong}}. {{a}}Learn more{{/a}}',
{
components: {
strong: ,
a: (
),
},
args: {
aftermarketAuctionEnd: moment.utc( domain.aftermarketAuctionEnd ).format( 'LL' ),
},
}
),
listStatusWeight: 400,
};
}
if ( domain.isPendingRenewal ) {
const pendingRenewalMessage = translate( 'Renewal in progress' );
return {
statusText: pendingRenewalMessage,
statusClass: 'status-success',
status: translate( 'Renewing' ),
icon: 'info',
noticeText: translate( 'Attempting to get it renewed for you.' ),
listStatusWeight: 400,
};
}
if ( domain.pendingTransfer ) {
return {
statusText: translate( 'Outbound transfer initiated' ),
statusClass: 'status-success',
status: translate( 'In progress' ),
icon: 'cached',
};
}
if ( domain.isPendingIcannVerification && domain.isIcannVerificationSuspended ) {
return {
statusText: translate( 'Suspended' ),
statusClass: 'status-error',
status: translate( 'Suspended' ),
icon: 'info',
listStatusWeight: 800,
};
}
if ( domain.isPendingIcannVerification ) {
const noticeText = domain.currentUserIsOwner
? translate(
'We sent you an email to verify your contact information. Please complete the verification or your domain will stop working. You can also {{a}}change your email address{{/a}} if you like.',
{
components: {
a: (
),
},
args: {
domainName: domain.name,
},
}
)
: translate(
'We sent an email to the domain owner. Please complete the verification or your domain will stop working.'
);
return {
statusText: translate( 'Action required' ),
statusClass: 'status-error',
status: translate( 'Verify email' ),
noticeText,
icon: 'info',
listStatusWeight: 600,
};
}
if ( domain.expired ) {
let renewCta;
if ( domain.isRenewable ) {
const renewableUntil = moment.utc( domain.renewableUntil ).format( 'LL' );
renewCta =
purchase && siteSlug && domain.currentUserIsOwner
? translate(
'You can renew the domain at the regular rate until {{strong}}%(renewableUntil)s{{/strong}}. {{a}}Renew now{{/a}}',
{
components: {
strong: ,
a: (