Debian Codegraph @

pkg/apps/application-list.jsx

raw
1 /*
2 * Copyright (C) 2017 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React, { useState } from "react";
8 import { Alert, AlertActionCloseButton, AlertActionLink } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
9 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
10 import { DataList, DataListAction, DataListCell, DataListItem, DataListItemCells, DataListItemRow } from "@patternfly/react-core/dist/esm/components/DataList/index.js";
11 import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
12 import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
13 import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js";
14
15 import { RebootingIcon } from "@patternfly/react-icons";
16
17 import { get_manifest_config_matchlist } from "utils";
18 import { read_os_release } from "os-release";
19 import { EmptyStatePanel } from "cockpit-components-empty-state.jsx";
20 import { useInit } from "hooks";
21
22 import { ProgressReporter, icon_url, show_error, launch, ProgressBar, CancelButton } from "./utils";
23 import { ActionButton } from "./application.jsx";
24 import { getPackageManager } from "packagemanager.js";
25 import { Title, TitleSizes } from "@patternfly/react-core/dist/esm/components/Title";
26
27 const _ = cockpit.gettext;
28
29 async function refresh_appstream_metadata(origin_files, config_packages, data_packages, progress_cb) {
30 /* In addition to refreshing the repository metadata, we also
31 * update all packages that contain AppStream collection metadata.
32 *
33 * AppStream collection metadata is arguably part of the
34 * repository metadata and should be updated during a regular
35 * refresh of the repositories. On some distributions this is
36 * what happens, but on others (such as Fedora), the collection
37 * metadata is delivered in packages. We find them and update
38 * them explicitly.
39 *
40 * Also, we have two explicit lists of packages, and we make sure
41 * that they are installed. The first list contains packages that
42 * configure the system to retrieve AppStream data as part of
43 * repository metadata, and the second list contains packages that
44 * contain AppStream data themselves.
45 */
46 const packagemanager = await getPackageManager();
47 const progress = new ProgressReporter(0, 1, progress_cb);
48
49 if (config_packages.length > 0) {
50 progress.base = 0;
51 progress.range = 5;
52
53 await packagemanager.install_packages(config_packages, progress.progress_reporter);
54 }
55
56 const origin_pkgs = new Set(await packagemanager.find_file_packages(origin_files, progress.progress_reporter));
57
58 progress.base = 6;
59 progress.range = 69;
60 await packagemanager.refresh(true, progress.progress_reporter);
61
62 progress.base = 75;
63 progress.range = 5;
64
65 const updates = await packagemanager.get_updates(false, progress.progress_reporter);
66 const filtered_updates = [];
67
68 for (const update of updates) {
69 if (origin_pkgs.has(update.name))
70 filtered_updates.push(update);
71 }
72
73 progress.base = 80;
74 progress.range = 15;
75
76 if (filtered_updates.length > 0)
77 return packagemanager.update_packages(filtered_updates, progress.progress_reporter, null);
78
79 if (data_packages.length > 0) {
80 progress.range = 95;
81 await packagemanager.install_packages(data_packages, progress.progress_reporter);
82 }
83 }
84
85 const ApplicationRow = ({ comp, progress, progress_title, action }) => {
86 const [error, setError] = useState();
87
88 const name = (
89 <Button variant="link"
90 isInline id={comp.name}
91 onClick={() => comp.installed ? launch(comp) : cockpit.location.go(comp.id)}>
92 {comp.name}
93 </Button>);
94
95 let summary_or_progress;
96 if (progress) {
97 summary_or_progress = (
98 <Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}>
99 <span id={comp.name + "-progress"} className="progress-title-span">{progress_title}</span>
100 <ProgressBar data={progress} ariaLabelledBy={comp.name + "-progress"} />
101 </Flex>);
102 } else {
103 if (error) {
104 summary_or_progress = (
105 <div>
106 {comp.summary}
107 <Alert isInline variant='danger'
108 actionClose={<AlertActionCloseButton onClose={() => setError(null)} />}
109 title={error} />
110 </div>
111 );
112 } else {
113 summary_or_progress = comp.summary;
114 }
115 }
116
117 return (
118 <DataListItem className="app-list" aria-labelledby={comp.name}>
119 <DataListItemRow>
120 <DataListItemCells
121 dataListCells={[
122 <DataListCell isIcon key="icon">
123 <img src={icon_url(comp.icon)} alt={cockpit.format(_("icon of $0"), comp.name)} />
124 </DataListCell>,
125 <DataListCell width={1} key="app name">
126 {name}
127 </DataListCell>,
128 <DataListCell width={4} key="secondary content">
129 {summary_or_progress}
130 </DataListCell>,
131 ]}
132 />
133 <DataListAction aria-labelledby={comp.name} aria-label={_("Actions")}>
134 <ActionButton comp={comp} progress={progress} action={action} />
135 </DataListAction>
136 </DataListItemRow>
137 </DataListItem>
138 );
139 };
140
141 export const ApplicationList = ({ metainfo_db, appProgress, appProgressTitle, action }) => {
142 const [progress, setProgress] = useState(false);
143 const [dataPackagesInstalled, setDataPackagesInstalled] = useState(null);
144 const comps = [];
145 for (const id in metainfo_db.components)
146 comps.push(metainfo_db.components[id]);
147 comps.sort((a, b) => a.name.localeCompare(b.name));
148
149 async function check_missing_data(packages) {
150 try {
151 const packagemanager = await getPackageManager();
152 setDataPackagesInstalled(await packagemanager.is_installed(packages));
153 } catch (e) {
154 console.warn("Failed to check missing AppStream metadata packages:", e.toString());
155 }
156 }
157
158 async function get_packages() {
159 const os_release = await read_os_release();
160 // ID is a single value, ID_LIKE is a list
161 const os_list = [os_release?.ID, ...(os_release?.ID_LIKE || "").split(/\s+/)];
162 const configPackages = get_manifest_config_matchlist('apps', 'appstream_config_packages', [], os_list);
163 const dataPackages = get_manifest_config_matchlist('apps', 'appstream_data_packages', [], os_list);
164 return [configPackages, dataPackages];
165 }
166
167 useInit(async () => {
168 const [config, data] = await get_packages();
169 await check_missing_data([...config, ...data]);
170 });
171
172 async function refresh() {
173 const [configPackages, dataPackages] = await get_packages();
174 try {
175 await refresh_appstream_metadata(metainfo_db.origin_files,
176 configPackages,
177 dataPackages,
178 setProgress);
179 } catch (e) {
180 show_error(e);
181 } finally {
182 await check_missing_data([...dataPackages, ...configPackages]);
183 setProgress(false);
184 }
185 }
186
187 let refresh_progress;
188 let refresh_button;
189 let tbody;
190 if (progress) {
191 refresh_progress = <ProgressBar id="refresh-progress" size="sm" data={progress} />;
192 refresh_button = <CancelButton data={progress} />;
193 } else {
194 refresh_progress = null;
195 refresh_button = (
196 <Button icon={<RebootingIcon />} variant="secondary" onClick={refresh} id="refresh" aria-label={_("Update package information")} />
197 );
198 }
199
200 if (comps.length) {
201 tbody = comps.map(c => <ApplicationRow comp={c} key={c.id}
202 progress={appProgress[c.id]}
203 progress_title={appProgressTitle[c.id]}
204 action={action} />);
205 }
206
207 const data_missing_msg = (dataPackagesInstalled == false && !refresh_progress)
208 ? _("Application information is missing")
209 : null;
210
211 return (
212 <Page id="list-page" data-packages-checked={dataPackagesInstalled !== null} className="pf-m-no-sidebar">
213 <PageSection hasBodyWrapper={false}>
214 <Flex alignItems={{ default: 'alignItemsCenter' }}>
215 <Title headingLevel="h2" size={TitleSizes['3xl']}>
216 {_("Applications")}
217 </Title>
218 <FlexItem align={{ default: 'alignRight' }}>
219 <Flex alignItems={{ default: 'alignItemsCenter' }} spacer={{ default: 'spacerXs' }}>
220 <FlexItem>
221 {refresh_progress}
222 </FlexItem>
223 <FlexItem>
224 {refresh_button}
225 </FlexItem>
226 </Flex>
227 </FlexItem>
228 </Flex>
229 </PageSection>
230 {comps.length == 0
231 ? <EmptyStatePanel title={ _("No applications installed or available.") }
232 paragraph={data_missing_msg}
233 action={ data_missing_msg && _("Install application information")} onAction={refresh} />
234 : <PageSection hasBodyWrapper={false}>
235 <Stack hasGutter>
236 {!progress && data_missing_msg &&
237 <StackItem key="missing-meta-alert">
238 <Alert variant="warning" isInline title={data_missing_msg}
239 actionLinks={ <AlertActionLink onClick={refresh}>{_("Install")}</AlertActionLink>} />
240 </StackItem>
241 }
242 <StackItem>
243 <DataList aria-label={_("Applications list")}>
244 { tbody }
245 </DataList>
246 </StackItem>
247 </Stack>
248 </PageSection>
249 }
250 </Page>
251 );
252 };