| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { Button, Flex, FlexItem, TabPanel } from '@wordpress/components'; |
| 5 |
import { useCopyToClipboard } from '@wordpress/compose'; |
| 6 |
import { dateI18n } from '@wordpress/date'; |
| 7 |
import { Fragment, useMemo, WPElement } from '@wordpress/element'; |
| 8 |
import { __, sprintf } from '@wordpress/i18n'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Internal dependencies. |
| 12 |
*/ |
| 13 |
import { useSettingsScreen } from '../../settings-screen'; |
| 14 |
import { useSync } from '../../sync'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Sync logs component. |
| 18 |
* |
| 19 |
* @returns {WPElement} Component. |
| 20 |
*/ |
| 21 |
export default () => { |
| 22 |
const { clearLog, log } = useSync(); |
| 23 |
const { createNotice } = useSettingsScreen(); |
| 24 |
|
| 25 |
/** |
| 26 |
* The log as plain text. |
| 27 |
* |
| 28 |
* @type {string} |
| 29 |
*/ |
| 30 |
const plainTextLog = useMemo(() => { |
| 31 |
return log.map((m) => `${m.dateTime} ${m.message}`).join('\n'); |
| 32 |
}, [log]); |
| 33 |
|
| 34 |
/** |
| 35 |
* Error messages from the log. |
| 36 |
* |
| 37 |
* @type {Array} |
| 38 |
*/ |
| 39 |
const errorLog = useMemo( |
| 40 |
() => log.filter((m) => m.status === 'error' || m.status === 'warning'), |
| 41 |
[log], |
| 42 |
); |
| 43 |
|
| 44 |
/** |
| 45 |
* Handle clicking the clear log button. |
| 46 |
* |
| 47 |
* @returns {void} |
| 48 |
*/ |
| 49 |
const onClear = () => { |
| 50 |
clearLog(); |
| 51 |
}; |
| 52 |
|
| 53 |
/** |
| 54 |
* Copy to clipboard button ref. |
| 55 |
* |
| 56 |
* @type {object} |
| 57 |
*/ |
| 58 |
const ref = useCopyToClipboard(plainTextLog, () => { |
| 59 |
createNotice('info', __('Copied log to clipboard.', 'elasticpress')); |
| 60 |
}); |
| 61 |
|
| 62 |
/** |
| 63 |
* Log tabs. |
| 64 |
* |
| 65 |
* @type {Array} |
| 66 |
*/ |
| 67 |
const tabs = [ |
| 68 |
{ |
| 69 |
messages: log, |
| 70 |
name: 'full', |
| 71 |
title: __('Full Log', 'elasticpress'), |
| 72 |
}, |
| 73 |
{ |
| 74 |
messages: errorLog, |
| 75 |
name: 'error', |
| 76 |
title: sprintf( |
| 77 |
/* translators: %d: Error message count. */ |
| 78 |
__('Errors (%d)', 'elasticpress'), |
| 79 |
errorLog.length, |
| 80 |
), |
| 81 |
}, |
| 82 |
]; |
| 83 |
|
| 84 |
return ( |
| 85 |
<> |
| 86 |
<TabPanel className="ep-sync-log" tabs={tabs}> |
| 87 |
{({ messages }) => ( |
| 88 |
<div className="ep-sync-messages"> |
| 89 |
{messages.map((m) => ( |
| 90 |
<Fragment key={m.id}> |
| 91 |
<div className="ep-sync-messages__line-number" role="presentation"> |
| 92 |
{dateI18n('Y-m-d H:i:s', m.dateTime)} |
| 93 |
</div> |
| 94 |
<div |
| 95 |
className={`ep-sync-messages__message ep-sync-messages__message--${m.status}`} |
| 96 |
> |
| 97 |
{m.message} |
| 98 |
</div> |
| 99 |
</Fragment> |
| 100 |
))} |
| 101 |
</div> |
| 102 |
)} |
| 103 |
</TabPanel> |
| 104 |
<Flex justify="start"> |
| 105 |
<FlexItem> |
| 106 |
<Button disabled={!log.length} onClick={onClear} variant="secondary"> |
| 107 |
{__('Clear log', 'elasticpress')} |
| 108 |
</Button> |
| 109 |
</FlexItem> |
| 110 |
<FlexItem> |
| 111 |
<Button disabled={!log.length} ref={ref} variant="secondary"> |
| 112 |
{__('Copy log to clipboard', 'elasticpress')} |
| 113 |
</Button> |
| 114 |
</FlexItem> |
| 115 |
</Flex> |
| 116 |
</> |
| 117 |
); |
| 118 |
}; |
| 119 |
|