| 1 |
/* |
| 2 |
* Copyright (C) 2022 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import React, { useState, useEffect } from 'react'; |
| 6 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 7 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 8 |
import { PowerOffIcon, RedoIcon } from "@patternfly/react-icons"; |
| 9 |
|
| 10 |
import * as timeformat from "timeformat"; |
| 11 |
|
| 12 |
import cockpit from "cockpit"; |
| 13 |
|
| 14 |
import "./shutdownStatus.scss"; |
| 15 |
|
| 16 |
const _ = cockpit.gettext; |
| 17 |
|
| 18 |
const getScheduledShutdown = (setShutdownTime: (t: number) => void, setShutdownType: (t: string) => void) => { |
| 19 |
const client = cockpit.dbus("org.freedesktop.login1"); |
| 20 |
return client.call("/org/freedesktop/login1", "org.freedesktop.DBus.Properties", "Get", |
| 21 |
["org.freedesktop.login1.Manager", "ScheduledShutdown"], { type: "ss" }) |
| 22 |
.then(([result]) => { |
| 23 |
const [type, time] = (result as cockpit.Variant).v as [string, number]; |
| 24 |
setShutdownType(type); |
| 25 |
setShutdownTime(time); |
| 26 |
}) |
| 27 |
.catch(err => console.warn("Failed to get ScheduledShutdown property", err.toString())); |
| 28 |
}; |
| 29 |
|
| 30 |
const cancelShutdownAction = () => { |
| 31 |
const client = cockpit.dbus("org.freedesktop.login1", { superuser: "try" }); |
| 32 |
return client.call("/org/freedesktop/login1", "org.freedesktop.login1.Manager", "CancelScheduledShutdown") |
| 33 |
.then(([cancelled]) => { |
| 34 |
if (!cancelled) { |
| 35 |
console.warn("Unable to cancel shutdown"); |
| 36 |
} |
| 37 |
}) |
| 38 |
.catch(err => console.warn("Failed to cancel shutdown", err.toString())); |
| 39 |
}; |
| 40 |
|
| 41 |
export const ShutDownStatus = () => { |
| 42 |
const [shutdownType, setShutdownType] = useState<string | null>(null); |
| 43 |
const [shutdownTime, setShutdownTime] = useState<number | null>(0); |
| 44 |
|
| 45 |
useEffect(() => { |
| 46 |
getScheduledShutdown(setShutdownTime, setShutdownType); |
| 47 |
// logind does not have a propertieschanged mechanism https://github.com/systemd/systemd/issues/22244 |
| 48 |
cockpit.file("/run/systemd/shutdown/scheduled").watch(() => { |
| 49 |
getScheduledShutdown(setShutdownTime, setShutdownType); |
| 50 |
}); |
| 51 |
}, []); |
| 52 |
|
| 53 |
// We only care about these two types |
| 54 |
if (shutdownType !== "poweroff" && shutdownType !== "reboot") { |
| 55 |
// don't log undefined |
| 56 |
if (shutdownType !== null && shutdownType !== "") { |
| 57 |
console.log(`unsupported shutdown type ${shutdownType}`); |
| 58 |
} |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
let displayDate = null; |
| 63 |
if (shutdownTime) { |
| 64 |
const date = new Date(shutdownTime / 1000); |
| 65 |
const now = new Date(); |
| 66 |
if (date.getFullYear() == now.getFullYear()) { |
| 67 |
displayDate = timeformat.dateTimeNoYear(date); |
| 68 |
} else { |
| 69 |
displayDate = timeformat.dateTime(date); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
let text; |
| 74 |
let cancelText; |
| 75 |
let icon; |
| 76 |
if (shutdownType === "poweroff") { |
| 77 |
icon = <PowerOffIcon className="shutdown-status-poweroff-icon" />; |
| 78 |
text = _("Scheduled poweroff at $0"); |
| 79 |
cancelText = _("Cancel poweroff"); |
| 80 |
} else { |
| 81 |
icon = <RedoIcon className="reboot-status-poweroff-icon" />; |
| 82 |
text = _("Scheduled reboot at $0"); |
| 83 |
cancelText = _("Cancel reboot"); |
| 84 |
} |
| 85 |
|
| 86 |
return ( |
| 87 |
<li id="system-health-shutdown-status"> |
| 88 |
<Flex flexWrap={{ default: 'nowrap' }}> |
| 89 |
<FlexItem>{icon}</FlexItem> |
| 90 |
<Flex id="system-health-shutdown-status-text" spaceItems={{ default: 'spaceItemsNone' }} direction={{ default: 'column' }}> |
| 91 |
{cockpit.format(text, displayDate)} |
| 92 |
<FlexItem> |
| 93 |
<Button variant="link" isInline |
| 94 |
id="system-health-shutdown-status-cancel-btn" |
| 95 |
className="pf-v6-u-font-size-sm" |
| 96 |
onClick={cancelShutdownAction}> |
| 97 |
{cancelText} |
| 98 |
</Button> |
| 99 |
</FlexItem> |
| 100 |
</Flex> |
| 101 |
</Flex> |
| 102 |
</li>); |
| 103 |
}; |