/** * WordPress dependencies */ import { Button, CheckboxControl, Modal, TextControl, } from '@safe-wordpress/components'; import { useSelect, useDispatch } from '@safe-wordpress/data'; import { useState } from '@safe-wordpress/element'; import { _x, sprintf } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import { noop } from 'lodash'; import { store as NAB_ACCOUNT } from '@nab/account'; import { HelpIcon, ConfirmationDialog } from '@nab/components'; import { store as NAB_DATA } from '@nab/data'; import { formatNumber } from '@nab/i18n'; import type { Site as SiteType, SiteId } from '@nab/types'; /** * Internal dependencies */ import './style.scss'; export type SiteProps = SiteType & { readonly displayQuotaLimit?: boolean; }; export const Site = ( { displayQuotaLimit, actualUrl, id, isCurrentSite, usedMonthlyQuota, maxMonthlyQuota, url, }: SiteProps ): JSX.Element => (
  • { isCurrentSite ? ( { actualUrl } { !! actualUrl && url !== actualUrl && ( ) } { _x( 'This site', 'text', 'nelio-ab-testing' ) } ) : ( { url } ) } { !! displayQuotaLimit && ( { _x( 'Quota Usage', 'text', 'nelio-ab-testing' ) } { ': ' } { formatNumber( usedMonthlyQuota ) } { ' / ' } { stringifyMaxQuota( maxMonthlyQuota ) } ) }
  • ); // ============== // INTERNAL VIEWS // ============== type ActionProps = { readonly siteId: SiteId; }; const LimitQuotaAction = ( { siteId }: ActionProps ) => { const [ isDialogOpen, setDialogOpen ] = useState( false ); const canLimitQuota = useSelect( ( select ) => select( NAB_DATA ).canLimitSiteQuota(), [] ); const { limitSiteQuota } = useDispatch( NAB_ACCOUNT ); const currentSiteLimit = useSelect( ( select ) => select( NAB_ACCOUNT ).getSite( siteId )?.maxMonthlyQuota ?? -1, [ siteId ] ); if ( ! canLimitQuota ) { return null; } return ( <> { isDialogOpen && ( setDialogOpen( false ) } onSave={ ( value ) => limitSiteQuota( siteId, value ) } /> ) } ); }; const DeleteAction = ( { siteId }: ActionProps ) => { const [ isDialogOpen, setDialogOpen ] = useState( false ); const { unlinkSite } = useDispatch( NAB_ACCOUNT ); return ( <> setDialogOpen( false ) } onConfirm={ () => { setDialogOpen( false ); void unlinkSite( siteId ); } } /> ); }; const SiteQuotaDialog = ( { initialValue, onClose, onSave: save, }: { readonly initialValue: string; readonly onClose: () => void; readonly onSave: ( value: number ) => Promise< unknown >; } ) => { const [ isSaving, setSaving ] = useState( false ); const [ value, setValue ] = useState( initialValue ); const isDirty = value !== initialValue; const isUnlimited = ( n: number ) => n === -1; const onSave = () => { const intValue = Number.parseInt( value ); const limit = isUnlimited( intValue ) ? -1 : Math.abs( intValue ); setSaving( true ); save( limit ) .then( onClose ) .catch( () => setSaving( false ) ); }; return ( setValue( enabled ? '1000' : '-1' ) } label={ _x( 'Limit quota usage in this site', 'command', 'nelio-ab-testing' ) } /> { '-1' !== value && ( ) }
    ); }; // ======= // HELPERS // ======= const stringifyMaxQuota = ( n: number ) => -1 === n ? '∞' : formatNumber( n );