| 1 |
/* |
| 2 |
* Copyright (C) 2019 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
import React from "react"; |
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 11 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 12 |
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; |
| 13 |
import { |
| 14 |
BugIcon, |
| 15 |
CheckIcon, |
| 16 |
EnhancementIcon, |
| 17 |
ExclamationCircleIcon, |
| 18 |
InfoCircleIcon, |
| 19 |
SecurityIcon, |
| 20 |
ExclamationTriangleIcon, |
| 21 |
} from '@patternfly/react-icons'; |
| 22 |
|
| 23 |
import "./page-status.scss"; |
| 24 |
|
| 25 |
import { page_status } from "notifications"; |
| 26 |
|
| 27 |
function icon_for_type(type) { |
| 28 |
if (type == "error") |
| 29 |
return ( |
| 30 |
<Icon status="danger"> |
| 31 |
<ExclamationCircleIcon /> |
| 32 |
</Icon> |
| 33 |
); |
| 34 |
else if (type == "warning") |
| 35 |
return ( |
| 36 |
<Icon status="warning"> |
| 37 |
<ExclamationTriangleIcon /> |
| 38 |
</Icon> |
| 39 |
); |
| 40 |
else |
| 41 |
return ( |
| 42 |
<Icon status="info"> |
| 43 |
<InfoCircleIcon /> |
| 44 |
</Icon> |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
function get_pficon(name) { |
| 49 |
// set data-pficon for the tests |
| 50 |
if (name == "security") |
| 51 |
return <Icon isInline status="danger"><SecurityIcon data-pficon={name} /></Icon>; |
| 52 |
if (name == "enhancement") |
| 53 |
return <Icon isInline status="custom"><EnhancementIcon data-pficon={name} /></Icon>; |
| 54 |
if (name == "bug") |
| 55 |
return <Icon isInline className="pf-m-important"><BugIcon data-pficon={name} /></Icon>; |
| 56 |
if (name == "check") |
| 57 |
return <Icon isInline status="success"><CheckIcon data-pficon={name} /></Icon>; |
| 58 |
if (name == "spinner") |
| 59 |
return <Spinner diameter="1em" data-pficon={name} />; |
| 60 |
|
| 61 |
throw new Error(`get_pficon(): unknown icon name ${name}`); |
| 62 |
} |
| 63 |
|
| 64 |
export class PageStatusNotifications extends React.Component { |
| 65 |
constructor() { |
| 66 |
super(); |
| 67 |
this.state = { }; |
| 68 |
this.on_page_status_changed = () => this.setState({ }); |
| 69 |
} |
| 70 |
|
| 71 |
componentDidMount() { |
| 72 |
page_status.addEventListener("changed", this.on_page_status_changed); |
| 73 |
} |
| 74 |
|
| 75 |
componentWillUnmount() { |
| 76 |
page_status.removeEventListener("changed", this.on_page_status_changed); |
| 77 |
} |
| 78 |
|
| 79 |
render() { |
| 80 |
// Explicit allowlist for now, until we can get a dynamic list |
| 81 |
return ["system/services", "updates"].map(page => { |
| 82 |
const status = page_status.get(page); |
| 83 |
if (status && (status.type || status.details) && status.title) { |
| 84 |
let action; |
| 85 |
if (status.details && status.details.link !== undefined) { |
| 86 |
if (status.details.link) |
| 87 |
action = <Button variant="link" isInline component="a" |
| 88 |
onClick={ ev => { ev.preventDefault(); cockpit.jump("/" + status.details.link) } }>{status.title}</Button>; |
| 89 |
else |
| 90 |
action = <span>{status.title}</span>; // no link |
| 91 |
} else { |
| 92 |
action = <Button variant="link" isInline component="a" |
| 93 |
onClick={ ev => { ev.preventDefault(); cockpit.jump("/" + page) } }>{status.title}</Button>; |
| 94 |
} |
| 95 |
|
| 96 |
let icon; |
| 97 |
if (status.details && status.details.icon) |
| 98 |
icon = <span className={status.details.icon} />; |
| 99 |
else if (status.details && status.details.pficon) |
| 100 |
icon = get_pficon(status.details.pficon); |
| 101 |
else |
| 102 |
icon = icon_for_type(status.type); |
| 103 |
return ( |
| 104 |
<li id={ "page_status_notification_" + page.replace('/', '_') } key={page}> |
| 105 |
<Flex flexWrap={{ default: 'nowrap' }} spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 106 |
{icon} |
| 107 |
{action} |
| 108 |
</Flex> |
| 109 |
</li> |
| 110 |
); |
| 111 |
} else { |
| 112 |
return null; |
| 113 |
} |
| 114 |
}); |
| 115 |
} |
| 116 |
} |