| 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 from 'react'; |
| 9 |
import ReactDOM from "react-dom"; |
| 10 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button"; |
| 11 |
import { |
| 12 |
CaretDownIcon, |
| 13 |
CaretUpIcon, |
| 14 |
EditIcon, |
| 15 |
MinusIcon, |
| 16 |
} from '@patternfly/react-icons'; |
| 17 |
import { PageSidebar } from "@patternfly/react-core/dist/esm/components/Page"; |
| 18 |
import { Tooltip } from "@patternfly/react-core/dist/esm/components/Tooltip"; |
| 19 |
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; |
| 20 |
|
| 21 |
import 'polyfills'; |
| 22 |
import { CockpitNav, CockpitNavItem } from "./nav.jsx"; |
| 23 |
import { encode_location } from "./util.jsx"; |
| 24 |
import { split_connection_string, Machine } from "./machines/machines"; |
| 25 |
import { add_host, edit_host, connect_host, HostModalState } from "./hosts_dialog.jsx"; |
| 26 |
|
| 27 |
import { ShellState } from "./state"; |
| 28 |
|
| 29 |
const _ = cockpit.gettext; |
| 30 |
|
| 31 |
interface HostsSelectorProps { |
| 32 |
children: React.ReactNode; |
| 33 |
} |
| 34 |
|
| 35 |
class HostsSelector extends React.Component { |
| 36 |
el: HTMLDivElement; |
| 37 |
props: HostsSelectorProps; |
| 38 |
|
| 39 |
constructor(props: HostsSelectorProps) { |
| 40 |
super(props); |
| 41 |
this.props = props; |
| 42 |
|
| 43 |
this.el = document.createElement("div"); |
| 44 |
this.el.className = "view-hosts"; |
| 45 |
} |
| 46 |
|
| 47 |
componentDidMount() { |
| 48 |
const hosts_sel = document.getElementById("nav-hosts"); |
| 49 |
hosts_sel?.appendChild(this.el); |
| 50 |
} |
| 51 |
|
| 52 |
componentWillUnmount() { |
| 53 |
const hosts_sel = document.getElementById("nav-hosts"); |
| 54 |
hosts_sel?.removeChild(this.el); |
| 55 |
} |
| 56 |
|
| 57 |
render() { |
| 58 |
const { children } = this.props; |
| 59 |
return ReactDOM.createPortal(children, this.el); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
const HostLine = ({ |
| 64 |
host, |
| 65 |
user |
| 66 |
} : { |
| 67 |
host: React.ReactNode, |
| 68 |
user: React.ReactNode |
| 69 |
}) => { |
| 70 |
return ( |
| 71 |
<> |
| 72 |
<span id="current-username" className="username">{user}</span> |
| 73 |
{user && <span className="at">@</span>} |
| 74 |
<span className="hostname">{host}</span> |
| 75 |
</> |
| 76 |
); |
| 77 |
}; |
| 78 |
|
| 79 |
// top left navigation element when host switching is disabled |
| 80 |
export const CockpitCurrentHost = ({ |
| 81 |
current_user, |
| 82 |
machine |
| 83 |
} : { |
| 84 |
current_user: string, |
| 85 |
machine: Machine |
| 86 |
}) => { |
| 87 |
return ( |
| 88 |
<div className="ct-switcher ct-switcher-localonly pf-m-dark"> |
| 89 |
<HostLine user={machine.user || current_user || ""} host={machine.label || ""} /> |
| 90 |
</div> |
| 91 |
); |
| 92 |
}; |
| 93 |
|
| 94 |
interface CockpitHostsProps { |
| 95 |
selector: string; |
| 96 |
host_modal_state: ReturnType<typeof HostModalState>; |
| 97 |
state: ShellState; |
| 98 |
} |
| 99 |
|
| 100 |
interface CockpitHostsState { |
| 101 |
opened: boolean; |
| 102 |
editing: boolean; |
| 103 |
current_user: string; |
| 104 |
current_key: string | undefined; |
| 105 |
} |
| 106 |
|
| 107 |
interface HostItem extends Machine { |
| 108 |
keyword?: string; |
| 109 |
} |
| 110 |
|
| 111 |
// full host switcher |
| 112 |
export class CockpitHosts extends React.Component { |
| 113 |
props: CockpitHostsProps; |
| 114 |
state: CockpitHostsState; |
| 115 |
|
| 116 |
constructor(props : CockpitHostsProps) { |
| 117 |
super(props); |
| 118 |
this.props = props; |
| 119 |
|
| 120 |
this.state = { |
| 121 |
opened: false, |
| 122 |
editing: false, |
| 123 |
current_user: "", |
| 124 |
current_key: props.state.current_machine?.key, |
| 125 |
}; |
| 126 |
|
| 127 |
this.toggleMenu = this.toggleMenu.bind(this); |
| 128 |
this.filterHosts = this.filterHosts.bind(this); |
| 129 |
this.onAddNewHost = this.onAddNewHost.bind(this); |
| 130 |
this.onEditHosts = this.onEditHosts.bind(this); |
| 131 |
this.onHostEdit = this.onHostEdit.bind(this); |
| 132 |
this.onRemove = this.onRemove.bind(this); |
| 133 |
} |
| 134 |
|
| 135 |
componentDidMount() { |
| 136 |
cockpit.user().then(user => { |
| 137 |
this.setState({ current_user: user.name || "" }); |
| 138 |
}).catch(exc => console.log(exc)); |
| 139 |
} |
| 140 |
|
| 141 |
static getDerivedStateFromProps(nextProps: CockpitHostsProps, prevState: CockpitHostsState) { |
| 142 |
if (nextProps.state.current_machine?.key !== prevState.current_key) { |
| 143 |
document.getElementById(nextProps.selector)?.classList.toggle("interact", false); |
| 144 |
return { |
| 145 |
current_key: nextProps.state.current_machine?.key, |
| 146 |
opened: false, |
| 147 |
editing: false, |
| 148 |
}; |
| 149 |
} |
| 150 |
return null; |
| 151 |
} |
| 152 |
|
| 153 |
toggleMenu() { |
| 154 |
document.getElementById(this.props.selector)?.classList.toggle("interact", !this.state.opened); |
| 155 |
|
| 156 |
this.setState((s: CockpitHostsState) => { |
| 157 |
return ( |
| 158 |
{ |
| 159 |
opened: !s.opened, |
| 160 |
editing: false, |
| 161 |
} |
| 162 |
); |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
async onAddNewHost() { |
| 167 |
await add_host(this.props.host_modal_state, this.props.state); |
| 168 |
} |
| 169 |
|
| 170 |
async onHostEdit(machine: Machine) { |
| 171 |
await edit_host(this.props.host_modal_state, this.props.state, machine); |
| 172 |
} |
| 173 |
|
| 174 |
async onHostSwitch(machine: Machine) { |
| 175 |
const { state, host_modal_state } = this.props; |
| 176 |
|
| 177 |
const connection_string = await connect_host(host_modal_state, state, machine); |
| 178 |
if (connection_string) { |
| 179 |
const parts = split_connection_string(connection_string); |
| 180 |
state.jump({ host: parts.address }); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
onEditHosts() { |
| 185 |
this.setState((s: CockpitHostsState) => { return { editing: !s.editing } }); |
| 186 |
} |
| 187 |
|
| 188 |
onRemove(event: React.MouseEvent, machine: Machine) { |
| 189 |
const { state } = this.props; |
| 190 |
const { current_machine } = state; |
| 191 |
|
| 192 |
event.preventDefault(); |
| 193 |
|
| 194 |
if (current_machine === machine) { |
| 195 |
// Removing machine underneath ourself - jump to localhost |
| 196 |
state.jump({ host: "localhost" }); |
| 197 |
} |
| 198 |
|
| 199 |
if (state.machines.list.length <= 2) |
| 200 |
this.setState({ editing: false }); |
| 201 |
state.machines.change(machine.key, { visible: false }); |
| 202 |
} |
| 203 |
|
| 204 |
filterHosts(host: Machine, term: string): HostItem | null { |
| 205 |
if (!term) |
| 206 |
return host; |
| 207 |
const new_host: HostItem = Object.assign({}, host); |
| 208 |
term = term.toLowerCase(); |
| 209 |
|
| 210 |
if (host.label.toLowerCase().indexOf(term) > -1) |
| 211 |
new_host.keyword = host.label.toLowerCase(); |
| 212 |
|
| 213 |
const user = host.user || this.state.current_user; |
| 214 |
if (user.toLowerCase().indexOf(term) > -1) |
| 215 |
new_host.keyword = user.toLowerCase() + " @"; |
| 216 |
|
| 217 |
if (new_host.keyword) |
| 218 |
return new_host; |
| 219 |
return null; |
| 220 |
} |
| 221 |
|
| 222 |
// HACK: using HTML rather than Select PF4 component as: |
| 223 |
// 1. It does not change the arrow when opened/closed |
| 224 |
// 2. It closes the dropdown even when trying to search... and cannot tell it not to |
| 225 |
render() { |
| 226 |
const { state } = this.props; |
| 227 |
const { current_machine } = state; |
| 228 |
|
| 229 |
const editing = this.state.editing; |
| 230 |
const groups = [{ |
| 231 |
name: _("Hosts"), |
| 232 |
items: state.machines.list, |
| 233 |
}]; |
| 234 |
const render = (m: HostItem, term: string) => <CockpitNavItem |
| 235 |
term={term} |
| 236 |
keyword={m.keyword || ""} |
| 237 |
href={encode_location({ host: m.address })} |
| 238 |
active={m === current_machine} |
| 239 |
key={m.key} |
| 240 |
name={m.label} |
| 241 |
header={(m.user ? m.user : this.state.current_user) + " @"} |
| 242 |
status={m.state === "failed" ? { type: "error", title: _("Connection error") } : null} |
| 243 |
className={m.state || ""} |
| 244 |
onClick={() => this.onHostSwitch(m)} |
| 245 |
actions={ |
| 246 |
editing && <> |
| 247 |
<Tooltip content={_("Edit")} position="right"> |
| 248 |
<Button icon={<EditIcon />} isDisabled={m.address === "localhost"} className="nav-action" hidden={!editing} onClick={_e => this.onHostEdit(m)} key={m.label + "edit"} variant="secondary" /> |
| 249 |
</Tooltip> |
| 250 |
<Tooltip content={_("Remove")} position="right"> |
| 251 |
<Button icon={<MinusIcon />} isDisabled={m.address === "localhost"} onClick={e => this.onRemove(e, m)} className="nav-action" hidden={!editing} key={m.label + "remove"} variant="danger" /> |
| 252 |
</Tooltip> |
| 253 |
</> |
| 254 |
} |
| 255 |
/>; |
| 256 |
const label = current_machine?.label || ""; |
| 257 |
const user = current_machine?.user || this.state.current_user; |
| 258 |
|
| 259 |
const add_host_action = <Button variant="secondary" onClick={this.onAddNewHost}>{_("Add new host")}</Button>; |
| 260 |
|
| 261 |
return ( |
| 262 |
<div className="ct-switcher"> |
| 263 |
<div className="pf-v6-c-select pf-m-dark"> |
| 264 |
<button onClick={this.toggleMenu} id="host-toggle" aria-labelledby="host-toggle" aria-expanded={(this.state.opened ? "true" : "false")} aria-haspopup="listbox" type="button" className="ct-nav-toggle pf-v6-c-select__toggle pf-v6-c-menu-toggle pf-m-plain"> |
| 265 |
<span className="pf-v6-c-button__text desktop_v"> |
| 266 |
<span className="pf-v6-c-select__toggle-wrapper"> |
| 267 |
<span className="pf-v6-c-select__toggle-text"> |
| 268 |
<HostLine user={user} host={label} /> |
| 269 |
</span> |
| 270 |
</span> |
| 271 |
</span> |
| 272 |
<Icon size="xl" className="mobile_v"> |
| 273 |
<CaretUpIcon |
| 274 |
className={`pf-v6-c-select__toggle-arrow ${this.state.opened ? 'clicked' : ''}`} |
| 275 |
aria-hidden="true" |
| 276 |
/> |
| 277 |
</Icon> |
| 278 |
<span className="pf-v6-c-select__toggle-wrapper mobile_v"> |
| 279 |
{_("Host")} |
| 280 |
</span> |
| 281 |
<Icon size="xl" className="desktop_v"> |
| 282 |
<CaretDownIcon |
| 283 |
className={`pf-v6-c-select__toggle-arrow ${this.state.opened ? 'clicked' : ''}`} |
| 284 |
aria-hidden="true" |
| 285 |
/> |
| 286 |
</Icon> |
| 287 |
</button> |
| 288 |
</div> |
| 289 |
|
| 290 |
{ this.state.opened && |
| 291 |
<HostsSelector> |
| 292 |
<PageSidebar className={"sidebar-hosts" + (this.state.editing ? " edit-hosts" : "")}> |
| 293 |
<CockpitNav |
| 294 |
selector={this.props.selector} |
| 295 |
groups={groups} |
| 296 |
item_render={render} |
| 297 |
sorting={(_a, _b) => 1} |
| 298 |
filtering={this.filterHosts} |
| 299 |
current={label} |
| 300 |
jump={() => console.error("internal error: jump not supported in hosts selector")} |
| 301 |
/> |
| 302 |
<div className="nav-hosts-actions"> |
| 303 |
{state.machines.list.length > 1 && <Button variant="secondary" onClick={this.onEditHosts}>{this.state.editing ? _("Stop editing hosts") : _("Edit hosts")}</Button>} |
| 304 |
{add_host_action} |
| 305 |
</div> |
| 306 |
</PageSidebar> |
| 307 |
</HostsSelector> |
| 308 |
} |
| 309 |
</div> |
| 310 |
); |
| 311 |
} |
| 312 |
} |