/*
* Copyright (C) 2023 Red Hat, Inc.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
import cockpit from "cockpit";
import React, { useState, useRef } from "react";
import client from "./client";
import { useEvent } from "hooks.js";
import { AlertGroup } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
import { Card, CardHeader, CardTitle, CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
import { Divider } from '@patternfly/react-core/dist/esm/components/Divider/index.js';
import { DropdownGroup, DropdownList } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js';
import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js";
import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js";
import { Bullseye } from "@patternfly/react-core/dist/esm/layouts/Bullseye/index.js";
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
import { Table, Thead, Tbody, Tr, Th, Td } from '@patternfly/react-table';
import { EmptyState, EmptyStateBody } from "@patternfly/react-core/dist/esm/components/EmptyState/index.js";
import { ExclamationTriangleIcon, ExclamationCircleIcon, HelpIcon } from "@patternfly/react-icons";
import { Page, PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js";
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js";
import { DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon";
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover";
import {
decode_filename, block_short_name, fmt_size,
reset_available_spaces
} from "./utils.js";
import { StorageButton, StorageBarMenu, StorageMenuItem, StorageSize } from "./storage-controls.jsx";
import { MultipathAlert } from "./multipath.jsx";
import { JobsPanel } from "./jobs-panel.jsx";
import { Truncate } from "../lib/cockpit-components-truncate.jsx";
const _ = cockpit.gettext;
/* PAGES and CARDS
This is the central hub of the Storage application. It implements a
couple of concepts: "page", "card", "crossref", and "action". It
also implements the React components that construct the actual UI
out of them.
On every change of the client, a tree of pages is constructed. Each
page has a list of cards on it. The cards each have a list of
actions. In addition to that, there are a few crossrefs (cross
references) between pages that don't follow the tree structure.
The tree of pages starts with "make_overview_page" in
overview/overview.jsx. That function creates the data structures
that represent the overview page (using functions exported from
this file), and is also responsible for kicking off the creation of
its child pages. (And each page construction function of course
also creates the cards and actions for that page).
Pages can be shown by themselves, or be part of various tables in
other pages. For example, each row in the table in the overview
page represents another page, and clicking on it will navigate to
that page. (In fact, the overview table shows the whole tree. Other
pages show subsets of the tree in their tables.)
Most of the complications with pages and cards are there to support
putting pages into tables. Without having to do that, we wouldn't
need the pages and cards abstractions at all.
A page derives its "ID", "Type", "Location", and "Size" properties
from its cards in various ad-hoc and arguably too complex
ways. This is seems necessary since the cards that end up together
on a page can vary significantly depending on the actual storage
configuration of the machine.
For example, a MDRAID device might have a encrypted LVM2 physical
volume directly on it, or it might have a partition table with a
PV, in which case the card for the PV appears on another page
together with a card for the partition.
Cards are constructed with a call like this:
const card = new_card({
next: next_card,
title: ...,
component: CardComponent,
props: { react_props... }
});
A page is constructed with a call like this:
new_page(parent_page, first_card, { options... });
A call to "new_page" will implicitly build the tree by inserting
the new_page into the children of "parent_page".
The main React component for this application is StoragePage. It
will find the correct page for the browser URL and display it,
together with all the trimmings like the breadcrumb trail.
The page will be filled with the React components specified by its
cards. Such a component should use StorageCard to get the standard
look of all cards, and to work well with the test helpers.
Tables of pages are handled by the PageTable component exported
from this file or, more likely, its specialized version
ChildrenTable.
A ChildrenTable shows all direct and indirect children of a given
page, by using a PageTable appropriately. A raw PageTable can also
show cross references.
A cross reference associates a page with an arbitrary key. For
example, a page that represents a LVM2 physical volume will
associate itself with the DBusProxy for its volume group. The page
for the volume group will then retrieve all pages associated with
its DBusProxy and put them into a table to show all its physical
volumes.
When a page appears in such a crossref table, it has dedicated
actions, a dedicated size, and generally looks different from when
it appears in a "normal" children's table. This is another source of
complication of how cards and pages interact.
*/
let pages = null;
let crossrefs = null;
export const PAGE_CATEGORY_PHYSICAL = 1;
export const PAGE_CATEGORY_VIRTUAL = 2;
export const PAGE_CATEGORY_NETWORK = 3;
export function reset_pages() {
pages = new Map();
crossrefs = new Map();
reset_available_spaces();
}
function name_from_card(card) {
if (!card)
return null;
return name_from_card(card.next) || card.page_name;
}
function icon_from_card(card) {
if (!card)
return null;
return icon_from_card(card.next) || card.page_icon;
}
function category_from_card(card) {
if (!card)
return null;
return category_from_card(card.next) || card.page_category;
}
function key_from_card(card) {
if (!card)
return null;
return key_from_card(card.next) || card.page_key;
}
function location_from_card(card) {
if (!card)
return null;
return location_from_card(card.next) || card.page_location;
}
function size_from_card(card) {
if (!card)
return null;
if (card.page_size)
return card.page_size;
return size_from_card(card.next);
}
export function new_page(parent, card, options) {
const page = {
location: location_from_card(card),
name: name_from_card(card),
icon: icon_from_card(card),
category: category_from_card(card),
key: key_from_card(card),
parent,
children: [],
card,
options: options || {},
};
page.columns = [
card.title,
card.location,
size_from_card(card),
];
if (parent)
parent.children.push(page);
while (card) {
card.page = page;
card = card.next;
}
if (page.location) {
pages.set(JSON.stringify(page.location), page);
if (page.location.length == 0) {
// This is the Overview page. Make it the parent of the
// special "not found" page (but don't make the "not
// found" page a child of the Overview...)
not_found_page.parent = page;
}
}
return page;
}
export function new_card({
title, location, next,
type_extra, id_extra,
page_name, page_icon, page_category, page_key, page_location, page_size,
page_block,
for_summary,
has_warning, has_danger, job_path,
component, props,
actions,
}) {
if (page_block) {
page_location = [block_location(page_block)];
page_name = block_short_name(page_block);
page_size = page_block.Size;
job_path = page_block.path;
}
return {
title,
location,
next,
type_extra,
page_name,
page_icon,
page_category: page_category || PAGE_CATEGORY_PHYSICAL,
page_key,
page_location,
page_size,
for_summary,
component,
props,
has_warning,
has_danger,
job_path,
actions: actions ? actions.filter(a => !!a) : null,
id_extra,
};
}
export function register_crossref(crossref) {
const val = crossrefs.get(crossref.key) || [];
crossref.actions = crossref.actions ? crossref.actions.filter(a => !!a) : null;
val.push(crossref);
crossrefs.set(crossref.key, val);
}
export function get_crossrefs(key) {
return crossrefs.get(key);
}
/* Getting the page for a navigation location.
*
* We have a special "not found" page that is returned when there is
* no real page at the given location.
*/
const NotFoundCard = ({ card }) => {
return (
| } | {_("ID")} | {_("Type")} | {_("Location")} | {_("Size")} |
|---|