| 1 |
/* |
| 2 |
* Copyright (C) 2010 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React from "react"; |
| 7 |
|
| 8 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 10 |
import { Tooltip, TooltipPosition } from "@patternfly/react-core/dist/esm/components/Tooltip/index.js"; |
| 11 |
import { Badge } from "@patternfly/react-core/dist/esm/components/Badge/index.js"; |
| 12 |
import { ListingTable } from 'cockpit-components-table.jsx'; |
| 13 |
import { ExclamationCircleIcon, SearchIcon, ThumbtackIcon } from '@patternfly/react-icons'; |
| 14 |
|
| 15 |
import { EmptyStatePanel } from "cockpit-components-empty-state.jsx"; |
| 16 |
|
| 17 |
import cockpit from "cockpit"; |
| 18 |
|
| 19 |
const _ = cockpit.gettext; |
| 20 |
|
| 21 |
export const ServicesList = ({ units, isTimer, onClearAllFilters }) => { |
| 22 |
let columns; |
| 23 |
if (!isTimer) { |
| 24 |
columns = [ |
| 25 |
{ title: _("Unit"), header: true }, |
| 26 |
{ title: _("State") }, |
| 27 |
]; |
| 28 |
} else { |
| 29 |
columns = [ |
| 30 |
{ title: _("Unit"), header: true }, |
| 31 |
{ title: _("Trigger"), props: { width: 20 } }, |
| 32 |
{ title: _("State") }, |
| 33 |
]; |
| 34 |
} |
| 35 |
return ( |
| 36 |
<ListingTable aria-label={_("Systemd units")} |
| 37 |
columns={columns} |
| 38 |
gridBreakPoint={isTimer ? "grid-xl" : "grid-lg"} |
| 39 |
showHeader={false} |
| 40 |
id="services-list" |
| 41 |
rows={ units.map(unit => getServicesRow({ key: unit[0], isTimer, shortId: unit[0], ...unit[1] })) } |
| 42 |
emptyComponent={<EmptyStatePanel icon={SearchIcon} |
| 43 |
paragraph={_("No results match the filter criteria. Clear all filters to show results.")} |
| 44 |
action={<Button id="clear-all-filters" onClick={onClearAllFilters} isInline variant='link'>{_("Clear all filters")}</Button>} |
| 45 |
title={_("No matching results")} /> } |
| 46 |
className="services-list" /> |
| 47 |
); |
| 48 |
}; |
| 49 |
|
| 50 |
const getServicesRow = ({ Id, shortId, AutomaticStartup, UnitFileState, LoadState, HasFailed, IsPinned, CombinedState, LastTriggerTime, NextRunTime, Description, isTimer }) => { |
| 51 |
let displayName = shortId; |
| 52 |
// Remove ".service" from services as this is not necessary |
| 53 |
if (shortId.endsWith(".service")) |
| 54 |
displayName = shortId.substring(0, shortId.length - 8); |
| 55 |
const props = { displayName, Description }; |
| 56 |
|
| 57 |
const enabled = UnitFileState && UnitFileState.includes("enabled"); |
| 58 |
const disabled = UnitFileState && UnitFileState.includes("disabled"); |
| 59 |
const isStatic = UnitFileState && UnitFileState == "static"; |
| 60 |
const masked = LoadState && LoadState.includes("masked"); |
| 61 |
let unitFileState; |
| 62 |
if (enabled || disabled) |
| 63 |
unitFileState = <Badge className="service-unit-file-state" isRead={!enabled}>{AutomaticStartup}</Badge>; |
| 64 |
else |
| 65 |
unitFileState = <span className="service-unit-file-state service-unit-file-state-non-badge">{AutomaticStartup}</span>; |
| 66 |
let tooltipMessage = ""; |
| 67 |
if (enabled) |
| 68 |
tooltipMessage = _("Automatically starts"); |
| 69 |
else if (disabled) |
| 70 |
tooltipMessage = _("Does not automatically start"); |
| 71 |
else if (masked) |
| 72 |
tooltipMessage = _("Forbidden from running"); |
| 73 |
else if (isStatic) |
| 74 |
tooltipMessage = _("Cannot be enabled"); |
| 75 |
|
| 76 |
const columns = [ |
| 77 |
{ |
| 78 |
title: ( |
| 79 |
<div className='service-unit-first-column'> |
| 80 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 81 |
<Button className='service-unit-id' |
| 82 |
isInline |
| 83 |
component="a" |
| 84 |
onClick={() => cockpit.location.go([shortId], cockpit.location.options)} |
| 85 |
variant='link'> |
| 86 |
{props.displayName} |
| 87 |
</Button> |
| 88 |
{IsPinned && |
| 89 |
<Tooltip content={_("Pinned unit")}> |
| 90 |
<ThumbtackIcon className='service-thumbtack-icon-color' /> |
| 91 |
</Tooltip>} |
| 92 |
</Flex> |
| 93 |
{props.Description != shortId && <div className='service-unit-description'>{props.Description}</div>} |
| 94 |
</div> |
| 95 |
) |
| 96 |
}, |
| 97 |
{ |
| 98 |
title: ( |
| 99 |
<Flex |
| 100 |
id={cockpit.format("$0-service-unit-state", Id)} |
| 101 |
flexWrap={{ default: 'wrap', md: 'nowrap' }} |
| 102 |
justifyContent={{ default: 'justifyContentFlexStart', [isTimer ? 'xl' : 'lg']: 'justifyContentFlexEnd' }} |
| 103 |
className='service-unit-status-flex-container'> |
| 104 |
<FlexItem className={"service-unit-status" + (HasFailed ? " service-unit-status-failed" : "")}> |
| 105 |
{HasFailed && <ExclamationCircleIcon className='ct-exclamation-circle' />} |
| 106 |
{CombinedState} |
| 107 |
</FlexItem> |
| 108 |
{tooltipMessage ? <Tooltip id="switch-unit-state" content={tooltipMessage} position={TooltipPosition.left}>{unitFileState}</Tooltip> : unitFileState} |
| 109 |
</Flex> |
| 110 |
), |
| 111 |
props: { |
| 112 |
className: 'service-unit-second-column' |
| 113 |
} |
| 114 |
}, |
| 115 |
]; |
| 116 |
if (isTimer) { |
| 117 |
columns.splice( |
| 118 |
1, 0, |
| 119 |
{ |
| 120 |
title: ( |
| 121 |
<div className="service-unit-triggers"> |
| 122 |
{NextRunTime && <div className="service-unit-next-trigger">{cockpit.format("Next run: $0", NextRunTime)}</div>} |
| 123 |
{LastTriggerTime && <div className="service-unit-last-trigger">{cockpit.format("Last trigger: $0", LastTriggerTime)}</div>} |
| 124 |
</div> |
| 125 |
) |
| 126 |
} |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
return { |
| 131 |
props: { |
| 132 |
'data-goto-unit': shortId, |
| 133 |
id: shortId, |
| 134 |
key: shortId, |
| 135 |
className: HasFailed ? "service-unit-failed" : "", |
| 136 |
}, |
| 137 |
columns |
| 138 |
}; |
| 139 |
}; |