Debian Codegraph @

pkg/storaged/storage-controls.jsx

raw
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React, { useState } from 'react';
7
8 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
9 import { Dropdown, DropdownItem } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js';
10 import { MenuToggle } from '@patternfly/react-core/dist/esm/components/MenuToggle/index.js';
11 import { Tooltip, TooltipPosition } from "@patternfly/react-core/dist/esm/components/Tooltip/index.js";
12 import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js";
13 import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js";
14 import { BarsIcon, EllipsisVIcon } from '@patternfly/react-icons';
15
16 import cockpit from "cockpit";
17 import * as utils from "./utils.js";
18 import client from "./client.js";
19
20 import { dialog_open } from "./dialog.jsx";
21
22 const _ = cockpit.gettext;
23
24 /* StorageControl - a button or similar that triggers
25 * a privileged action.
26 *
27 * It can be disabled and will show a tooltip then. It will
28 * automatically disappear when the logged in user doesn't
29 * have permission.
30 *
31 * Properties:
32 *
33 * - excuse: If set, the button/link is disabled and will show the
34 * excuse in a tooltip.
35 */
36
37 class StorageControl extends React.Component {
38 render() {
39 const excuse = this.props.excuse;
40 if (!client.superuser.allowed)
41 return <div />;
42
43 if (excuse) {
44 return (
45 <Tooltip id="tip-storage" content={excuse}
46 position={this.props.excuse_placement || TooltipPosition.top}>
47 <span>
48 { this.props.content(excuse) }
49 </span>
50 </Tooltip>
51 );
52 } else {
53 return this.props.content();
54 }
55 }
56 }
57
58 function checked(callback, setSpinning, excuse) {
59 return function (event) {
60 if (!event)
61 return;
62
63 // only consider primary mouse button for clicks
64 if (event.type === 'click' && event.button !== 0)
65 return;
66
67 // only consider enter button for keyboard events
68 if (event.type === 'KeyDown' && event.key !== "Enter")
69 return;
70
71 event.stopPropagation();
72
73 if (excuse) {
74 dialog_open({
75 Title: _("Sorry"),
76 Body: excuse
77 });
78 return;
79 }
80
81 const promise = client.run(callback);
82 if (promise) {
83 if (setSpinning)
84 setSpinning(true);
85 promise.finally(() => {
86 if (setSpinning)
87 setSpinning(false);
88 });
89 promise.catch(function (error) {
90 console.warn(error.toString());
91 dialog_open({
92 Title: _("Error"),
93 Body: error.toString()
94 });
95 });
96 }
97 };
98 }
99
100 export const StorageButton = ({ id, kind, excuse, onClick, children, ariaLabel, spinner }) => {
101 const [spinning, setSpinning] = useState(false);
102
103 return <StorageControl excuse={excuse}
104 content={excuse => (
105 <Button id={id}
106 aria-label={ariaLabel}
107 onClick={checked(onClick, setSpinning)}
108 variant={kind || "secondary"}
109 isDisabled={!!excuse || (spinner && spinning)}
110 isLoading={spinner ? spinning : undefined}>
111 {children}
112 </Button>
113 )} />;
114 };
115
116 export const StorageLink = ({ id, excuse, onClick, children }) => (
117 <StorageControl excuse={excuse}
118 content={excuse => (
119 <Button onClick={checked(onClick)}
120 variant="link"
121 isInline
122 isDisabled={!!excuse}>
123 {children}
124 </Button>
125 )} />
126 );
127
128 // StorageOnOff - OnOff switch for asynchronous actions.
129 //
130
131 export class StorageOnOff extends React.Component {
132 constructor() {
133 super();
134 this.state = { promise: null };
135 }
136
137 render() {
138 const self = this;
139
140 function onChange(_event, val) {
141 const promise = self.props.onChange(val);
142 if (promise) {
143 promise.catch(error => {
144 dialog_open({
145 Title: _("Error"),
146 Body: error.toString()
147 });
148 })
149 .finally(() => { self.setState({ promise: null }) });
150 }
151
152 self.setState({ promise, promise_goal_state: val });
153 }
154
155 return (
156 <StorageControl excuse={this.props.excuse}
157 content={(excuse) => (
158 <Switch isChecked={this.state.promise
159 ? this.state.promise_goal_state
160 : this.props.state}
161 aria-label={this.props['aria-label']}
162 isDisabled={!!(excuse || this.state.promise)}
163 onChange={onChange} />
164 )} />
165 );
166 }
167 }
168
169 /* Render a usage bar showing props.stats[0] out of props.stats[1]
170 * bytes in use. If the ratio is above props.critical, the bar will be
171 * in a dangerous color.
172 */
173
174 export const StorageUsageBar = ({ stats, critical, block, offset, total, short }) => {
175 if (!stats)
176 return null;
177
178 const fraction = stats[0] / stats[1];
179 const off_fraction = offset / stats[1];
180 const total_fraction = total / stats[1];
181 const labelText = utils.format_fsys_usage(stats[0], stats[1]);
182
183 return (
184 <div>
185 <span className="usage-text pf-v6-u-text-nowrap">
186 {labelText}
187 </span>
188 <div className={"usage-bar" + (fraction > critical ? " usage-bar-danger" : "") + (short ? " usage-bar-short" : "")}
189 role="progressbar"
190 aria-valuemin={0} aria-valuemax={stats[1]} aria-valuenow={stats[0]}
191 aria-label={cockpit.format(_("Usage of $0"), block)}
192 aria-valuetext={labelText}>
193 <div className="usage-bar-indicator usage-bar-other" aria-hidden="true" style={{ width: total_fraction * 100 + "%" }} />
194 <div className="usage-bar-indicator" style={{ insetInlineStart: off_fraction * 100 + "%", width: fraction * 100 + "%" }} />
195 </div>
196 </div>);
197 };
198
199 /* Render a static size that goes well with a short StorageusageBar in
200 the same table column, and also works well with the tests.
201 */
202
203 export const StorageSize = ({ size }) => {
204 return (
205 <div>
206 <span className="usage-text pf-v6-u-text-nowrap">
207 {utils.fmt_size(size)}
208 </span>
209 <div className="usage-bar usage-bar-short usage-bar-empty" />
210 </div>);
211 };
212
213 export const StorageMenuItem = ({ onClick, danger, excuse, children, isDisabled }) => (
214 <DropdownItem className={danger && !excuse ? " delete-resource-dangerous" : ""}
215 description={excuse}
216 isDisabled={isDisabled || !!excuse}
217 onClick={checked(onClick, null, excuse)}>
218 {children}
219 </DropdownItem>
220 );
221
222 export const StorageBarMenu = ({ label, isKebab, menuItems }) => {
223 const [isOpen, setIsOpen] = useState(false);
224
225 if (!client.superuser.allowed)
226 return null;
227
228 const onToggleClick = (event) => {
229 setIsOpen(!isOpen);
230 };
231
232 const onSelect = (event) => {
233 setIsOpen(false);
234 };
235
236 const toggle = ref => (
237 <MenuToggle
238 ref={ref}
239 variant="plain"
240 onClick={onToggleClick}
241 isExpanded={isOpen}
242 aria-label={label}>
243 {isKebab ? <EllipsisVIcon /> : <Icon size="lg"><BarsIcon /></Icon>}
244 </MenuToggle>
245 );
246
247 return (
248 <Dropdown isOpen={isOpen}
249 onSelect={onSelect}
250 onOpenChange={isOpen => setIsOpen(isOpen)}
251 toggle={toggle}
252 popperProps={{ position: "right" }}
253 shouldFocusToggleOnSelect>
254 {menuItems}
255 </Dropdown>);
256 };