Debian Codegraph @

pkg/shell/shell.tsx

raw
1 /*
2 * Copyright (C) 2024 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7
8 import React, { useEffect } from 'react';
9 import { createRoot } from "react-dom/client";
10
11 import {
12 Modal, ModalBody, ModalFooter, ModalHeader
13 } from '@patternfly/react-core/dist/esm/components/Modal/index.js';
14 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
15
16 import { WithDialogs } from "dialogs.jsx";
17 import { useInit, useEvent, useOn, useLoggedInUser } from "hooks";
18
19 import { TopNav } from "./topnav.jsx";
20 import { SidebarToggle, PageNav } from "./nav.jsx";
21 import { CockpitHosts, CockpitCurrentHost } from "./hosts.jsx";
22 import { HostModalState, HostModal, connect_host } from "./hosts_dialog.jsx";
23 import { Frames } from "./frames.jsx";
24 import { EarlyFailure, Disconnected, MachineTroubleshoot } from "./failures.jsx";
25
26 import { ShellState } from "./state.jsx";
27 import { get_session_controller } from "./session";
28
29 import { SkipToContent } from '@patternfly/react-core/dist/esm/components/SkipToContent/index.js';
30
31 import 'cockpit-dark-theme'; // once per page
32
33 import '../lib/patternfly/patternfly-6-cockpit.scss';
34 import "./shell.scss";
35
36 const _ = cockpit.gettext;
37
38 interface SkipLinkProps {
39 focus_id: string;
40 children?: React.ReactNode
41 }
42
43 // We create a wrapper specifically to handle edge-cases like headless browsers
44 // not getting focus when clicking an anchor to a heading. We can also scroll
45 // it into view in case it wouldn't do that before - just to be safe.
46 const SkipLink = ({ focus_id, children }: SkipLinkProps) => {
47 return (
48 <SkipToContent
49 href={`#${focus_id}`}
50 onClick={ev => {
51 const el = document.getElementById(focus_id);
52 el?.focus();
53 el?.scrollIntoView();
54 ev.preventDefault();
55 }}>
56 {children}
57 </SkipToContent>
58 );
59 };
60
61 const SessionCountdownModal = () => {
62 const controller = get_session_controller();
63 useOn(controller, "changed");
64
65 controller.inhibit_activity_reporting(controller.countdown > 0);
66
67 if (controller.countdown <= 0)
68 return null;
69
70 return (
71 <Modal isOpen position="top" variant="medium"
72 id="session-timeout-modal">
73 <ModalHeader title={_("Session is about to expire")} />
74 <ModalBody>
75 { cockpit.format(_("You will be logged out in $0 seconds."), controller.countdown) }
76 </ModalBody>
77 <ModalFooter>
78 <Button variant='primary'
79 onClick={() => controller.continue_session()}
80 >
81 {_("Continue session")}
82 </Button>
83 </ModalFooter>
84 </Modal>
85 );
86 };
87
88 const Shell = () => {
89 const current_user = useLoggedInUser()?.name || "";
90 const state = useInit(() => new ShellState());
91 const session_controller = get_session_controller();
92 const host_modal_state = useInit(() => HostModalState());
93
94 useOn(state, "update");
95 useOn(session_controller, "changed");
96 useEvent(host_modal_state, "changed");
97
98 useEffect(() => {
99 return state.on("connect", () => {
100 connect_host(host_modal_state, state, state.current_machine);
101 });
102 }, [host_modal_state, state]);
103
104 const {
105 ready,
106 config,
107
108 current_machine,
109 current_manifest_item,
110 } = state;
111
112 const { countdown, problem } = session_controller;
113
114 if (problem && !ready)
115 return <EarlyFailure />;
116
117 if (!ready)
118 return null;
119
120 cockpit.assert(current_machine && current_manifest_item);
121
122 const title_parts = [];
123 if (current_manifest_item.label)
124 title_parts.push(current_manifest_item.label);
125 title_parts.push((current_machine.user || current_user) + "@" + current_machine.label);
126 document.title = title_parts.join(" - ");
127
128 if (countdown > 0)
129 document.title = "(" + countdown + ") " + document.title;
130
131 document.documentElement.lang = config.language;
132 if (config.language_direction)
133 document.documentElement.dir = config.language_direction;
134
135 let failure = null;
136 if (problem) {
137 failure = <Disconnected problem={problem} />;
138 } else if (current_machine.state != "connected") {
139 failure = <MachineTroubleshoot machine={current_machine}
140 onClick={() => connect_host(host_modal_state, state, current_machine)} />;
141 }
142
143 return (
144 <div id="main" className="page"
145 style={
146 {
147 '--ct-color-host-accent': (current_machine.address == "localhost" ? undefined : current_machine.color)
148 } as React.CSSProperties
149 }>
150
151 <SkipLink focus_id="content">{_("Skip to content")}</SkipLink>
152 <SkipLink focus_id="hosts-sel">{_("Skip main navigation")}</SkipLink>
153
154 <div id="sidebar-toggle" className="pf-v6-c-select pf-m-dark sidebar-toggle">
155 <SidebarToggle />
156 </div>
157
158 <div id="nav-system" className="area-ct-subnav nav-system-menu sidebar interact">
159 <nav id="host-apps" className="host-apps">
160 <PageNav state={state} />
161 </nav>
162 </div>
163
164 <nav id="hosts-sel" className="navbar navbar-default navbar-pf navbar-pf-vertical" tabIndex={-1}>
165 { config.host_switcher_enabled
166 ? <CockpitHosts state={state} host_modal_state={host_modal_state} selector="nav-hosts" />
167 : <CockpitCurrentHost current_user={current_user} machine={current_machine} />
168 }
169 </nav>
170
171 <div id="nav-hosts" className="area-ct-subnav nav-hosts-menu sidebar" />
172
173 <div id="topnav" className="header">
174 <TopNav state={state} />
175 </div>
176
177 <Frames hidden={!!failure} state={state} />
178
179 { failure &&
180 <div id="failure-content" className="area-ct-content" role="main" tabIndex={-1}>
181 { failure }
182 </div>
183 }
184
185 <SessionCountdownModal />
186 <HostModal state={host_modal_state} machines={state.machines} />
187
188 </div>);
189 };
190
191 interface ShellWindow extends Window {
192 features?: object;
193 }
194
195 function init() {
196 cockpit.translate();
197
198 /* Give us a name. This used to (maybe) indicate at some point to
199 * child frames that we are a cockpit1 router frame. But they
200 * actually check for a "cockpit1:" prefix of their own name.
201 */
202 window.name = "cockpit1";
203
204 /* Tell the pages about our features. */
205 (window as ShellWindow).features = {
206 navbar_is_for_current_machine: true
207 };
208
209 function follow(arg: unknown) {
210 /* A promise of some sort */
211 if (arguments.length == 1 && arg && typeof arg == "object" && "then" in arg && typeof arg.then == "function") {
212 arg.then(function(...args: unknown[]) { console.log(...args) },
213 function(...args: unknown[]) { console.error(...args) });
214 if ("stream" in arg && typeof arg.stream == "function")
215 arg.stream(function(...args: unknown[]) { console.log(...args) });
216 }
217 }
218
219 let zz_value: unknown;
220
221 /* For debugging in the browser console */
222 Object.defineProperties(window, {
223 cockpit: { value: cockpit },
224 zz: {
225 get: function() { return zz_value },
226 set: function(val) { zz_value = val; follow(val) }
227 }
228 });
229
230 const root = createRoot(document.getElementById("shell")!);
231 root.render(<WithDialogs><Shell /></WithDialogs>);
232 }
233
234 document.addEventListener("DOMContentLoaded", init);