| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 { AboutModal } from "@patternfly/react-core/dist/esm/components/AboutModal/index.js"; |
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { Divider } from "@patternfly/react-core/dist/esm/components/Divider/index.js"; |
| 11 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 12 |
import { Menu, MenuContent, MenuSearch, MenuSearchInput, MenuItem, MenuList } from "@patternfly/react-core/dist/esm/components/Menu/index.js"; |
| 13 |
import { |
| 14 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 15 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 16 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 17 |
import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js"; |
| 18 |
import { SearchIcon } from '@patternfly/react-icons'; |
| 19 |
|
| 20 |
import { useInit } from "hooks"; |
| 21 |
import { DialogResult } from "dialogs"; |
| 22 |
|
| 23 |
import { ShellState } from "./state"; |
| 24 |
|
| 25 |
import "menu-select-widget.scss"; |
| 26 |
|
| 27 |
const _ = cockpit.gettext; |
| 28 |
|
| 29 |
interface Package { |
| 30 |
name: string; |
| 31 |
version: string; |
| 32 |
} |
| 33 |
|
| 34 |
export const AboutCockpitModal = ({ |
| 35 |
dialogResult |
| 36 |
} : { |
| 37 |
dialogResult: DialogResult<void> |
| 38 |
}) => { |
| 39 |
const [packages, setPackages] = useState<Package[]>([]); |
| 40 |
|
| 41 |
useInit(() => { |
| 42 |
const packages: Package[] = []; |
| 43 |
const cmd = "(set +e; rpm -qa --qf '%{NAME} %{VERSION}\\n'; dpkg-query -f '${Package} ${Version}\n' --show; pacman -Q) 2> /dev/null | grep cockpit | sort"; |
| 44 |
cockpit.spawn(["bash", "-c", cmd], { err: "message" }) |
| 45 |
.then(pkgs => |
| 46 |
pkgs.trim().split("\n") |
| 47 |
.forEach(p => { |
| 48 |
const parts = p.split(" "); |
| 49 |
packages.push({ name: parts[0], version: parts[1] }); |
| 50 |
}) |
| 51 |
) |
| 52 |
.catch(error => console.error("Could not read packages versions:", error)) |
| 53 |
.finally(() => setPackages(packages)); |
| 54 |
}); |
| 55 |
|
| 56 |
return ( |
| 57 |
<AboutModal |
| 58 |
isOpen |
| 59 |
onClose={() => dialogResult.resolve()} |
| 60 |
id="about-cockpit-modal" |
| 61 |
aria-label="About Cockpit and its versions" |
| 62 |
trademark={_("Licensed under GNU LGPL version 2.1")} |
| 63 |
productName={_("Web Console")} |
| 64 |
brandImageSrc="../shell/images/cockpit-icon-gray.svg" |
| 65 |
brandImageAlt={_("Web console logo")} |
| 66 |
> |
| 67 |
<Content component={ContentVariants.p}> |
| 68 |
{_("Cockpit is an interactive Linux server admin interface.")} |
| 69 |
</Content> |
| 70 |
<Content component={ContentVariants.p}> |
| 71 |
<Content component={ContentVariants.a} href="https://cockpit-project.org" target="_blank" rel="noopener noreferrer"> |
| 72 |
{_("Project website")} |
| 73 |
</Content> |
| 74 |
</Content> |
| 75 |
<Content component="dl"> |
| 76 |
{packages === null && <span>{_("Loading packages...")}</span>} |
| 77 |
{packages !== null && packages.map(p => |
| 78 |
<React.Fragment key={p.name}> |
| 79 |
<Content key={p.name} component="dt">{p.name}</Content> |
| 80 |
<Content component="dd">{p.version}</Content> |
| 81 |
</React.Fragment> |
| 82 |
)} |
| 83 |
</Content> |
| 84 |
</AboutModal> |
| 85 |
); |
| 86 |
}; |
| 87 |
|
| 88 |
export interface LangModalProps { |
| 89 |
dialogResult: DialogResult<void>; |
| 90 |
state: ShellState; |
| 91 |
} |
| 92 |
|
| 93 |
export const LangModal = ({ |
| 94 |
dialogResult, |
| 95 |
state |
| 96 |
}: LangModalProps) => { |
| 97 |
const language = document.cookie.replace(/(?:(?:^|.*;\s*)CockpitLang\s*=\s*([^;]*).*$)|^.*$/, "$1") || "en-us"; |
| 98 |
|
| 99 |
const [selected, setSelected] = useState(language); |
| 100 |
const [searchInput, setSearchInput] = useState(""); |
| 101 |
|
| 102 |
function onSelect() { |
| 103 |
if (!selected) |
| 104 |
return; |
| 105 |
|
| 106 |
const cookie = "CockpitLang=" + encodeURIComponent(selected) + "; path=/; expires=Sun, 16 Jul 3567 06:23:41 GMT"; |
| 107 |
document.cookie = cookie; |
| 108 |
window.localStorage.setItem("cockpit.lang", selected); |
| 109 |
window.location.reload(); |
| 110 |
} |
| 111 |
|
| 112 |
return ( |
| 113 |
<Modal isOpen position="top" variant="small" |
| 114 |
id="display-language-modal" |
| 115 |
className="display-language-modal" |
| 116 |
onClose={() => dialogResult.resolve()} |
| 117 |
> |
| 118 |
<ModalHeader title={_("Display language")} /> |
| 119 |
<ModalBody> |
| 120 |
<Flex direction={{ default: 'column' }}> |
| 121 |
<p>{_("Choose the language to be used in the application")}</p> |
| 122 |
<Menu id="display-language-list" |
| 123 |
isPlain |
| 124 |
isScrollable |
| 125 |
className="ct-menu-select-widget" |
| 126 |
onSelect={(_, selected) => setSelected(selected as string)} |
| 127 |
activeItemId={selected} |
| 128 |
selected={selected}> |
| 129 |
<MenuSearch> |
| 130 |
<MenuSearchInput> |
| 131 |
<TextInput |
| 132 |
value={searchInput} |
| 133 |
aria-label={_("Filter menu items")} |
| 134 |
customIcon={<SearchIcon />} |
| 135 |
type="search" |
| 136 |
onChange={(_event, value) => setSearchInput(value)} |
| 137 |
/> |
| 138 |
</MenuSearchInput> |
| 139 |
</MenuSearch> |
| 140 |
<Divider /> |
| 141 |
<MenuContent> |
| 142 |
<MenuList> |
| 143 |
{ |
| 144 |
(() => { |
| 145 |
const locales = state.config.manifest.locales || {}; |
| 146 |
const filteredLocales = Object.keys(locales) |
| 147 |
.filter(key => !searchInput || locales[key].toLowerCase().includes(searchInput.toString().toLowerCase())); |
| 148 |
|
| 149 |
if (filteredLocales.length === 0) { |
| 150 |
return ( |
| 151 |
<MenuItem isDisabled> |
| 152 |
{_("No languages match")} |
| 153 |
</MenuItem> |
| 154 |
); |
| 155 |
} |
| 156 |
return filteredLocales.map(key => { |
| 157 |
return ( |
| 158 |
<MenuItem itemId={key} key={key} data-value={key}> |
| 159 |
{locales[key]} |
| 160 |
</MenuItem> |
| 161 |
); |
| 162 |
}); |
| 163 |
})() |
| 164 |
} |
| 165 |
</MenuList> |
| 166 |
</MenuContent> |
| 167 |
</Menu> |
| 168 |
</Flex> |
| 169 |
</ModalBody> |
| 170 |
<ModalFooter> |
| 171 |
<Button variant='primary' onClick={onSelect}>{_("Select")}</Button> |
| 172 |
<Button variant='link' onClick={() => dialogResult.resolve()}>{_("Cancel")}</Button> |
| 173 |
</ModalFooter> |
| 174 |
</Modal> |
| 175 |
); |
| 176 |
}; |
| 177 |
|
| 178 |
export interface OopsModalProps { |
| 179 |
dialogResult: DialogResult<void>; |
| 180 |
} |
| 181 |
|
| 182 |
export function OopsModal({ dialogResult }: OopsModalProps) { |
| 183 |
return ( |
| 184 |
<Modal isOpen position="top" variant="medium" |
| 185 |
onClose={() => dialogResult.resolve()} |
| 186 |
> |
| 187 |
<ModalHeader title={_("Unexpected error")} /> |
| 188 |
<ModalBody> |
| 189 |
{_("Cockpit had an unexpected internal error.")} |
| 190 |
<br /> |
| 191 |
<br /> |
| 192 |
<span>{("You can try restarting Cockpit by pressing refresh in your browser. The javascript console contains details about this error") + " ("} |
| 193 |
<b>{_("Ctrl-Shift-I")}</b> |
| 194 |
{" " + _("in most browsers") + ")."} |
| 195 |
</span> |
| 196 |
</ModalBody> |
| 197 |
<ModalFooter> |
| 198 |
<Button variant='secondary' onClick={() => dialogResult.resolve()}>{_("Close")}</Button> |
| 199 |
</ModalFooter> |
| 200 |
</Modal> |
| 201 |
); |
| 202 |
} |