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