-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
history.jsx
137 lines (115 loc) · 4.54 KB
/
history.jsx
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
/*
* This file is part of Cockpit.
*
* Copyright (C) 2019 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
import moment from 'moment';
import React from "react";
import PropTypes from "prop-types";
import { OverlayTrigger, Tooltip } from "patternfly-react";
import cockpit from "cockpit";
const _ = cockpit.gettext;
function formatPkgs(pkgs) {
let names = Object.keys(pkgs).filter(i => i != "_time");
names.sort();
return names.map(n => (
<OverlayTrigger key={n} overlay={ <Tooltip id="tip-history">{ n + " " + pkgs[n] }</Tooltip> } placement="top">
<li>{n}</li>
</OverlayTrigger>)
);
}
export const PackageList = ({ packages }) => packages ? <ul className='flow-list'>{formatPkgs(packages)}</ul> : null;
export class History extends React.Component {
constructor() {
super();
this.state = { expanded: new Set([0]) };
}
onExpand(index) {
let e = new Set(this.state.expanded);
if (e.has(index))
e.delete(index);
else
e.add(index);
this.setState({ expanded: e });
}
/* Some PackageKit transactions come in pairs with identical package list,
* but different versions. This is an internal technicality, merge them
* together for presentation.
*
* Returns a time sorted (descending) list of objects like
* { time: moment_object, num_packages: 2, packages: {names...}}
*/
mergeHistory() {
let history = [];
let prevTime, prevPackages;
for (let i = 0; i < this.props.packagekit.length; ++i) {
let packages = Object.keys(this.props.packagekit[i]).filter(i => i != "_time");
let time = moment(this.props.packagekit[i]["_time"]);
packages.sort();
if (prevTime && (time - prevTime) <= 600000 /* 10 mins */ &&
prevPackages.toString() == packages.toString())
history.pop();
history.push({ time, packages: this.props.packagekit[i], num_packages: packages.length });
if (history.length === 3)
break;
prevPackages = packages;
prevTime = time;
}
return history;
}
render() {
const history = this.mergeHistory();
if (history.length === 0)
return null;
let rows = history.map((update, index) => {
const time = update.time.format("YYYY-MM-DD HH:mm");
let pkgcount, details;
pkgcount = (
<div className="list-view-pf-additional-info-item">
<span className="pficon pficon-bundle" />
{ cockpit.format(cockpit.ngettext("$0 Package", "$0 Packages", update.num_packages), update.num_packages) }
</div>);
details = (
<tr className="listing-ct-panel">
<td colSpan="3">
<div className="listing-ct-body">
<PackageList packages={update.packages} />
</div>
</td>
</tr>);
return (
<tbody key={index} className={ details && this.state.expanded.has(index) ? "open" : null } >
<tr className="listing-ct-item" onClick={ () => this.onExpand(index) } >
{ details ? <td className="listing-ct-toggle"><i className="fa fa-fw" /></td> : <td /> }
<th>{time}</th>
<td className="history-pkgcount">{pkgcount}</td>
</tr>
{details}
</tbody>);
});
return (
<React.Fragment>
<h2>{ _("Update History") }</h2>
<table className="listing-ct updates-history">
{rows}
</table>
</React.Fragment>
);
}
}
History.propTypes = {
packagekit: PropTypes.arrayOf(PropTypes.object).isRequired,
};