| 1 |
/* |
| 2 |
* Copyright (C) 2019 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import '../lib/patternfly/patternfly-6-cockpit.scss'; |
| 7 |
import 'polyfills'; |
| 8 |
import 'cockpit-dark-theme'; // once per page |
| 9 |
import cockpit from "cockpit"; |
| 10 |
|
| 11 |
import React from 'react'; |
| 12 |
import { createRoot } from "react-dom/client"; |
| 13 |
import { Page, PageSection, PageSectionVariants } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 14 |
import { Gallery } from "@patternfly/react-core/dist/esm/layouts/Gallery/index.js"; |
| 15 |
import { Dropdown, DropdownItem, DropdownList } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js'; |
| 16 |
import { MenuToggle, MenuToggleAction } from "@patternfly/react-core/dist/esm/components/MenuToggle"; |
| 17 |
|
| 18 |
import { superuser } from "superuser"; |
| 19 |
|
| 20 |
import { SystemInformationCard } from './overview-cards/systemInformationCard.jsx'; |
| 21 |
import { ConfigurationCard } from './overview-cards/configurationCard.jsx'; |
| 22 |
import { HealthCard } from './overview-cards/healthCard.jsx'; |
| 23 |
import { MotdCard } from './overview-cards/motdCard.jsx'; |
| 24 |
import { UsageCard } from './overview-cards/usageCard.jsx'; |
| 25 |
import { SuperuserAlert } from './superuser-alert.jsx'; |
| 26 |
import { superuser_proxy, SuperuserIndicator } from "superuser-dialogs"; |
| 27 |
import { ShutdownModal } from 'cockpit-components-shutdown.jsx'; |
| 28 |
import { WithDialogs, DialogsContext } from "dialogs.jsx"; |
| 29 |
|
| 30 |
import "./overview.scss"; |
| 31 |
import { Title, TitleSizes } from '@patternfly/react-core/dist/esm/components/Title'; |
| 32 |
|
| 33 |
const _ = cockpit.gettext; |
| 34 |
|
| 35 |
class OverviewPage extends React.Component { |
| 36 |
static contextType = DialogsContext; |
| 37 |
|
| 38 |
constructor(props) { |
| 39 |
super(props); |
| 40 |
|
| 41 |
this.state = { |
| 42 |
actionIsOpen: false, |
| 43 |
privileged: true, |
| 44 |
}; |
| 45 |
this.hostnameMonitor = this.hostnameMonitor.bind(this); |
| 46 |
this.onPermissionChanged = this.onPermissionChanged.bind(this); |
| 47 |
|
| 48 |
this.superuser = superuser_proxy(); |
| 49 |
} |
| 50 |
|
| 51 |
componentDidMount() { |
| 52 |
this.hostnameMonitor(); |
| 53 |
superuser.addEventListener("changed", this.onPermissionChanged); |
| 54 |
this.onPermissionChanged(); |
| 55 |
} |
| 56 |
|
| 57 |
componentWillUnmount() { |
| 58 |
superuser.removeEventListener("changed", this.onPermissionChanged); |
| 59 |
} |
| 60 |
|
| 61 |
onPermissionChanged() { |
| 62 |
this.setState({ privileged: superuser.allowed }); |
| 63 |
} |
| 64 |
|
| 65 |
hostname_text() { |
| 66 |
if (!this.state.hostnameData) |
| 67 |
return undefined; |
| 68 |
|
| 69 |
const pretty_hostname = this.state.hostnameData.PrettyHostname; |
| 70 |
const static_hostname = this.state.hostnameData.StaticHostname; |
| 71 |
let str = this.state.hostnameData.Hostname; |
| 72 |
|
| 73 |
if (pretty_hostname && static_hostname && static_hostname != pretty_hostname) |
| 74 |
str = pretty_hostname + " (" + static_hostname + ")"; |
| 75 |
else if (static_hostname) |
| 76 |
str = static_hostname; |
| 77 |
|
| 78 |
return str || ''; |
| 79 |
} |
| 80 |
|
| 81 |
hostnameMonitor() { |
| 82 |
this.client = cockpit.dbus('org.freedesktop.hostname1'); |
| 83 |
this.hostname_proxy = this.client.proxy('org.freedesktop.hostname1', |
| 84 |
'/org/freedesktop/hostname1'); |
| 85 |
this.hostname_proxy.addEventListener("changed", data => { |
| 86 |
this.setState({ hostnameData: data.detail }); |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
render() { |
| 91 |
const Dialogs = this.context; |
| 92 |
const { actionIsOpen } = this.state; |
| 93 |
const dropdownItems = [ |
| 94 |
<DropdownItem key="reboot" id="reboot" |
| 95 |
onClick={() => Dialogs.show(<ShutdownModal />)} |
| 96 |
component="button"> |
| 97 |
{_("Reboot")} |
| 98 |
</DropdownItem>, |
| 99 |
<DropdownItem key="shutdown" id="shutdown" |
| 100 |
onClick={() => Dialogs.show(<ShutdownModal shutdown />)} |
| 101 |
component="button"> |
| 102 |
{_("Shutdown")} |
| 103 |
</DropdownItem>, |
| 104 |
]; |
| 105 |
|
| 106 |
let headerActions = null; |
| 107 |
if (this.state.privileged) |
| 108 |
headerActions = ( |
| 109 |
<Dropdown onSelect={() => this.setState({ actionIsOpen: false })} |
| 110 |
toggle={(toggleRef) => ( |
| 111 |
<MenuToggle |
| 112 |
ref={toggleRef} |
| 113 |
variant="secondary" |
| 114 |
splitButtonItems={[ |
| 115 |
<MenuToggleAction |
| 116 |
id='reboot-button' |
| 117 |
key='reboot-button' |
| 118 |
onClick={() => Dialogs.show(<ShutdownModal />)} |
| 119 |
> |
| 120 |
{_("Reboot")} |
| 121 |
</MenuToggleAction> |
| 122 |
]} |
| 123 |
onClick={() => this.setState({ actionIsOpen: !actionIsOpen })} |
| 124 |
id="shutdown-group" |
| 125 |
/> |
| 126 |
)} |
| 127 |
isOpen={actionIsOpen} |
| 128 |
popperProps={{ position: "right" }} |
| 129 |
> |
| 130 |
<DropdownList> |
| 131 |
{dropdownItems} |
| 132 |
</DropdownList> |
| 133 |
</Dropdown> |
| 134 |
); |
| 135 |
|
| 136 |
const show_superuser = ( |
| 137 |
cockpit.transport.host && cockpit.transport.host != "localhost" && |
| 138 |
!(window.parent.name == "cockpit1" && window.parent.features && |
| 139 |
window.parent.features.navbar_is_for_current_machine)); |
| 140 |
|
| 141 |
return ( |
| 142 |
<Page className='pf-m-no-sidebar'> |
| 143 |
<SuperuserAlert /> |
| 144 |
<PageSection hasBodyWrapper={false} className='ct-overview-header' padding={{ default: 'padding' }}> |
| 145 |
<div className='ct-overview-header-hostname'> |
| 146 |
<Title size={TitleSizes['3xl']} headingLevel='h1'> |
| 147 |
{this.hostname_text()} |
| 148 |
</Title> |
| 149 |
{this.state.hostnameData && |
| 150 |
this.state.hostnameData.OperatingSystemPrettyName && |
| 151 |
<div className="ct-overview-header-subheading" id="system_information_os_text">{cockpit.format(_("running $0"), this.state.hostnameData.OperatingSystemPrettyName)}</div>} |
| 152 |
</div> |
| 153 |
<div className='ct-overview-header-actions'> |
| 154 |
{ show_superuser && <SuperuserIndicator proxy={this.superuser} /> } |
| 155 |
{ "\n" } |
| 156 |
{ headerActions } |
| 157 |
</div> |
| 158 |
</PageSection> |
| 159 |
<PageSection hasBodyWrapper={false} variant={PageSectionVariants.default}> |
| 160 |
<Gallery className='ct-system-overview' hasGutter> |
| 161 |
<MotdCard /> |
| 162 |
<HealthCard /> |
| 163 |
<UsageCard /> |
| 164 |
<SystemInformationCard /> |
| 165 |
<ConfigurationCard hostname={this.hostname_text()} /> |
| 166 |
</Gallery> |
| 167 |
</PageSection> |
| 168 |
</Page> |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
function init() { |
| 174 |
cockpit.translate(); |
| 175 |
const root = createRoot(document.getElementById("overview")); |
| 176 |
root.render(<WithDialogs><OverviewPage /></WithDialogs>); |
| 177 |
} |
| 178 |
|
| 179 |
document.addEventListener("DOMContentLoaded", init); |