File size: 4,994 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { WIDE_BREAKPOINT } from '@automattic/viewport';
import { useBreakpoint } from '@automattic/viewport-react';
import { Button, DropdownMenu, Tooltip, FormToggle } from '@wordpress/components';
import { Icon, info } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useScheduledUpdatesActivateMutation } from 'calypso/data/plugins/use-scheduled-updates-activate-mutation';
import { useUpdateScheduleQuery } from 'calypso/data/plugins/use-update-schedules-query';
import { Badge } from '../plugin-scheduled-updates-common/badge';
import { useDateTimeFormat } from '../plugin-scheduled-updates-common/hooks/use-date-time-format';
import { usePreparePluginsTooltipInfo } from '../plugin-scheduled-updates-common/hooks/use-prepare-plugins-tooltip-info';
import { usePrepareScheduleName } from '../plugin-scheduled-updates-common/hooks/use-prepare-schedule-name';
import { useIsEligibleForFeature } from './hooks/use-is-eligible-for-feature';
import { useSiteSlug } from './hooks/use-site-slug';
import { ellipsis } from './icons';

interface Props {
	onEditClick: ( id: string ) => void;
	onRemoveClick: ( id: string ) => void;
	onShowLogs: ( id: string ) => void;
}
export const ScheduleListTable = ( props: Props ) => {
	const siteSlug = useSiteSlug();
	const translate = useTranslate();
	const { isEligibleForFeature } = useIsEligibleForFeature();
	const isWideScreen = useBreakpoint( WIDE_BREAKPOINT );

	const { onEditClick, onRemoveClick, onShowLogs } = props;
	const { data: schedules = [] } = useUpdateScheduleQuery( siteSlug, isEligibleForFeature );
	const { countInstalledPlugins, preparePluginsTooltipInfo } =
		usePreparePluginsTooltipInfo( siteSlug );
	const { prepareScheduleName } = usePrepareScheduleName();
	const { prepareDateTime } = useDateTimeFormat( siteSlug );
	const { activateSchedule } = useScheduledUpdatesActivateMutation();

	/**
	 * NOTE: If you update the table structure,
	 * make sure to update the ScheduleListCards component as well
	 */
	return (
		<table className="plugins-update-manager-table">
			<thead>
				<tr>
					<th>{ translate( 'Name' ) }</th>
					<th>{ translate( 'Last update' ) }</th>
					<th>{ translate( 'Next update' ) }</th>
					<th className="frequency">{ translate( 'Frequency' ) }</th>
					<th>{ translate( 'Plugins' ) }</th>
					<th>{ translate( 'Active' ) }</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				{ schedules.map( ( schedule ) => (
					<tr key={ schedule.id }>
						<td className="name">
							<Button
								className="schedule-name"
								variant="link"
								onClick={ () => onEditClick && onEditClick( schedule.id ) }
							>
								{ prepareScheduleName( schedule ) }
							</Button>
						</td>
						<td className="last-update">
							{ schedule.last_run_status && (
								<>
									<Badge type={ schedule.last_run_status } />
									{ ( schedule.last_run_timestamp ||
										schedule.last_run_status === 'in-progress' ) && (
										<Button
											className="schedule-last-run"
											variant="link"
											onClick={ () => onShowLogs( schedule.id ) }
										>
											{ schedule.last_run_status === 'in-progress'
												? translate( 'In progress' )
												: isWideScreen &&
												  schedule.last_run_timestamp &&
												  prepareDateTime( schedule.last_run_timestamp ) }
										</Button>
									) }
								</>
							) }

							{ ! schedule.last_run_status && ! schedule.last_run_timestamp && '-' }
						</td>
						<td className="next-update">{ prepareDateTime( schedule.timestamp ) }</td>
						<td className="frequency">
							{
								{
									daily: translate( 'Daily' ),
									weekly: translate( 'Weekly' ),
								}[ schedule.schedule ]
							}
						</td>
						<td>
							{ countInstalledPlugins( schedule.args ) }
							{ schedule?.args && (
								<Tooltip
									text={ preparePluginsTooltipInfo( schedule.args ) as unknown as string }
									position="middle right"
									delay={ 0 }
									hideOnClick={ false }
								>
									<Icon className="icon-info" icon={ info } size={ 16 } />
								</Tooltip>
							) }
						</td>
						<td>
							<FormToggle
								checked={ schedule.active }
								onChange={ ( e ) =>
									activateSchedule( siteSlug, schedule.id, { active: e.target.checked } )
								}
							/>
						</td>
						<td style={ { textAlign: 'end' } }>
							<DropdownMenu
								popoverProps={ { position: 'bottom left' } }
								controls={ [
									{
										title: translate( 'Edit' ),
										onClick: () => onEditClick( schedule.id ),
									},
									{
										title: translate( 'Logs' ),
										onClick: () => onShowLogs( schedule.id ),
									},
									{
										title: translate( 'Remove' ),
										onClick: () => onRemoveClick( schedule.id ),
									},
								] }
								icon={ ellipsis }
								label={ translate( 'More' ) }
							/>
						</td>
					</tr>
				) ) }
			</tbody>
		</table>
	);
};