Debian Codegraph @

pkg/packagekit/history.tsx

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React from "react";
7
8 import { Tooltip } from "@patternfly/react-core/dist/esm/components/Tooltip/index.js";
9 import { BundleIcon } from "@patternfly/react-icons";
10 import { ListingTable } from "cockpit-components-table.jsx";
11 import type { History as PackageKitEntry } from "_internal/packagemanager-abstract";
12 import * as timeformat from "timeformat";
13
14 import cockpit from "cockpit";
15
16 const _ = cockpit.gettext;
17
18 type Packages = PackageKitEntry["packages"];
19
20 function formatPkgs(pkgs: Packages) {
21 const names = Object.keys(pkgs);
22 names.sort();
23 return names.map(n => (
24 <li key={n}>
25 <Tooltip content={ n + " " + pkgs[n] }>
26 <span>{n}</span>
27 </Tooltip>
28 </li>)
29 );
30 }
31
32 export const PackageList = ({ packages }: { packages?: Packages }) => packages ? <ul className='flow-list'>{formatPkgs(packages)}</ul> : null;
33
34 interface HistoryProps {
35 packagekit: PackageKitEntry[];
36 }
37
38 export class History extends React.Component<HistoryProps> {
39 /* Some PackageKit transactions come in pairs with identical package list,
40 * but different versions. This is an internal technicality, merge them
41 * together for presentation.
42 *
43 * Returns a time sorted (descending) list of objects like
44 * { time: timestamp, num_packages: 2, packages: {names...}}
45 */
46 mergeHistory() {
47 const history: { time: number; packages: Packages; num_packages: number }[] = [];
48 let prevTime: number | undefined;
49 let prevPackages: string[] | undefined;
50
51 for (let i = 0; i < this.props.packagekit.length; ++i) {
52 const packages = Object.keys(this.props.packagekit[i].packages);
53 const time = this.props.packagekit[i].timestamp;
54 packages.sort();
55
56 if (prevTime && (time - prevTime) <= 600000 /* 10 mins */ &&
57 prevPackages && packages &&
58 prevPackages.toString() == packages.toString())
59 history.pop();
60
61 history.push({ time, packages: this.props.packagekit[i].packages, num_packages: packages.length });
62
63 if (history.length === 3)
64 break;
65
66 prevPackages = packages;
67 prevTime = time;
68 }
69
70 return history;
71 }
72
73 render() {
74 const history = this.mergeHistory();
75 if (history.length === 0)
76 return null;
77
78 const rows = history.map((update, index) => {
79 const pkgcount = (
80 <div className="list-view-pf-additional-info-item">
81 <BundleIcon />
82 { cockpit.format(cockpit.ngettext("$0 package", "$0 packages", update.num_packages), update.num_packages) }
83 </div>);
84
85 const expandedContent = <PackageList packages={update.packages} />;
86
87 return ({
88 props: { key: update.time },
89 columns: [
90 { title: timeformat.dateTime(update.time), props: { className: "history-time" } },
91 { title: pkgcount, props: { className: "history-pkgcount" } },
92 ],
93 initiallyExpanded: index == 0,
94 hasPadding: true,
95 expandedContent
96 });
97 });
98
99 return (
100 <ListingTable aria-label={_("Updates history")}
101 showHeader={false}
102 className="updates-history"
103 columns={[_("Time"), _("History package count")]}
104 rows={rows} />
105 );
106 }
107 }