| 1 |
/* |
| 2 |
* Copyright (C) 2019 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React, { useState } from 'react'; |
| 7 |
|
| 8 |
import { Alert, AlertActionCloseButton } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { |
| 11 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 12 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 13 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 14 |
import { TextArea } from "@patternfly/react-core/dist/esm/components/TextArea/index.js"; |
| 15 |
import { EditIcon } from '@patternfly/react-icons'; |
| 16 |
import { ModalError } from 'cockpit-components-inline-notification.jsx'; |
| 17 |
import { superuser } from "superuser"; |
| 18 |
import { useDialogs } from "dialogs.jsx"; |
| 19 |
import { useInit } from "hooks"; |
| 20 |
|
| 21 |
import cockpit from "cockpit"; |
| 22 |
|
| 23 |
import './motdCard.scss'; |
| 24 |
|
| 25 |
const _ = cockpit.gettext; |
| 26 |
|
| 27 |
const MotdEditDialog = ({ text, expectedTag }) => { |
| 28 |
const Dialogs = useDialogs(); |
| 29 |
const [value, setValue] = useState(text); |
| 30 |
const [error, setError] = useState(null); |
| 31 |
const [errorDetail, setErrorDetail] = useState(null); |
| 32 |
|
| 33 |
return ( |
| 34 |
<Modal position="top" |
| 35 |
variant="medium" isOpen |
| 36 |
id="motd-box-edit-modal" |
| 37 |
onClose={Dialogs.close}> |
| 38 |
|
| 39 |
<ModalHeader title={_("Edit /etc/motd")} /> |
| 40 |
<ModalBody> |
| 41 |
<Stack hasGutter> |
| 42 |
{error && |
| 43 |
<ModalError dialogError={error} |
| 44 |
dialogErrorDetail={errorDetail} />} |
| 45 |
<TextArea resizeOrientation="vertical" |
| 46 |
value={value} |
| 47 |
onChange={(_event, value) => setValue(value)} /> |
| 48 |
</Stack> |
| 49 |
</ModalBody> |
| 50 |
<ModalFooter> |
| 51 |
<Button variant='primary' |
| 52 |
onClick={() => cockpit.file("/etc/motd", { superuser: "try", err: "message" }) |
| 53 |
.replace(value, expectedTag) |
| 54 |
.then(Dialogs.close) |
| 55 |
.catch(exc => { |
| 56 |
setError(_("Failed to save changes in /etc/motd")); |
| 57 |
setErrorDetail(exc.message); |
| 58 |
})}> |
| 59 |
{_("Save changes")} |
| 60 |
</Button> |
| 61 |
<Button variant='link' |
| 62 |
onClick={Dialogs.close}> |
| 63 |
{_("Cancel")} |
| 64 |
</Button> |
| 65 |
</ModalFooter> |
| 66 |
</Modal>); |
| 67 |
}; |
| 68 |
|
| 69 |
export const MotdCard = () => { |
| 70 |
const Dialogs = useDialogs(); |
| 71 |
const [motdText, setMotdText] = useState(""); |
| 72 |
const [motdTag, setMotdTag] = useState(null); |
| 73 |
const [motdVisible, setMotdVisible] = useState(false); |
| 74 |
|
| 75 |
useInit(() => { |
| 76 |
cockpit.file("/etc/motd").watch((content, tag) => { |
| 77 |
/* trim initial empty lines and trailing space, but keep initial spaces to not break ASCII art */ |
| 78 |
if (content) |
| 79 |
content = content.trimEnd().replace(/^\s*\n/, ''); |
| 80 |
if (content && content != cockpit.localStorage.getItem('dismissed-motd')) { |
| 81 |
setMotdText(content); |
| 82 |
setMotdTag(tag); |
| 83 |
setMotdVisible(true); |
| 84 |
} else { |
| 85 |
setMotdVisible(false); |
| 86 |
} |
| 87 |
}); |
| 88 |
}); |
| 89 |
|
| 90 |
function hideAlert() { |
| 91 |
setMotdVisible(false); |
| 92 |
cockpit.localStorage.setItem('dismissed-motd', motdText); |
| 93 |
} |
| 94 |
|
| 95 |
if (!motdVisible) |
| 96 |
return null; |
| 97 |
|
| 98 |
const actionClose = <> |
| 99 |
{superuser.allowed && |
| 100 |
<Button icon={<EditIcon />} variant="plain" |
| 101 |
id="motd-box-edit" |
| 102 |
onClick={() => Dialogs.show(<MotdEditDialog text={motdText} expectedTag={motdTag} />)} |
| 103 |
aria-label={_("Edit motd")} />} |
| 104 |
<AlertActionCloseButton onClose={hideAlert} /> |
| 105 |
</>; |
| 106 |
|
| 107 |
return <Alert id="motd-box" isInline className="motd-box" |
| 108 |
variant="custom" |
| 109 |
title={<pre id="motd">{motdText}</pre>} |
| 110 |
actionClose={actionClose} />; |
| 111 |
}; |