File size: 4,515 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
import { Badge } from '@automattic/ui';
import { Link } from '@tanstack/react-router';
import {
Icon,
__experimentalText as Text,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { dateI18n } from '@wordpress/date';
import { sprintf, __ } from '@wordpress/i18n';
import { caution, reusableBlock } from '@wordpress/icons';
import { useMemo } from 'react';
import { DomainTypes } from '../../data/domains';
import type { Domain, Site } from '../../data/types';
import type { Field } from '@wordpress/dataviews';
const textOverflowStyles = {
overflowX: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
} as const;
const IneligibleIndicator = () => <Text color="#CCCCCC">-</Text>;
const DomainName = ( { domain, site, value }: { domain: Domain; site?: Site; value: string } ) => {
const siteSlug = site?.slug ?? domain.site_slug;
const domainManagementUrl = site
? `${ window.location.origin }/overview/site-domain/domain/${ domain.domain }/${ siteSlug }`
: `${ window.location.origin }/domains/manage/all/overview/${ domain.domain }/${ siteSlug }`;
return (
<Link to={ domainManagementUrl } disabled={ domain.type === DomainTypes.WPCOM }>
<HStack alignment="center" spacing={ 1 }>
<span style={ textOverflowStyles }>{ value }</span>
{ domain.primary_domain && (
<span style={ { flexShrink: 0 } }>
<Badge>{ __( 'Primary' ) }</Badge>
</span>
) }
</HStack>
</Link>
);
};
const DomainExpiry = ( {
domain,
value,
isCompact = false,
}: {
domain: Domain;
value: string;
isCompact?: boolean;
} ) => {
if ( ! domain.expiry ) {
return __( 'Free forever' );
}
const isAutoRenewing = Boolean( domain.auto_renewing );
const isExpired = new Date( domain.expiry ) < new Date();
const isHundredYearDomain = Boolean( domain.is_hundred_year_domain );
const renderExpiry = () => {
if ( isHundredYearDomain ) {
return sprintf(
/* translators: %s - The date until which a domain was paid for */
__( 'Paid until %s' ),
value
);
}
if ( isExpired ) {
return sprintf(
/* translators: %s - The date on which a domain has expired */
__( 'Expired on %s' ),
value
);
}
if ( ! isAutoRenewing ) {
return sprintf(
/* translators: %s - The date on which a domain expires */
__( 'Expires on %s' ),
value
);
}
return sprintf(
/* translators: %s - The future date on which a domain renews */
__( 'Renews on %s' ),
value
);
};
return (
<HStack justify="flex-start" alignment="center" spacing={ 1 }>
{ ! isCompact && (
<Icon
icon={ isExpired || isHundredYearDomain ? caution : reusableBlock }
size={ 16 }
style={ { fill: 'currentColor' } }
/>
) }
<span>{ renderExpiry() }</span>
</HStack>
);
};
export const useFields = ( { site }: { site?: Site } = {} ) => {
const fields: Field< Domain >[] = useMemo(
() => [
{
id: 'domain',
label: __( 'Domains' ),
enableHiding: false,
enableSorting: true,
enableGlobalSearch: true,
getValue: ( { item }: { item: Domain } ) => item.domain,
render: ( { field, item } ) => (
<DomainName domain={ item } site={ site } value={ field.getValue( { item } ) } />
),
},
{
id: 'type',
label: __( 'Type' ),
enableHiding: false,
enableSorting: false,
},
// {
// id: 'owner',
// label: __( 'Owner' ),
// enableHiding: false,
// enableSorting: true,
// },
{
id: 'blog_name',
label: __( 'Site' ),
enableHiding: false,
enableSorting: true,
getValue: ( { item }: { item: Domain } ) => item.blog_name ?? '',
},
// {
// id: 'ssl_status',
// label: __( 'SSL' ),
// enableHiding: false,
// enableSorting: true,
// },
{
id: 'expiry',
label: __( 'Expires/Renews on' ),
enableHiding: false,
enableSorting: true,
getValue: ( { item }: { item: Domain } ) =>
item.expiry ? dateI18n( 'F j, Y', item.expiry ) : '',
render: ( { field, item } ) => (
<DomainExpiry
domain={ item }
value={ field.getValue( { item } ) }
isCompact={ !! site }
/>
),
},
{
id: 'domain_status',
label: __( 'Status' ),
enableHiding: false,
enableSorting: true,
getValue: ( { item }: { item: Domain } ) => item.domain_status?.status ?? '',
render: ( { field, item } ) => {
const value = field.getValue( { item } );
return value || <IneligibleIndicator />;
},
},
],
[ site ]
);
return fields;
};
|