forked from cockpit-project/cockpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page-status.jsx
117 lines (105 loc) · 4.38 KB
/
page-status.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
/*
* 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 cockpit from "cockpit";
import React from "react";
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js";
import {
BugIcon,
CheckIcon,
EnhancementIcon,
ExclamationCircleIcon,
InfoCircleIcon,
SecurityIcon,
ExclamationTriangleIcon,
} from '@patternfly/react-icons';
import "./page-status.scss";
import { page_status } from "notifications";
function icon_for_type(type) {
if (type == "error")
return <ExclamationCircleIcon className="ct-exclamation-circle" />;
else if (type == "warning")
return <ExclamationTriangleIcon className="ct-exclamation-triangle" />;
else
return <InfoCircleIcon className="ct-info-circle" />;
}
function get_pficon(name) {
// set data-pficon for the tests
if (name == "security")
return <SecurityIcon data-pficon={name} />;
if (name == "enhancement")
return <EnhancementIcon data-pficon={name} />;
if (name == "bug")
return <BugIcon data-pficon={name} className="page-status-bug-icon" />;
if (name == "check")
return <CheckIcon color="green" data-pficon={name} />;
if (name == "spinner")
return <Spinner size="md" data-pficon={name} />;
throw new Error(`get_pficon(): unknown icon name ${name}`);
}
export class PageStatusNotifications extends React.Component {
constructor() {
super();
this.state = { };
this.on_page_status_changed = () => this.setState({ });
}
componentDidMount() {
page_status.addEventListener("changed", this.on_page_status_changed);
}
componentWillUnmount() {
page_status.removeEventListener("changed", this.on_page_status_changed);
}
render() {
// Explicit allowlist for now, until we can get a dynamic list
return ["system/services", "updates"].map(page => {
const status = page_status.get(page);
if (status && (status.type || status.details) && status.title) {
let action;
if (status.details && status.details.link !== undefined) {
if (status.details.link)
action = <Button variant="link" isInline component="a"
onClick={ ev => { ev.preventDefault(); cockpit.jump("/" + status.details.link) } }>{status.title}</Button>;
else
action = <span>{status.title}</span>; // no link
} else {
action = <Button variant="link" isInline component="a"
onClick={ ev => { ev.preventDefault(); cockpit.jump("/" + page) } }>{status.title}</Button>;
}
let icon;
if (status.details && status.details.icon)
icon = <span className={status.details.icon} />;
else if (status.details && status.details.pficon)
icon = get_pficon(status.details.pficon);
else
icon = icon_for_type(status.type);
return (
<li id={ "page_status_notification_" + page.replace('/', '_') } key={page}>
<Flex flexWrap={{ default: 'nowrap' }} spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}>
{icon}
{action}
</Flex>
</li>
);
} else {
return null;
}
});
}
}