Debian Codegraph @

pkg/lib/cockpit-components-modifications.tsx

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React from 'react';
7 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
8 import { Card, CardBody, CardHeader, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js";
9 import { DataList, DataListCell, DataListItem, DataListItemCells, DataListItemRow } from "@patternfly/react-core/dist/esm/components/DataList/index.js";
10 import {
11 Modal, ModalBody, ModalFooter, ModalHeader
12 } from '@patternfly/react-core/dist/esm/components/Modal/index.js';
13 import { Tab, Tabs } from "@patternfly/react-core/dist/esm/components/Tabs/index.js";
14 import { TextArea } from "@patternfly/react-core/dist/esm/components/TextArea/index.js";
15 import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js";
16 import { CheckIcon, CopyIcon, ExternalLinkAltIcon, OutlinedQuestionCircleIcon } from '@patternfly/react-icons';
17
18 import cockpit from "cockpit";
19 import 'cockpit-components-modifications.css';
20
21 const _ = cockpit.gettext;
22
23 /* Dialog for showing scripts to modify system
24 *
25 * Enables showing shell and ansible script. Shell one is mandatory and ansible one can be omitted.
26 *
27 */
28 export const ModificationsExportDialog = ({ onClose, shell, ansible }: {
29 onClose: () => void;
30 shell: string;
31 ansible?: string | undefined;
32 }) => {
33 const [active_tab, setActiveTab] = React.useState<string | number>(ansible ? "ansible" : "shell");
34 const [copied, setCopied] = React.useState(false);
35 const [timeoutId, setTimeoutId] = React.useState<number | null>(null);
36
37 const handleSelect = (_event: React.MouseEvent, active_tab: string | number) => {
38 setCopied(false);
39 setActiveTab(active_tab);
40 if (timeoutId !== null) {
41 clearTimeout(timeoutId);
42 setTimeoutId(null);
43 }
44 };
45
46 const copyToClipboard = () => {
47 try {
48 navigator.clipboard.writeText((active_tab === "ansible" ? ansible! : shell).trim())
49 .then(() => {
50 setCopied(true);
51 setTimeoutId(setTimeout(() => {
52 setCopied(false);
53 setTimeoutId(null);
54 }, 3000));
55 })
56 .catch(e => console.error('Text could not be copied: ', e ? e.toString() : ""));
57 } catch (error) {
58 console.error('Text could not be copied: ', String(error));
59 }
60 };
61
62 const footer = (
63 <ModalFooter>
64 <Button variant='secondary' className="btn-clipboard" onClick={copyToClipboard} icon={copied ? <Icon status="success"><CheckIcon /></Icon> : <CopyIcon />}>
65 { _("Copy to clipboard") }
66 </Button>
67 <Button variant='secondary' className='btn-cancel' onClick={onClose}>
68 { _("Close") }
69 </Button>
70 </ModalFooter>
71 );
72
73 return (
74 <Modal isOpen className="automation-script-modal"
75 position="top" variant="medium"
76 onClose={onClose}>
77 <ModalHeader title={_("Automation script") } />
78 <ModalBody>
79 <Tabs activeKey={active_tab} onSelect={handleSelect}>
80 { ansible
81 ? <Tab eventKey="ansible" title={_("Ansible")}>
82 <TextArea resizeOrientation='vertical' readOnlyVariant="default" defaultValue={ansible.trim()} />
83 <div className="ansible-docs-link">
84 <OutlinedQuestionCircleIcon />
85 { _("Create new task file with this content.") }
86 <Button variant="link" component="a" href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html"
87 target="_blank" rel="noopener noreferrer"
88 icon={<ExternalLinkAltIcon />}>
89 { _("Ansible roles documentation") }
90 </Button>
91 </div>
92 </Tab>
93 : undefined
94 }
95 <Tab eventKey="shell" title={_("Shell script")}>
96 <TextArea resizeOrientation='vertical' readOnlyVariant="default" defaultValue={shell.trim()} />
97 </Tab>
98 </Tabs>
99 </ModalBody>
100 {footer}
101 </Modal>
102 );
103 };
104
105 /* Display list of modifications in human readable format
106 *
107 * Also show `View automation script` button which opens dialog in which different
108 * scripts are available. With these scripts it is possible to apply the same
109 * configurations to other machines.
110 *
111 * Pass array `entries` to show human readable messages.
112 * Pass string `shell` and `ansible` with scripts.
113 *
114 */
115 export const Modifications = ({ entries, failed, permitted, title, shell, ansible }: {
116 entries: string[] | null;
117 failed?: string;
118 permitted: boolean;
119 title: string;
120 shell: string;
121 ansible?: string;
122 }) => {
123 const [showDialog, setShowDialog] = React.useState(false);
124
125 let emptyRow = null;
126 let fail_message = permitted ? _("No system modifications") : _("The logged in user is not permitted to view system modifications");
127 fail_message = failed || fail_message;
128 if (entries === null) {
129 emptyRow = <DataListItem>
130 <DataListItemRow>
131 <DataListItemCells dataListCells={[<DataListCell key="loading">{_("Loading system modifications...")}</DataListCell>]} />
132 </DataListItemRow>
133 </DataListItem>;
134 }
135 if (entries?.length === 0) {
136 emptyRow = <DataListItem>
137 <DataListItemRow>
138 <DataListItemCells dataListCells={[<DataListCell key={fail_message}>{fail_message}</DataListCell>]} />
139 </DataListItemRow>
140 </DataListItem>;
141 }
142
143 return (
144 <>
145 { showDialog &&
146 <ModificationsExportDialog shell={shell} ansible={ansible}
147 onClose={() => setShowDialog(false)} />
148 }
149 <Card isPlain className="modifications-table">
150 <CardHeader>
151 <CardTitle component="h2">{title}</CardTitle>
152 { !emptyRow &&
153 <Button variant="secondary" onClick={() => setShowDialog(true)}>
154 {_("View automation script")}
155 </Button>
156 }
157 </CardHeader>
158 <CardBody className="contains-list">
159 <DataList aria-label={title} isCompact>
160 { emptyRow ||
161 entries?.map(entry => <DataListItem key={entry}>
162 <DataListItemRow>
163 <DataListItemCells dataListCells={[<DataListCell key={entry}>{entry}</DataListCell>]} />
164 </DataListItemRow>
165 </DataListItem>
166 )
167 }
168 </DataList>
169 </CardBody>
170 </Card>
171 </>
172 );
173 };