| 1 |
/** |
| 2 |
* External dependencies. |
| 3 |
*/ |
| 4 |
import classnames from 'classnames'; |
| 5 |
|
| 6 |
/** |
| 7 |
* WordPress dependencies. |
| 8 |
*/ |
| 9 |
import { Icon } from '@wordpress/components'; |
| 10 |
import { createInterpolateElement, useMemo, WPElement } from '@wordpress/element'; |
| 11 |
import { dateI18n } from '@wordpress/date'; |
| 12 |
import { __, sprintf } from '@wordpress/i18n'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Internal dependencies. |
| 16 |
*/ |
| 17 |
import { useSync } from '../../sync'; |
| 18 |
import sync from './icons/sync'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Sync button component. |
| 22 |
* |
| 23 |
* @returns {WPElement} Component. |
| 24 |
*/ |
| 25 |
export default () => { |
| 26 |
const { |
| 27 |
isCli, |
| 28 |
isComplete, |
| 29 |
isFailed, |
| 30 |
isPaused, |
| 31 |
itemsProcessed, |
| 32 |
itemsTotal, |
| 33 |
syncStartDateTime, |
| 34 |
syncTrigger, |
| 35 |
} = useSync(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Sync progress label. |
| 39 |
*/ |
| 40 |
const label = useMemo( |
| 41 |
/** |
| 42 |
* Determine appropriate sync status label. |
| 43 |
* |
| 44 |
* @returns {string} Sync progress label. |
| 45 |
*/ |
| 46 |
() => { |
| 47 |
if (isFailed) { |
| 48 |
return __('Sync failed', 'elasticpress'); |
| 49 |
} |
| 50 |
|
| 51 |
if (isComplete) { |
| 52 |
return __('Sync complete', 'elasticpress'); |
| 53 |
} |
| 54 |
|
| 55 |
if (isPaused) { |
| 56 |
return __('Sync paused', 'elasticpress'); |
| 57 |
} |
| 58 |
|
| 59 |
if (isCli) { |
| 60 |
return __('WP CLI sync in progress', 'elasticpress'); |
| 61 |
} |
| 62 |
|
| 63 |
return __('Sync in progress', 'elasticpress'); |
| 64 |
}, |
| 65 |
[isCli, isComplete, isFailed, isPaused], |
| 66 |
); |
| 67 |
|
| 68 |
const now = itemsTotal ? Math.floor((itemsProcessed / itemsTotal) * 100) : 100; |
| 69 |
|
| 70 |
const why = useMemo(() => { |
| 71 |
if (isCli) { |
| 72 |
/* translators: %1$s Sync start date and time. */ |
| 73 |
return __('Started manually from WP CLI at <time>%s</time>.', 'elasticpress'); |
| 74 |
} |
| 75 |
|
| 76 |
switch (syncTrigger) { |
| 77 |
case 'features': |
| 78 |
/* translators: %1$s Sync start date and time. */ |
| 79 |
return __( |
| 80 |
'Started automatically after a change to feature settings at <time>%s</time>.', |
| 81 |
'elasticpress', |
| 82 |
); |
| 83 |
case 'install': |
| 84 |
/* translators: %1$s Sync start date and time. */ |
| 85 |
return __( |
| 86 |
'Started automatically after installing the ElasticPress plugin at <time>%s</time>.', |
| 87 |
'elasticpress', |
| 88 |
); |
| 89 |
case 'manual': |
| 90 |
/* translators: %1$s Sync start date and time. */ |
| 91 |
return __( |
| 92 |
'Started manually from the Sync page at <time>%s</time>.', |
| 93 |
'elasticpress', |
| 94 |
); |
| 95 |
case 'synonyms-error': |
| 96 |
/* translators: %1$s Sync start date and time. */ |
| 97 |
return __( |
| 98 |
'Started manually from an error on the Synonyms Settings page at <time>%s</time>.', |
| 99 |
'elasticpress', |
| 100 |
); |
| 101 |
case 'upgrade': |
| 102 |
/* translators: %1$s Sync start date and time. */ |
| 103 |
return __( |
| 104 |
'Started automatically after updating the ElasticPress plugin at <time>%s</time>.', |
| 105 |
'elasticpress', |
| 106 |
); |
| 107 |
default: |
| 108 |
/* translators: %1$s Sync start date and time. */ |
| 109 |
return __('Started on <time>%s</time>.', 'elasticpress'); |
| 110 |
} |
| 111 |
}, [isCli, syncTrigger]); |
| 112 |
|
| 113 |
return ( |
| 114 |
<div |
| 115 |
className={classnames('ep-sync-progress', { |
| 116 |
'ep-sync-progress--syncing': !isPaused && !isComplete && !isFailed, |
| 117 |
})} |
| 118 |
> |
| 119 |
<Icon icon={sync} /> |
| 120 |
<div id="ep-sync-progress-details" className="ep-sync-progress__details"> |
| 121 |
<strong>{label}</strong> |
| 122 |
{syncStartDateTime |
| 123 |
? createInterpolateElement( |
| 124 |
sprintf(why, dateI18n('g:ia l F jS, Y', syncStartDateTime)), |
| 125 |
{ |
| 126 |
time: <time dateTime={dateI18n('c', syncStartDateTime)} />, |
| 127 |
}, |
| 128 |
) |
| 129 |
: null} |
| 130 |
</div> |
| 131 |
<div className="ep-sync-progress__progress-bar"> |
| 132 |
<div |
| 133 |
aria-labelledby="ep-sync-progress-details" |
| 134 |
aria-valuemax={100} |
| 135 |
aria-valuemin={0} |
| 136 |
aria-valuenow={now} |
| 137 |
className={classnames('ep-sync-progress-bar', { |
| 138 |
'ep-sync-progress-bar--complete': isComplete, |
| 139 |
'ep-sync-progress-bar--failed': isFailed, |
| 140 |
})} |
| 141 |
role="progressbar" |
| 142 |
> |
| 143 |
<div |
| 144 |
className="ep-sync-progress-bar__progress" |
| 145 |
style={{ minWidth: `${now}%` }} |
| 146 |
/> |
| 147 |
</div> |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
); |
| 151 |
}; |
| 152 |
|