PluginProbe
ElasticPress / 5.0.0
ElasticPress v5.0.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / sync-ui / components / log.js

log.js in ElasticPress 5.0.0, at assets/js/sync-ui/components/log.js

119 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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