| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { Button, Tooltip } from '@wordpress/components'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import { store as preloadStore } from '../../store'; |
| 7 |
import { useDispatch } from '@wordpress/data'; |
| 8 |
|
| 9 |
/** |
| 10 |
* @param {boolean} isPreloading Whether we are preloading. |
| 11 |
* @param {string} text The translated button text. |
| 12 |
* @param {string} label The translated Tooltip text. |
| 13 |
* @param {boolean} force Whether we are force preloading the entire site. |
| 14 |
* @param {boolean} hidden If we are preloading, one of the buttons should be hidden. |
| 15 |
*/ |
| 16 |
export default function PreloadButton( { isPreloading, text, label, force = false, hidden = false } ) { |
| 17 |
if (hidden) { |
| 18 |
return null; |
| 19 |
} |
| 20 |
|
| 21 |
const { startPreloader, cancelPreloader } = useDispatch(preloadStore); |
| 22 |
|
| 23 |
const handleClick = async () => { |
| 24 |
if (isPreloading) { |
| 25 |
await cancelPreloader(); |
| 26 |
} else { |
| 27 |
await startPreloader(force); |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
const buttonText = isPreloading |
| 32 |
? __('Cancel Preloading', 'solid-performance') |
| 33 |
: text; |
| 34 |
|
| 35 |
const button = ( |
| 36 |
<Button |
| 37 |
variant="secondary" |
| 38 |
onClick={handleClick} |
| 39 |
> |
| 40 |
{buttonText} |
| 41 |
</Button> |
| 42 |
); |
| 43 |
|
| 44 |
const isLongLabel = label?.length > 80; |
| 45 |
const tooltipClassName = isLongLabel ? 'swpsp-preload-long-tooltip' : ''; |
| 46 |
|
| 47 |
return label ? ( |
| 48 |
<Tooltip text={label} className={tooltipClassName}> |
| 49 |
{button} |
| 50 |
</Tooltip> |
| 51 |
) : ( |
| 52 |
button |
| 53 |
); |
| 54 |
}; |
| 55 |
|