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