File size: 5,352 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
	__experimentalText as Text,
	Tooltip,
	Spinner,
	Button,
	Card,
	CardHeader,
	CardBody,
} from '@wordpress/components';
import { Icon, info, arrowLeft } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useCallback, useEffect } from 'react';
import Timeline from 'calypso/components/timeline';
import TimelineEvent from 'calypso/components/timeline/timeline-event';
import { useCorePluginsQuery } from 'calypso/data/plugins/use-core-plugins-query';
import { useUpdateScheduleLogsQuery } from 'calypso/data/plugins/use-update-schedule-logs-query';
import {
	type ScheduleUpdates,
	useUpdateScheduleQuery,
} from 'calypso/data/plugins/use-update-schedules-query';
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 { useSiteAdminUrl } from './hooks/use-site-admin-url';
import { useSiteSlug } from './hooks/use-site-slug';
import {
	getLogDetails,
	getLogIcon,
	getLogIconStatus,
	addSecondsToFormat,
	shouldIndentTimelineEvent,
} from './schedule-logs.helper';

interface Props {
	scheduleId: string;
	onNavBack?: () => void;
	setNavigationTitle: ( title: string ) => void;
}
export const ScheduleLogs = ( props: Props ) => {
	const siteSlug = useSiteSlug();
	const translate = useTranslate();
	const { scheduleId, onNavBack, setNavigationTitle } = props;

	const siteAdminUrl = useSiteAdminUrl();
	const {
		dateFormat: phpDateFormat,
		timeFormat: phpTimeFormat,
		convertPhpToMomentFormat,
	} = useDateTimeFormat( siteSlug );
	const dateFormat = convertPhpToMomentFormat( phpDateFormat );
	const timeFormat = addSecondsToFormat( convertPhpToMomentFormat( phpTimeFormat ) );
	const { prepareScheduleName } = usePrepareScheduleName();
	const { preparePluginsTooltipInfo } = usePreparePluginsTooltipInfo( siteSlug );
	const { data: plugins = [] } = useCorePluginsQuery( siteSlug, true, true );
	const { isEligibleForFeature } = useIsEligibleForFeature();
	const {
		data: schedules = [],
		isFetched,
		isPending,
	} = useUpdateScheduleQuery( siteSlug, isEligibleForFeature );
	const schedule = schedules.find( ( s ) => s.id === scheduleId );
	const { data: scheduleLogs = [], isPending: isPendingLogs } = useUpdateScheduleLogsQuery(
		siteSlug,
		scheduleId
	);

	const goToPluginsPage = useCallback( () => {
		window.location.href = `${ siteAdminUrl }plugins.php`;
	}, [ siteAdminUrl ] );

	useEffect( () => {
		// a bit hacky, but needed to override the title
		setTimeout( () => {
			setNavigationTitle(
				`${ translate( 'Logs' ) } - ${ prepareScheduleName( schedule as ScheduleUpdates ) }`
			);
		} );
	}, [ setNavigationTitle, schedule ] );

	if ( isPending ) {
		return <Spinner />;
	}
	// If the schedule is not found, navigate back to the list
	else if ( isFetched && ! schedule ) {
		onNavBack && onNavBack();
		return null;
	}

	return (
		<Card className="plugins-update-manager">
			<CardHeader size="extraSmall">
				<div className="ch-placeholder">
					{ onNavBack && (
						<Button icon={ arrowLeft } onClick={ onNavBack }>
							{ translate( 'Back' ) }
						</Button>
					) }
				</div>
				<Text>
					{ translate( 'Logs' ) } - { prepareScheduleName( schedule as ScheduleUpdates ) }
				</Text>
				<div className="ch-placeholder">
					<Text isBlock align="end" lineHeight={ 2.5 }>
						{ translate( '%(pluginsNumber)d plugin', '%(pluginsNumber)d plugins', {
							count: schedule?.args?.length || 0,
							args: {
								pluginsNumber: schedule?.args?.length || 0,
							},
						} ) }
						{ schedule?.args && (
							<Tooltip
								text={ preparePluginsTooltipInfo( schedule.args ) as unknown as string }
								delay={ 0 }
								hideOnClick={ false }
							>
								<Icon className="icon-info" icon={ info } size={ 16 } />
							</Tooltip>
						) }
					</Text>
				</div>
			</CardHeader>
			<CardBody>
				{ isPendingLogs && <Spinner /> }
				{ ! isPendingLogs && ! scheduleLogs.length && (
					<div className="empty-state">
						<Text align="center">{ translate( 'No logs available at the moment.' ) }</Text>
					</div>
				) }
			</CardBody>
			{ scheduleLogs.map( ( logs, i ) => (
				<Timeline key={ i }>
					{ logs.reverse().map( ( log, j ) => (
						<TimelineEvent
							key={ `${ log.timestamp }.${ j }` }
							date={ log.date }
							dateFormat={
								log.action === 'PLUGIN_UPDATES_START'
									? `${ dateFormat } ${ timeFormat }`
									: timeFormat
							}
							detail={ getLogDetails( log, plugins, siteSlug ) }
							icon={ getLogIcon( log ) }
							iconBackground={ getLogIconStatus( log ) }
							className={ shouldIndentTimelineEvent( log ) ? 'indent' : '' }
							disabled={ log.action === 'PLUGIN_UPDATES_START' }
							actionLabel={
								// show a button only for the most recent update failure
								i === 0 && log.action === 'PLUGIN_UPDATE_FAILURE'
									? translate( 'Try manual update' )
									: undefined
							}
							actionIsPrimary
							onActionClick={ goToPluginsPage }
						/>
					) ) }
				</Timeline>
			) ) }
		</Card>
	);
};