| 1 |
/* |
| 2 |
* Copyright (C) 2023 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import React, { useState, useRef } from "react"; |
| 8 |
import client from "./client"; |
| 9 |
import { useEvent } from "hooks.js"; |
| 10 |
|
| 11 |
import { AlertGroup } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 12 |
import { Card, CardHeader, CardTitle, CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js"; |
| 13 |
import { Divider } from '@patternfly/react-core/dist/esm/components/Divider/index.js'; |
| 14 |
import { DropdownGroup, DropdownList } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js'; |
| 15 |
import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 16 |
import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js"; |
| 17 |
import { Bullseye } from "@patternfly/react-core/dist/esm/layouts/Bullseye/index.js"; |
| 18 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 19 |
import { Table, Thead, Tbody, Tr, Th, Td } from '@patternfly/react-table'; |
| 20 |
import { EmptyState, EmptyStateBody } from "@patternfly/react-core/dist/esm/components/EmptyState/index.js"; |
| 21 |
import { ExclamationTriangleIcon, ExclamationCircleIcon, HelpIcon } from "@patternfly/react-icons"; |
| 22 |
import { Page, PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 23 |
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js"; |
| 24 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 25 |
import { DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; |
| 26 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 27 |
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon"; |
| 28 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover"; |
| 29 |
|
| 30 |
import { |
| 31 |
decode_filename, block_short_name, fmt_size, |
| 32 |
reset_available_spaces |
| 33 |
} from "./utils.js"; |
| 34 |
import { StorageButton, StorageBarMenu, StorageMenuItem, StorageSize } from "./storage-controls.jsx"; |
| 35 |
import { MultipathAlert } from "./multipath.jsx"; |
| 36 |
import { JobsPanel } from "./jobs-panel.jsx"; |
| 37 |
import { Truncate } from "../lib/cockpit-components-truncate.jsx"; |
| 38 |
|
| 39 |
const _ = cockpit.gettext; |
| 40 |
|
| 41 |
/* PAGES and CARDS |
| 42 |
|
| 43 |
This is the central hub of the Storage application. It implements a |
| 44 |
couple of concepts: "page", "card", "crossref", and "action". It |
| 45 |
also implements the React components that construct the actual UI |
| 46 |
out of them. |
| 47 |
|
| 48 |
On every change of the client, a tree of pages is constructed. Each |
| 49 |
page has a list of cards on it. The cards each have a list of |
| 50 |
actions. In addition to that, there are a few crossrefs (cross |
| 51 |
references) between pages that don't follow the tree structure. |
| 52 |
|
| 53 |
The tree of pages starts with "make_overview_page" in |
| 54 |
overview/overview.jsx. That function creates the data structures |
| 55 |
that represent the overview page (using functions exported from |
| 56 |
this file), and is also responsible for kicking off the creation of |
| 57 |
its child pages. (And each page construction function of course |
| 58 |
also creates the cards and actions for that page). |
| 59 |
|
| 60 |
Pages can be shown by themselves, or be part of various tables in |
| 61 |
other pages. For example, each row in the table in the overview |
| 62 |
page represents another page, and clicking on it will navigate to |
| 63 |
that page. (In fact, the overview table shows the whole tree. Other |
| 64 |
pages show subsets of the tree in their tables.) |
| 65 |
|
| 66 |
Most of the complications with pages and cards are there to support |
| 67 |
putting pages into tables. Without having to do that, we wouldn't |
| 68 |
need the pages and cards abstractions at all. |
| 69 |
|
| 70 |
A page derives its "ID", "Type", "Location", and "Size" properties |
| 71 |
from its cards in various ad-hoc and arguably too complex |
| 72 |
ways. This is seems necessary since the cards that end up together |
| 73 |
on a page can vary significantly depending on the actual storage |
| 74 |
configuration of the machine. |
| 75 |
|
| 76 |
For example, a MDRAID device might have a encrypted LVM2 physical |
| 77 |
volume directly on it, or it might have a partition table with a |
| 78 |
PV, in which case the card for the PV appears on another page |
| 79 |
together with a card for the partition. |
| 80 |
|
| 81 |
Cards are constructed with a call like this: |
| 82 |
|
| 83 |
const card = new_card({ |
| 84 |
next: next_card, |
| 85 |
title: ..., |
| 86 |
component: CardComponent, |
| 87 |
props: { react_props... } |
| 88 |
}); |
| 89 |
|
| 90 |
A page is constructed with a call like this: |
| 91 |
|
| 92 |
new_page(parent_page, first_card, { options... }); |
| 93 |
|
| 94 |
A call to "new_page" will implicitly build the tree by inserting |
| 95 |
the new_page into the children of "parent_page". |
| 96 |
|
| 97 |
The main React component for this application is StoragePage. It |
| 98 |
will find the correct page for the browser URL and display it, |
| 99 |
together with all the trimmings like the breadcrumb trail. |
| 100 |
|
| 101 |
The page will be filled with the React components specified by its |
| 102 |
cards. Such a component should use StorageCard to get the standard |
| 103 |
look of all cards, and to work well with the test helpers. |
| 104 |
|
| 105 |
Tables of pages are handled by the PageTable component exported |
| 106 |
from this file or, more likely, its specialized version |
| 107 |
ChildrenTable. |
| 108 |
|
| 109 |
A ChildrenTable shows all direct and indirect children of a given |
| 110 |
page, by using a PageTable appropriately. A raw PageTable can also |
| 111 |
show cross references. |
| 112 |
|
| 113 |
A cross reference associates a page with an arbitrary key. For |
| 114 |
example, a page that represents a LVM2 physical volume will |
| 115 |
associate itself with the DBusProxy for its volume group. The page |
| 116 |
for the volume group will then retrieve all pages associated with |
| 117 |
its DBusProxy and put them into a table to show all its physical |
| 118 |
volumes. |
| 119 |
|
| 120 |
When a page appears in such a crossref table, it has dedicated |
| 121 |
actions, a dedicated size, and generally looks different from when |
| 122 |
it appears in a "normal" children's table. This is another source of |
| 123 |
complication of how cards and pages interact. |
| 124 |
*/ |
| 125 |
|
| 126 |
let pages = null; |
| 127 |
let crossrefs = null; |
| 128 |
|
| 129 |
export const PAGE_CATEGORY_PHYSICAL = 1; |
| 130 |
export const PAGE_CATEGORY_VIRTUAL = 2; |
| 131 |
export const PAGE_CATEGORY_NETWORK = 3; |
| 132 |
|
| 133 |
export function reset_pages() { |
| 134 |
pages = new Map(); |
| 135 |
crossrefs = new Map(); |
| 136 |
reset_available_spaces(); |
| 137 |
} |
| 138 |
|
| 139 |
function name_from_card(card) { |
| 140 |
if (!card) |
| 141 |
return null; |
| 142 |
return name_from_card(card.next) || card.page_name; |
| 143 |
} |
| 144 |
|
| 145 |
function icon_from_card(card) { |
| 146 |
if (!card) |
| 147 |
return null; |
| 148 |
return icon_from_card(card.next) || card.page_icon; |
| 149 |
} |
| 150 |
|
| 151 |
function category_from_card(card) { |
| 152 |
if (!card) |
| 153 |
return null; |
| 154 |
return category_from_card(card.next) || card.page_category; |
| 155 |
} |
| 156 |
|
| 157 |
function key_from_card(card) { |
| 158 |
if (!card) |
| 159 |
return null; |
| 160 |
return key_from_card(card.next) || card.page_key; |
| 161 |
} |
| 162 |
|
| 163 |
function location_from_card(card) { |
| 164 |
if (!card) |
| 165 |
return null; |
| 166 |
return location_from_card(card.next) || card.page_location; |
| 167 |
} |
| 168 |
|
| 169 |
function size_from_card(card) { |
| 170 |
if (!card) |
| 171 |
return null; |
| 172 |
if (card.page_size) |
| 173 |
return card.page_size; |
| 174 |
return size_from_card(card.next); |
| 175 |
} |
| 176 |
|
| 177 |
export function new_page(parent, card, options) { |
| 178 |
const page = { |
| 179 |
location: location_from_card(card), |
| 180 |
name: name_from_card(card), |
| 181 |
icon: icon_from_card(card), |
| 182 |
category: category_from_card(card), |
| 183 |
key: key_from_card(card), |
| 184 |
parent, |
| 185 |
children: [], |
| 186 |
card, |
| 187 |
options: options || {}, |
| 188 |
}; |
| 189 |
page.columns = [ |
| 190 |
card.title, |
| 191 |
card.location, |
| 192 |
size_from_card(card), |
| 193 |
]; |
| 194 |
if (parent) |
| 195 |
parent.children.push(page); |
| 196 |
while (card) { |
| 197 |
card.page = page; |
| 198 |
card = card.next; |
| 199 |
} |
| 200 |
if (page.location) { |
| 201 |
pages.set(JSON.stringify(page.location), page); |
| 202 |
if (page.location.length == 0) { |
| 203 |
// This is the Overview page. Make it the parent of the |
| 204 |
// special "not found" page (but don't make the "not |
| 205 |
// found" page a child of the Overview...) |
| 206 |
not_found_page.parent = page; |
| 207 |
} |
| 208 |
} |
| 209 |
return page; |
| 210 |
} |
| 211 |
|
| 212 |
export function new_card({ |
| 213 |
title, location, next, |
| 214 |
type_extra, id_extra, |
| 215 |
page_name, page_icon, page_category, page_key, page_location, page_size, |
| 216 |
page_block, |
| 217 |
for_summary, |
| 218 |
has_warning, has_danger, job_path, |
| 219 |
component, props, |
| 220 |
actions, |
| 221 |
}) { |
| 222 |
if (page_block) { |
| 223 |
page_location = [block_location(page_block)]; |
| 224 |
page_name = block_short_name(page_block); |
| 225 |
page_size = page_block.Size; |
| 226 |
job_path = page_block.path; |
| 227 |
} |
| 228 |
return { |
| 229 |
title, |
| 230 |
location, |
| 231 |
next, |
| 232 |
type_extra, |
| 233 |
page_name, |
| 234 |
page_icon, |
| 235 |
page_category: page_category || PAGE_CATEGORY_PHYSICAL, |
| 236 |
page_key, |
| 237 |
page_location, |
| 238 |
page_size, |
| 239 |
for_summary, |
| 240 |
component, |
| 241 |
props, |
| 242 |
has_warning, |
| 243 |
has_danger, |
| 244 |
job_path, |
| 245 |
actions: actions ? actions.filter(a => !!a) : null, |
| 246 |
id_extra, |
| 247 |
}; |
| 248 |
} |
| 249 |
|
| 250 |
export function register_crossref(crossref) { |
| 251 |
const val = crossrefs.get(crossref.key) || []; |
| 252 |
crossref.actions = crossref.actions ? crossref.actions.filter(a => !!a) : null; |
| 253 |
val.push(crossref); |
| 254 |
crossrefs.set(crossref.key, val); |
| 255 |
} |
| 256 |
|
| 257 |
export function get_crossrefs(key) { |
| 258 |
return crossrefs.get(key); |
| 259 |
} |
| 260 |
|
| 261 |
/* Getting the page for a navigation location. |
| 262 |
* |
| 263 |
* We have a special "not found" page that is returned when there is |
| 264 |
* no real page at the given location. |
| 265 |
*/ |
| 266 |
|
| 267 |
const NotFoundCard = ({ card }) => { |
| 268 |
return ( |
| 269 |
<StorageCard card={card}> |
| 270 |
<CardBody> |
| 271 |
{_("Not found")} |
| 272 |
</CardBody> |
| 273 |
</StorageCard>); |
| 274 |
}; |
| 275 |
|
| 276 |
const not_found_page = new_page(null, new_card({ page_name: _("Not found"), component: NotFoundCard })); |
| 277 |
|
| 278 |
export function get_page_from_location(location) { |
| 279 |
return pages?.get(JSON.stringify(location)) || not_found_page; |
| 280 |
} |
| 281 |
|
| 282 |
/* Common UI things |
| 283 |
*/ |
| 284 |
|
| 285 |
export function navigate_away_from_card(card) { |
| 286 |
if (!card) |
| 287 |
return; |
| 288 |
|
| 289 |
const loc = cockpit.location; |
| 290 |
const page = card.page; |
| 291 |
if (page.parent && JSON.stringify(loc.path) == JSON.stringify(page.location)) |
| 292 |
loc.go(page.parent.location); |
| 293 |
} |
| 294 |
|
| 295 |
export function navigate_to_new_card_location(card, location) { |
| 296 |
if (!card) |
| 297 |
return; |
| 298 |
|
| 299 |
const loc = cockpit.location; |
| 300 |
const page = card.page; |
| 301 |
if (JSON.stringify(loc.path) == JSON.stringify(page.location)) |
| 302 |
loc.go(location); |
| 303 |
} |
| 304 |
|
| 305 |
function make_menu_item(action) { |
| 306 |
return <StorageMenuItem key={action.title} onClick={() => action.action(true)} |
| 307 |
danger={action.danger} excuse={action.excuse}> |
| 308 |
{action.title} |
| 309 |
</StorageMenuItem>; |
| 310 |
} |
| 311 |
|
| 312 |
function make_page_kebab(page) { |
| 313 |
const items = []; |
| 314 |
|
| 315 |
function card_item_group(card) { |
| 316 |
const a = card.actions || []; |
| 317 |
if (a.length > 0) { |
| 318 |
let result = <DropdownList key={items.length}>{a.map(make_menu_item)}</DropdownList>; |
| 319 |
if (card.title) { |
| 320 |
result = <DropdownGroup key={items.length} label={card.title} className="storage-menu-title"> |
| 321 |
{result} |
| 322 |
</DropdownGroup>; |
| 323 |
} |
| 324 |
return result; |
| 325 |
} else |
| 326 |
return null; |
| 327 |
} |
| 328 |
|
| 329 |
let c = page.card; |
| 330 |
while (c) { |
| 331 |
const g = card_item_group(c); |
| 332 |
if (g) { |
| 333 |
if (items.length > 0) |
| 334 |
items.push(<Divider key={"div" + items.length} />); |
| 335 |
items.push(g); |
| 336 |
} |
| 337 |
c = c.next; |
| 338 |
} |
| 339 |
|
| 340 |
if (items.length == 0) |
| 341 |
return null; |
| 342 |
|
| 343 |
return <StorageBarMenu menuItems={items} isKebab />; |
| 344 |
} |
| 345 |
|
| 346 |
function make_actions_kebab(actions) { |
| 347 |
if (!actions || actions.length == 0) |
| 348 |
return null; |
| 349 |
|
| 350 |
return <StorageBarMenu menuItems={actions.map(make_menu_item)} isKebab />; |
| 351 |
} |
| 352 |
|
| 353 |
export const Actions = ({ actions, onlyMenu }) => { |
| 354 |
const narrow = useIsNarrow(); |
| 355 |
|
| 356 |
function for_menu(action) { |
| 357 |
// Determine whether a action should get a button or be in the |
| 358 |
// menu |
| 359 |
|
| 360 |
// When requested or when in a narrow layout, put everything into the menu |
| 361 |
if (onlyMenu || narrow) |
| 362 |
return true; |
| 363 |
|
| 364 |
// Everything that is dangerous goes to the menu |
| 365 |
if (action.danger) |
| 366 |
return true; |
| 367 |
|
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
const buttons = []; |
| 372 |
const items = []; |
| 373 |
|
| 374 |
if (!actions) |
| 375 |
return null; |
| 376 |
|
| 377 |
for (const action of actions) { |
| 378 |
if (for_menu(action)) |
| 379 |
items.push(make_menu_item(action)); |
| 380 |
else |
| 381 |
buttons.push( |
| 382 |
<StorageButton key={action.title} onClick={() => action.action(false)} |
| 383 |
kind={action.danger ? "danger" : null} excuse={action.excuse} |
| 384 |
spinner={action.spinner}> |
| 385 |
{action.title} |
| 386 |
</StorageButton>); |
| 387 |
} |
| 388 |
|
| 389 |
if (items.length > 0) |
| 390 |
buttons.push(<StorageBarMenu key="menu" menuItems={items} isKebab />); |
| 391 |
|
| 392 |
return buttons; |
| 393 |
}; |
| 394 |
|
| 395 |
function page_type_extra(page) { |
| 396 |
const extra = []; |
| 397 |
let c = page.card; |
| 398 |
while (c) { |
| 399 |
if (c.type_extra) |
| 400 |
extra.push(c.type_extra); |
| 401 |
c = c.next; |
| 402 |
} |
| 403 |
return extra; |
| 404 |
} |
| 405 |
|
| 406 |
function page_type(page) { |
| 407 |
const type = page.card.title; |
| 408 |
const extra = page_type_extra(page); |
| 409 |
if (extra.length > 0) |
| 410 |
return type + " (" + extra.join(", ") + ")"; |
| 411 |
else |
| 412 |
return type; |
| 413 |
} |
| 414 |
|
| 415 |
// PAGE_BLOCK_SUMMARY |
| 416 |
// |
| 417 |
// Describe a page in a way that is useful to identify it when |
| 418 |
// deciding which block device to format, or which block devices to |
| 419 |
// make a volume group out of, for example. The block device itself |
| 420 |
// (such as /dev/sda5) should not be part of the description; it is in |
| 421 |
// another table column already. |
| 422 |
// |
| 423 |
// The first card on the page that has the "for_summary" flag set |
| 424 |
// provides the description, and the type extra of all cards |
| 425 |
// leading to it are added. The description is either the title |
| 426 |
// of the card, or its id_extra. |
| 427 |
// |
| 428 |
// For more context, the description for the parent of a page is also |
| 429 |
// added. |
| 430 |
// |
| 431 |
// Thus, we end up with things like "Partition - MDRAID device". |
| 432 |
|
| 433 |
function page_block_summary_1(page) { |
| 434 |
let description = null; |
| 435 |
const extra = []; |
| 436 |
for (let card = page.card; card; card = card.next) { |
| 437 |
if (card.for_summary) { |
| 438 |
description = card.id_extra || card.title; |
| 439 |
break; |
| 440 |
} |
| 441 |
if (card.type_extra) |
| 442 |
extra.push(card.type_extra); |
| 443 |
} |
| 444 |
|
| 445 |
if (description && extra.length > 0) |
| 446 |
description += " (" + extra.join(", ") + ")"; |
| 447 |
|
| 448 |
return description; |
| 449 |
} |
| 450 |
|
| 451 |
function page_block_summary(page) { |
| 452 |
const desc1 = page_block_summary_1(page); |
| 453 |
const desc2 = page.parent && page.parent.parent && page_block_summary_1(page.parent); |
| 454 |
if (desc1 && desc2) |
| 455 |
return desc1 + " - " + desc2; |
| 456 |
else |
| 457 |
return desc1 || desc2; |
| 458 |
} |
| 459 |
|
| 460 |
let narrow_query = null; |
| 461 |
|
| 462 |
export const useIsNarrow = (onChange) => { |
| 463 |
if (!narrow_query) { |
| 464 |
const val = window.getComputedStyle(window.document.body).getPropertyValue("--pf-t--global--breakpoint--md"); |
| 465 |
narrow_query = window.matchMedia(`(max-width: ${val})`); |
| 466 |
} |
| 467 |
useEvent(narrow_query, "change", onChange); |
| 468 |
|
| 469 |
return narrow_query.matches; |
| 470 |
}; |
| 471 |
|
| 472 |
export const PageTable = ({ emptyCaption, aria_label, pages, crossrefs, sorted, show_icons }) => { |
| 473 |
const [collapsed, setCollapsed] = useState(true); |
| 474 |
const firstKeys = useRef(false); |
| 475 |
const narrow = useIsNarrow(() => { firstKeys.current = false }); |
| 476 |
|
| 477 |
let rows = []; |
| 478 |
const row_keys = new Set(); |
| 479 |
|
| 480 |
function make_row(page, crossref, level, border, key) { |
| 481 |
function card_has_danger(card) { |
| 482 |
if (card) |
| 483 |
return card.has_danger || card_has_danger(card.next); |
| 484 |
else |
| 485 |
return false; |
| 486 |
} |
| 487 |
|
| 488 |
function card_has_warning(card) { |
| 489 |
if (card) |
| 490 |
return card.has_warning || card_has_warning(card.next); |
| 491 |
else |
| 492 |
return false; |
| 493 |
} |
| 494 |
|
| 495 |
function card_has_job(card) { |
| 496 |
if (card) |
| 497 |
return client.path_jobs[card.job_path] || card_has_job(card.next); |
| 498 |
else |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
let info = null; |
| 503 |
if (card_has_job(page.card)) |
| 504 |
info = <>{"\n"}<Spinner isInline size="md" /></>; |
| 505 |
if (card_has_danger(page.card)) |
| 506 |
info = <>{"\n"}<ExclamationCircleIcon className="ct-icon-times-circle" />{info}</>; |
| 507 |
else if (card_has_warning(page.card)) |
| 508 |
info = <>{"\n"}<ExclamationTriangleIcon className="ct-icon-exclamation-triangle" />{info}</>; |
| 509 |
|
| 510 |
const icon = (show_icons && page.icon) ? <page.icon /> : null; |
| 511 |
const name = crossref ? page.name : page_display_name(page); |
| 512 |
const type = crossref ? page_block_summary(page) : page_type(page); |
| 513 |
const location = crossref ? crossref.extra : page.columns[1]; |
| 514 |
let size = crossref ? crossref.size : page.columns[2]; |
| 515 |
const actions = crossref ? make_actions_kebab(crossref.actions) : make_page_kebab(page); |
| 516 |
|
| 517 |
if (typeof size === "number") { |
| 518 |
if (narrow) |
| 519 |
size = <span className="usage-text">{fmt_size(size)}</span>; |
| 520 |
else |
| 521 |
size = <StorageSize size={size} />; |
| 522 |
} |
| 523 |
|
| 524 |
function find_button(element, parent_node_name, field) { |
| 525 |
let column = 0; |
| 526 |
while (element && element.nodeName != parent_node_name) { |
| 527 |
if (element.nodeName == "TD") { |
| 528 |
let td = element; |
| 529 |
while (td) { |
| 530 |
column++; |
| 531 |
td = td.previousElementSibling; |
| 532 |
} |
| 533 |
} |
| 534 |
element = element.parentElement; |
| 535 |
} |
| 536 |
if (!element) |
| 537 |
return null; |
| 538 |
const selector = column > 0 ? `td:nth-child(${column}) button` : "button"; |
| 539 |
while ((element = element[field])) { |
| 540 |
const button = element.querySelector(selector); |
| 541 |
if (button) |
| 542 |
return button; |
| 543 |
} |
| 544 |
return null; |
| 545 |
} |
| 546 |
|
| 547 |
function onRowKeyDown(event) { |
| 548 |
const { code, target } = event; |
| 549 |
|
| 550 |
function step(parent_node_name, field) { |
| 551 |
find_button(target, parent_node_name, field)?.focus(); |
| 552 |
event.preventDefault(); |
| 553 |
} |
| 554 |
|
| 555 |
if (code == "ArrowDown") |
| 556 |
step("TR", "nextElementSibling"); |
| 557 |
else if (code == "ArrowUp") |
| 558 |
step("TR", "previousElementSibling"); |
| 559 |
else if (code == "ArrowRight") |
| 560 |
step("TD", "nextElementSibling"); |
| 561 |
else if (code == "ArrowLeft") |
| 562 |
step("TD", "previousElementSibling"); |
| 563 |
} |
| 564 |
|
| 565 |
function location_link(id, location) { |
| 566 |
if (!location) |
| 567 |
return null; |
| 568 |
if (typeof location == "string") |
| 569 |
return location; |
| 570 |
if (!location.to) |
| 571 |
return location.label; |
| 572 |
|
| 573 |
return ( |
| 574 |
<Button ouiaId={id} isInline variant="link" onClick={() => cockpit.location.go(location.to)}> |
| 575 |
<Truncate content={location.label} /> |
| 576 |
</Button> |
| 577 |
); |
| 578 |
} |
| 579 |
|
| 580 |
const is_new = firstKeys.current != false && !firstKeys.current.has(key); |
| 581 |
row_keys.add(key); |
| 582 |
|
| 583 |
if (narrow) { |
| 584 |
rows.push( |
| 585 |
<Card isPlain key={key} |
| 586 |
className={"ct-small-table-card" + |
| 587 |
(is_new ? " ct-new-item" : "")} |
| 588 |
data-test-row-name={page.name} |
| 589 |
data-test-row-location={location?.label || location}> |
| 590 |
<CardBody> |
| 591 |
<Split hasGutter> |
| 592 |
{ icon && <SplitItem>{icon}</SplitItem> } |
| 593 |
<SplitItem isFilled> |
| 594 |
{location_link("id-link", { to: page.location, label: <strong>{name}</strong> })} |
| 595 |
{info} |
| 596 |
</SplitItem> |
| 597 |
<SplitItem>{actions}</SplitItem> |
| 598 |
</Split> |
| 599 |
<Split hasGutter isWrappable> |
| 600 |
<SplitItem>{type}</SplitItem> |
| 601 |
<SplitItem isFilled>{location_link("location-link", location)}</SplitItem> |
| 602 |
<SplitItem isFilled className="pf-v6-u-text-align-end">{size}</SplitItem> |
| 603 |
</Split> |
| 604 |
</CardBody> |
| 605 |
</Card>); |
| 606 |
} else { |
| 607 |
const cols = [ |
| 608 |
<Td key="1"> |
| 609 |
<div className="indent" style={ { "--level": level } }> |
| 610 |
{location_link("id-link", { to: page.location, label: name })} |
| 611 |
{info} |
| 612 |
</div> |
| 613 |
</Td>, |
| 614 |
<Td key="2" modifier="nowrap">{type}</Td>, |
| 615 |
<Td key="3" modifier="nowrap">{location_link("location-link", location)}</Td>, |
| 616 |
<Td key="4" className="storage-size-column">{size}</Td>, |
| 617 |
<Td key="5" className="pf-v6-c-table__action">{actions || <div /> }</Td>, |
| 618 |
]; |
| 619 |
if (show_icons) |
| 620 |
cols.unshift(<Td key="0" className="storage-device-icon">{icon}</Td>); |
| 621 |
|
| 622 |
rows.push( |
| 623 |
<Tr key={key} |
| 624 |
className={(border ? "" : " remove-border") + |
| 625 |
(is_new ? " ct-new-item" : "")} |
| 626 |
onKeyDown={onRowKeyDown} |
| 627 |
data-test-row-name={page.name} data-test-row-location={location?.label || location} |
| 628 |
> |
| 629 |
{cols} |
| 630 |
</Tr>); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
function page_compare(a, b) { |
| 635 |
if (a.category < b.category) |
| 636 |
return -1; |
| 637 |
else if (a.category > b.category) |
| 638 |
return 1; |
| 639 |
else |
| 640 |
return a.name.localeCompare(b.name); |
| 641 |
} |
| 642 |
|
| 643 |
function sort(things, accessor, sorted) { |
| 644 |
if (sorted === false) |
| 645 |
return things; |
| 646 |
// HACK: Use toSorted() once enough users upgrade to at least Firefox ESR 115 |
| 647 |
// See: https://github.com/cockpit-project/cockpit/issues/19848 |
| 648 |
return things.slice().sort((a, b) => page_compare(accessor(a), accessor(b))); |
| 649 |
} |
| 650 |
|
| 651 |
function make_page_rows(pages, level, last_has_border, key, sorted) { |
| 652 |
const sorted_pages = sort(pages, p => p, sorted); |
| 653 |
for (const p of sorted_pages) { |
| 654 |
const is_last = (level == 0 || p == sorted_pages[pages.length - 1]); |
| 655 |
const p_key = key + ":" + (p.key || p.name); |
| 656 |
make_row(p, null, level, is_last && p.children.length == 0 && last_has_border, p_key); |
| 657 |
make_page_rows(p.children, level + 1, is_last && last_has_border, p_key, p.options.sorted); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
function make_crossref_rows(crossrefs) { |
| 662 |
for (const c of sort(crossrefs, c => c.card.page, sorted)) |
| 663 |
make_row(c.card.page, c, 0, true, c.card.page.name); |
| 664 |
} |
| 665 |
|
| 666 |
if (pages) |
| 667 |
make_page_rows(pages, 0, true, "", sorted); |
| 668 |
else if (crossrefs) |
| 669 |
make_crossref_rows(crossrefs); |
| 670 |
|
| 671 |
if (firstKeys.current === false) |
| 672 |
firstKeys.current = row_keys; |
| 673 |
else { |
| 674 |
firstKeys.current.forEach(v => { |
| 675 |
if (!row_keys.has(v)) |
| 676 |
firstKeys.current.delete(v); |
| 677 |
}); |
| 678 |
} |
| 679 |
|
| 680 |
if (rows.length == 0) { |
| 681 |
return <EmptyState> |
| 682 |
<EmptyStateBody> |
| 683 |
{emptyCaption} |
| 684 |
</EmptyStateBody> |
| 685 |
</EmptyState>; |
| 686 |
} |
| 687 |
|
| 688 |
let show_all_button = null; |
| 689 |
if (rows.length > 50 && collapsed) { |
| 690 |
show_all_button = ( |
| 691 |
<Bullseye> |
| 692 |
<Button variant='link' |
| 693 |
onKeyDown={ev => ev.key === "Enter" && setCollapsed(false)} |
| 694 |
onClick={() => setCollapsed(false)}> |
| 695 |
{cockpit.format(_("Show all $0 rows"), rows.length)} |
| 696 |
</Button> |
| 697 |
</Bullseye>); |
| 698 |
rows = rows.slice(0, 50); |
| 699 |
} |
| 700 |
|
| 701 |
return ( |
| 702 |
<> |
| 703 |
{ narrow |
| 704 |
? rows |
| 705 |
: <Table aria-label={aria_label} |
| 706 |
className="page-table" |
| 707 |
variant="compact"> |
| 708 |
{ pages && |
| 709 |
<Thead> |
| 710 |
<Tr> |
| 711 |
{ show_icons && <Th aria-label={_("Category")} />} |
| 712 |
<Th>{_("ID")}</Th> |
| 713 |
<Th>{_("Type")}</Th> |
| 714 |
<Th>{_("Location")}</Th> |
| 715 |
<Th className="storage-size-column-header">{_("Size")}</Th> |
| 716 |
<Th aria-label={_("Actions")} /> |
| 717 |
</Tr> |
| 718 |
</Thead> |
| 719 |
} |
| 720 |
<Tbody> |
| 721 |
{rows} |
| 722 |
</Tbody> |
| 723 |
</Table> |
| 724 |
} |
| 725 |
{show_all_button} |
| 726 |
</>); |
| 727 |
}; |
| 728 |
|
| 729 |
export const ChildrenTable = ({ emptyCaption, aria_label, page, show_icons }) => { |
| 730 |
return <PageTable emptyCaption={emptyCaption} |
| 731 |
aria_label={aria_label} |
| 732 |
pages={page.children} |
| 733 |
sorted={page.options.sorted} |
| 734 |
show_icons={show_icons} />; |
| 735 |
}; |
| 736 |
|
| 737 |
function page_id_extra(page) { |
| 738 |
let extra = ""; |
| 739 |
let c = page.card; |
| 740 |
while (c) { |
| 741 |
if (c.id_extra) |
| 742 |
extra += " " + c.id_extra; |
| 743 |
c = c.next; |
| 744 |
} |
| 745 |
return extra; |
| 746 |
} |
| 747 |
|
| 748 |
function page_display_name(page) { |
| 749 |
let name = page.name; |
| 750 |
const extra = page_id_extra(page); |
| 751 |
if (extra) |
| 752 |
name = name + " - " + extra; |
| 753 |
return name; |
| 754 |
} |
| 755 |
|
| 756 |
const PageCardStackItems = ({ page, plot_state }) => { |
| 757 |
const items = []; |
| 758 |
let c = page.card; |
| 759 |
while (c) { |
| 760 |
items.push(<React.Fragment key={items.length}> |
| 761 |
<StackItem> |
| 762 |
<c.component card={c} plot_state={plot_state} {...c.props} /> |
| 763 |
</StackItem> |
| 764 |
</React.Fragment>); |
| 765 |
c = c.next; |
| 766 |
} |
| 767 |
|
| 768 |
return items.reverse(); |
| 769 |
}; |
| 770 |
|
| 771 |
export function block_location(block) { |
| 772 |
return decode_filename(block.PreferredDevice).replace(/^\/dev\//, ""); |
| 773 |
} |
| 774 |
|
| 775 |
const StorageBreadcrumb = ({ page }) => { |
| 776 |
const parent_crumbs = []; |
| 777 |
let pp = page.parent; |
| 778 |
while (pp) { |
| 779 |
if (pp.location) { |
| 780 |
parent_crumbs.unshift( |
| 781 |
<BreadcrumbItem key={pp.name} to={"#" + cockpit.location.encode(pp.location)}> |
| 782 |
{page_display_name(pp)} |
| 783 |
</BreadcrumbItem> |
| 784 |
); |
| 785 |
} |
| 786 |
pp = pp.parent; |
| 787 |
} |
| 788 |
|
| 789 |
return ( |
| 790 |
<Breadcrumb> |
| 791 |
{ parent_crumbs } |
| 792 |
<BreadcrumbItem isActive>{page_display_name(page)}</BreadcrumbItem> |
| 793 |
</Breadcrumb>); |
| 794 |
}; |
| 795 |
|
| 796 |
export const StorageCard = ({ card, alert, alerts, actions, children }) => { |
| 797 |
return ( |
| 798 |
<Card isPlain={card.page_location && card.page_location.length == 0} data-test-card-title={card.title}> |
| 799 |
{ (client.in_anaconda_mode() && card.page.parent && !card.next) && |
| 800 |
<CardBody> |
| 801 |
<StorageBreadcrumb page={card.page} /> |
| 802 |
</CardBody> |
| 803 |
} |
| 804 |
<CardHeader actions={{ actions: actions || <Actions actions={card.actions} /> }}> |
| 805 |
<CardTitle>{card.title}</CardTitle> |
| 806 |
</CardHeader> |
| 807 |
{(alert || (alerts && alerts.length > 0)) && <CardBody><AlertGroup>{alert}{alerts}</AlertGroup></CardBody>} |
| 808 |
{children} |
| 809 |
<JobsPanel client={client} path={card.job_path} /> |
| 810 |
</Card>); |
| 811 |
}; |
| 812 |
|
| 813 |
export const StorageDescription = ({ title, value, action, help, children }) => { |
| 814 |
if (!value && !action && !children) |
| 815 |
return null; |
| 816 |
|
| 817 |
let content; |
| 818 |
if (action && value) { |
| 819 |
content = ( |
| 820 |
<Flex> |
| 821 |
<FlexItem data-test-value>{value}</FlexItem> |
| 822 |
<FlexItem data-test-action>{action}</FlexItem> |
| 823 |
</Flex>); |
| 824 |
} else { |
| 825 |
content = value || action; |
| 826 |
} |
| 827 |
|
| 828 |
let help_popover; |
| 829 |
if (help) { |
| 830 |
help_popover = ( |
| 831 |
<Popover headerContent={help}> |
| 832 |
<Button component="span" isInline variant="link" aria-label={_("more info")}> |
| 833 |
<Icon status="info"> |
| 834 |
<HelpIcon /> |
| 835 |
</Icon> |
| 836 |
</Button> |
| 837 |
</Popover> |
| 838 |
); |
| 839 |
} |
| 840 |
|
| 841 |
return ( |
| 842 |
<DescriptionListGroup data-test-desc-title={title}> |
| 843 |
<DescriptionListTerm>{title} {help_popover}</DescriptionListTerm> |
| 844 |
<DescriptionListDescription data-test-value={!(action && value)}> |
| 845 |
{content}{children} |
| 846 |
</DescriptionListDescription> |
| 847 |
</DescriptionListGroup>); |
| 848 |
}; |
| 849 |
|
| 850 |
export const StoragePage = ({ location, plot_state }) => { |
| 851 |
const page = get_page_from_location(location); |
| 852 |
|
| 853 |
return ( |
| 854 |
<Page id="storage" className={"pf-m-no-sidebar" + (client.in_anaconda_mode() ? " anaconda" : "")}> |
| 855 |
{ (!client.in_anaconda_mode() && page.parent) && |
| 856 |
<PageBreadcrumb hasBodyWrapper={false} stickyOnBreakpoint={{ default: "top" }}> |
| 857 |
<StorageBreadcrumb page={page} /> |
| 858 |
</PageBreadcrumb> |
| 859 |
} |
| 860 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 861 |
<Stack hasGutter> |
| 862 |
<MultipathAlert client={client} /> |
| 863 |
<PageCardStackItems page={page} plot_state={plot_state} noarrow /> |
| 864 |
</Stack> |
| 865 |
</PageSection> |
| 866 |
</Page> |
| 867 |
); |
| 868 |
}; |