Debian Codegraph @

pkg/networkmanager/network-interface-members.jsx

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 import cockpit from "cockpit";
6 import React, { useState, useContext } from "react";
7 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
8 import { Card, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js';
9 import { Dropdown, DropdownItem, DropdownList } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js';
10 import { MenuToggle } from "@patternfly/react-core/dist/esm/components/MenuToggle";
11 import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js";
12 import { MinusIcon } from '@patternfly/react-icons';
13
14 import { ListingTable } from "cockpit-components-table.jsx";
15 import { ModelContext } from './model-context.jsx';
16 import { useEvent } from "hooks";
17
18 import {
19 connection_settings,
20 device_state_text,
21 free_member_connection,
22 is_interesting_interface,
23 set_member,
24 show_unexpected_error,
25 syn_click,
26 with_checkpoint,
27 is_managed,
28 } from './interfaces.js';
29 import { fmt_to_fragments } from 'utils.jsx';
30
31 const _ = cockpit.gettext;
32
33 export const NetworkInterfaceMembers = ({
34 members,
35 memberIfaces,
36 interfaces,
37 iface,
38 usage_monitor,
39 privileged
40 }) => {
41 const model = useContext(ModelContext);
42 const [isOpen, setIsOpen] = useState(false);
43 useEvent(usage_monitor.grid, "notify");
44
45 function renderMemberRows() {
46 const rows = [];
47
48 members.forEach(iface => {
49 const member_con = iface.MainConnection;
50 const dev = iface.Device;
51 const isActive = (dev && dev.State == 100 && dev.Carrier === true);
52 const onoff = (
53 <Switch
54 aria-label={cockpit.format(_("Switch off $0"), iface.Name)}
55 isDisabled={!privileged}
56 isChecked={!!(dev && dev.ActiveConnection)}
57 onChange={(_event, val) => {
58 if (val) {
59 with_checkpoint(
60 model,
61 () => member_con.activate(dev).catch(show_unexpected_error),
62 {
63 devices: dev ? [dev] : [],
64 fail_text: fmt_to_fragments(_("Switching on $0 will break the connection to the server, and will make the administration UI unavailable."), <b>{iface.Name}</b>),
65 anyway_text: cockpit.format(_("Switch on $0"), iface.Name)
66 });
67 } else if (dev) {
68 with_checkpoint(
69 model,
70 () => dev.disconnect().catch(show_unexpected_error),
71 {
72 devices: [dev],
73 fail_text: fmt_to_fragments(_("Switching off $0 will break the connection to the server, and will make the administration UI unavailable."), <b>{iface.Name}</b>),
74 anyway_text: cockpit.format(_("Switch off $0"), iface.Name)
75 });
76 }
77 } } />
78 );
79
80 const row = ({
81 columns: [
82 { title: (!dev || is_managed(dev)) ? <Button variant="link" isInline onClick={() => cockpit.location.go([iface.Name])}>{iface.Name}</Button> : iface.Name },
83 // Will add traffic info right after
84 {
85 title: (
86 <div className="btn-group">
87 {onoff}
88 {privileged && <Button icon={<MinusIcon />} variant="secondary"
89 size="sm"
90 onClick={syn_click(model, () => {
91 with_checkpoint(
92 model,
93 () => free_member_connection(member_con).catch(show_unexpected_error),
94 {
95 devices: dev ? [dev] : [],
96 fail_text: fmt_to_fragments(_("Removing $0 will break the connection to the server, and will make the administration UI unavailable."), <b>{iface.Name}</b>),
97 anyway_text: cockpit.format(_("Remove $0"), iface.Name),
98 hack_does_add_or_remove: true
99 });
100 return false;
101 })} />}
102 </div>
103 ),
104 props: { className: "pf-v6-c-table__action" }
105 },
106 ],
107 props: {
108 key: iface.Name,
109 "data-interface": encodeURIComponent(iface.Name),
110 "data-sample-id": isActive ? encodeURIComponent(iface.Name) : null,
111 "data-row-id": iface.Name,
112 }
113 });
114
115 if (isActive) {
116 const samples = usage_monitor.samples[iface.Name];
117 row.columns.splice(1, 0, { title: samples ? cockpit.format_bits_per_sec(samples[1][0] * 8) : "" });
118 row.columns.splice(2, 0, { title: samples ? cockpit.format_bits_per_sec(samples[0][0] * 8) : "" });
119 } else {
120 row.columns.splice(1, 0, { title: device_state_text(dev) });
121 row.columns.splice(1, 0, { title: "" });
122 }
123
124 rows.push(row);
125 });
126 return rows;
127 }
128
129 const main_connection = iface.MainConnection;
130 const cs = iface.MainConnection && connection_settings(iface.MainConnection);
131 const dropdownItems = [];
132
133 interfaces.forEach(i => {
134 if (is_interesting_interface(i) && !memberIfaces[i.Name] && i != iface) {
135 const onClick = () => {
136 with_checkpoint(
137 model,
138 () => {
139 return set_member(model, main_connection, main_connection.Settings,
140 cs.type, i.Name, true)
141 .catch(show_unexpected_error);
142 },
143 {
144 devices: i.Device ? [i.Device] : [],
145 fail_text: fmt_to_fragments(_("Adding $0 will break the connection to the server, and will make the administration UI unavailable."), <b>{i.Name}</b>),
146 anyway_text: cockpit.format(_("Add $0"), i.Name),
147 hack_does_add_or_remove: true
148 }
149 );
150 };
151
152 dropdownItems.push(
153 <DropdownItem onClick={syn_click(model, onClick)}
154 key={"add-member-" + i.Name}
155 component="button">
156 {i.Name}
157 </DropdownItem>
158 );
159 }
160 });
161
162 // We want to show a Tooltip but that isn't supported by PatternFly.
163 // https://github.com/patternfly/patternfly-react/issues/12500
164 if (dropdownItems.length === 0) {
165 dropdownItems.push(
166 <DropdownItem isDisabled isAriaDisabled>
167 {_("No interfaces available")}
168 </DropdownItem>
169 );
170 }
171
172 const add_btn = (
173 <Dropdown onSelect={() => setIsOpen(false)}
174 toggle={(toggleRef) => (
175 <MenuToggle id="add-member" ref={toggleRef} onClick={() => setIsOpen(!isOpen)}>
176 {_("Add member")}
177 </MenuToggle>
178 )}
179 isOpen={isOpen}
180 popperProps={{ position: "right" }}
181 >
182 <DropdownList>
183 {dropdownItems}
184 </DropdownList>
185 </Dropdown>
186 );
187
188 return (
189 <Card isPlain id="network-interface-members" className="network-interface-members">
190 <CardHeader actions={{ actions: add_btn }}>
191 <CardTitle component="h2">{_("Interface members")}</CardTitle>
192 </CardHeader>
193 <ListingTable aria-label={_("Interface members")}
194 className="networking-interface-members"
195 variant='compact'
196 columns={[
197 { title: (cs && cs.type == "bond") ? _("Interfaces") : _("Ports"), props: { width: 25 } },
198 { title: _("Sending"), props: { width: 25 } },
199 { title: _("Receiving"), props: { width: 25 } },
200 { title: "" },
201 ]}
202 rows={renderMemberRows()} />
203 </Card>
204 );
205 };