| 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 from "react"; |
| 8 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js"; |
| 10 |
import { Card, CardBody, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js'; |
| 11 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 12 |
import { Page, PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 13 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 14 |
import { ExternalLinkAltIcon } from '@patternfly/react-icons'; |
| 15 |
|
| 16 |
import { icon_url, launch, ProgressBar, CancelButton, reload_bridge_packages, ProgressReporter } from "./utils"; |
| 17 |
|
| 18 |
import "./application.scss"; |
| 19 |
import { getPackageManager } from "packagemanager.js"; |
| 20 |
|
| 21 |
const _ = cockpit.gettext; |
| 22 |
|
| 23 |
async function install_package(pkgname, progress_cb) { |
| 24 |
const packagemanager = await getPackageManager(); |
| 25 |
await packagemanager.install_packages([pkgname], progress_cb); |
| 26 |
await reload_bridge_packages(); |
| 27 |
} |
| 28 |
|
| 29 |
async function remove_package(filename, progress_cb) { |
| 30 |
const progress = new ProgressReporter(0, 1, progress_cb); |
| 31 |
const packagemanager = await getPackageManager(); |
| 32 |
const pkgnames = await packagemanager.find_file_packages([filename], progress.progress_reporter); |
| 33 |
progress.base = 1; |
| 34 |
progress.range = 99; |
| 35 |
await packagemanager.remove_packages(pkgnames, progress.progress_reporter); |
| 36 |
await reload_bridge_packages(); |
| 37 |
} |
| 38 |
|
| 39 |
export const ActionButton = ({ comp, progress, action }) => { |
| 40 |
function install(comp) { |
| 41 |
action(install_package, comp.pkgname, _("Installing"), comp.id); |
| 42 |
} |
| 43 |
|
| 44 |
function remove(comp) { |
| 45 |
action(remove_package, comp.file, _("Removing"), comp.id); |
| 46 |
} |
| 47 |
|
| 48 |
if (progress) { |
| 49 |
return <CancelButton data={progress} />; |
| 50 |
} else if (comp.installed) { |
| 51 |
return <Button variant="danger" onClick={() => remove(comp)}>{_("Remove")}</Button>; |
| 52 |
} else { |
| 53 |
return <Button variant="secondary" onClick={() => install(comp)}>{_("Install")}</Button>; |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
export const Application = ({ metainfo_db, id, progress, action }) => { |
| 58 |
if (!id) |
| 59 |
return null; |
| 60 |
|
| 61 |
const comp = metainfo_db.components[id]; |
| 62 |
|
| 63 |
function render_homepage_link(urls) { |
| 64 |
return urls.map((url, index) => { |
| 65 |
if (url.type == 'homepage') { |
| 66 |
return ( |
| 67 |
<Button isInline variant="link" component='a' href={url.link} |
| 68 |
key={"project-url-" + index} |
| 69 |
target="_blank" rel="noopener noreferrer" |
| 70 |
icon={<ExternalLinkAltIcon />} |
| 71 |
iconPosition="right"> |
| 72 |
{_("View project website")} |
| 73 |
</Button> |
| 74 |
); |
| 75 |
} else { |
| 76 |
return null; |
| 77 |
} |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
// Render a description in the form returned by the AppStream |
| 82 |
// parser, which is a list of paragraphs and lists. |
| 83 |
|
| 84 |
function render_description(description) { |
| 85 |
if (!description) |
| 86 |
return <p>{_("No description provided.")}</p>; |
| 87 |
|
| 88 |
return description.map((paragraph, index) => { |
| 89 |
if (paragraph.tag == 'ul') { |
| 90 |
return <ul key={`paragraph-${index}`}>{paragraph.items.map(item => <li key={item}>{item}</li>)}</ul>; |
| 91 |
} else if (paragraph.tag == 'ol') { |
| 92 |
return <ol key={`paragraph-${index}`}>{paragraph.items.map(item => <li key={item}>{item}</li>)}</ol>; |
| 93 |
} else { |
| 94 |
return <p key={`paragraph-${index}`}>{paragraph}</p>; |
| 95 |
} |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
// Render the icon, name, homepage link, summary, description, and screenshots of the component, |
| 100 |
// plus the UI for installing and removing it. |
| 101 |
|
| 102 |
function render_comp() { |
| 103 |
if (!comp) |
| 104 |
return <div>{_("Unknown application")}</div>; |
| 105 |
|
| 106 |
let progress_or_launch; |
| 107 |
if (progress) { |
| 108 |
progress_or_launch = <ProgressBar data={progress} />; |
| 109 |
} else if (comp.installed) { |
| 110 |
progress_or_launch = <Button variant="link" onClick={() => launch(comp)}>{_("Go to application")}</Button>; |
| 111 |
} else { |
| 112 |
progress_or_launch = null; |
| 113 |
} |
| 114 |
|
| 115 |
return ( |
| 116 |
<Card isPlain> |
| 117 |
<CardHeader actions={{ |
| 118 |
actions: <>{progress_or_launch}<ActionButton comp={comp} progress={progress} action={action} /></>, |
| 119 |
}}> |
| 120 |
<CardTitle> |
| 121 |
<Flex alignItems={{ default: 'alignItemsCenter' }}> |
| 122 |
<img src={icon_url(comp.icon)} alt="" /> |
| 123 |
<span>{comp.summary}</span> |
| 124 |
</Flex> |
| 125 |
</CardTitle> |
| 126 |
</CardHeader> |
| 127 |
<CardBody> |
| 128 |
<Stack hasGutter> |
| 129 |
{render_homepage_link(comp.urls)} |
| 130 |
<div className="app-description">{render_description(comp.description)}</div> |
| 131 |
{comp.screenshots.length |
| 132 |
? <div className="text-center"> |
| 133 |
{ comp.screenshots.map((s, index) => <img key={`comp-${index}`} className="app-screenshot" alt="" src={s.full} />) } |
| 134 |
</div> |
| 135 |
: null} |
| 136 |
</Stack> |
| 137 |
</CardBody> |
| 138 |
</Card> |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
return ( |
| 143 |
<Page id="app-page" |
| 144 |
className="application-details pf-m-no-sidebar"> |
| 145 |
<PageBreadcrumb hasBodyWrapper={false} stickyOnBreakpoint={{ default: "top" }}> |
| 146 |
<Breadcrumb> |
| 147 |
<BreadcrumbItem to="#/">{_("Applications")}</BreadcrumbItem> |
| 148 |
<BreadcrumbItem isActive>{comp ? comp.name : id}</BreadcrumbItem> |
| 149 |
</Breadcrumb> |
| 150 |
</PageBreadcrumb> |
| 151 |
<PageSection hasBodyWrapper={false}> |
| 152 |
{render_comp()} |
| 153 |
</PageSection> |
| 154 |
</Page> |
| 155 |
); |
| 156 |
}; |