Line data Source code
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 6 : import cockpit from "cockpit";
11 :
12 6 : import React, { useState } from "react";
13 6 : 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 6 : const App = () => {
24 6 : const [progress, setProgress] = useState({});
25 6 : const [progressTitle, setProgressTitle] = useState({});
26 :
27 2 : function action(func, arg, progress_title, id) {
28 2 : setProgressTitle({ ...progressTitle, [id]: progress_title });
29 2 : func(arg, progress => setProgress({ ...progress, [id]: progress }))
30 2 : .finally(() => setProgress({ ...progress, [id]: null }))
31 2 : .catch(show_error);
32 2 : }
33 :
34 6 : const { path } = usePageLocation();
35 :
36 6 : const metainfo_db = useObject(get_metainfo_db, null, []);
37 6 : useEvent(metainfo_db, "changed");
38 :
39 6 : if (!metainfo_db.ready)
40 6 : return <EmptyStatePanel loading />;
41 :
42 6 : if (path.length === 0) {
43 6 : return <ApplicationList metainfo_db={metainfo_db}
44 6 : action={action}
45 6 : appProgress={progress}
46 6 : appProgressTitle={progressTitle} />;
47 3 : } else if (path.length == 1) {
48 3 : const id = path[0];
49 3 : return <Application metainfo_db={metainfo_db}
50 3 : action={action}
51 3 : progress={progress[id]}
52 3 : id={id} />;
53 1 : } else { /* redirect */
54 1 : console.warn("not a apps location: " + path);
55 1 : cockpit.location = '';
56 1 : }
57 6 : };
58 :
59 6 : function init() {
60 6 : const root = createRoot(document.getElementById("apps-page"));
61 6 : root.render(<App />);
62 6 : }
63 :
64 6 : document.addEventListener("DOMContentLoaded", init);
|