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