Debian Codegraph @

pkg/apps/apps.jsx

raw
1 /*
2 * Copyright (C) 2017 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import '../lib/patternfly/patternfly-6-cockpit.scss';
7 import "polyfills";
8 import 'cockpit-dark-theme'; // once per page
9
10 import cockpit from "cockpit";
11
12 import React, { useState } from "react";
13 import { createRoot } from 'react-dom/client';
14
15 import { EmptyStatePanel } from "cockpit-components-empty-state.jsx";
16 import { usePageLocation, useObject, useEvent } from "hooks";
17
18 import { ApplicationList } from "./application-list.jsx";
19 import { Application } from "./application.jsx";
20 import { get_metainfo_db } from "./appstream.js";
21 import { show_error } from "./utils";
22
23 const App = () => {
24 const [progress, setProgress] = useState({});
25 const [progressTitle, setProgressTitle] = useState({});
26
27 function action(func, arg, progress_title, id) {
28 setProgressTitle({ ...progressTitle, [id]: progress_title });
29 func(arg, progress => setProgress({ ...progress, [id]: progress }))
30 .finally(() => setProgress({ ...progress, [id]: null }))
31 .catch(show_error);
32 }
33
34 const { path } = usePageLocation();
35
36 const metainfo_db = useObject(get_metainfo_db, null, []);
37 useEvent(metainfo_db, "changed");
38
39 if (!metainfo_db.ready)
40 return <EmptyStatePanel loading />;
41
42 if (path.length === 0) {
43 return <ApplicationList metainfo_db={metainfo_db}
44 action={action}
45 appProgress={progress}
46 appProgressTitle={progressTitle} />;
47 } else if (path.length == 1) {
48 const id = path[0];
49 return <Application metainfo_db={metainfo_db}
50 action={action}
51 progress={progress[id]}
52 id={id} />;
53 } else { /* redirect */
54 console.warn("not a apps location: " + path);
55 cockpit.location = '';
56 }
57 };
58
59 function init() {
60 const root = createRoot(document.getElementById("apps-page"));
61 root.render(<App />);
62 }
63
64 document.addEventListener("DOMContentLoaded", init);