| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
import React, { useState } from "react"; |
| 9 |
import { ListingTable } from "cockpit-components-table.jsx"; |
| 10 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 11 |
import { Label } from "@patternfly/react-core/dist/esm/components/Label/index.js"; |
| 12 |
import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js"; |
| 13 |
import { |
| 14 |
Modal, |
| 15 |
ModalBody, |
| 16 |
ModalFooter, |
| 17 |
ModalHeader |
| 18 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 19 |
import { useInit } from "hooks"; |
| 20 |
import { DialogResult } from "dialogs"; |
| 21 |
|
| 22 |
import { ShellState } from "./state"; |
| 23 |
|
| 24 |
const _ = cockpit.gettext; |
| 25 |
|
| 26 |
export interface ActivePagesDialogProps { |
| 27 |
dialogResult: DialogResult<void>; |
| 28 |
state: ShellState; |
| 29 |
} |
| 30 |
|
| 31 |
export const ActivePagesDialog = ({ |
| 32 |
dialogResult, |
| 33 |
state |
| 34 |
}: ActivePagesDialogProps) => { |
| 35 |
function get_pages() { |
| 36 |
const result = []; |
| 37 |
for (const frame of Object.values(state.frames)) { |
| 38 |
if (frame.url) { |
| 39 |
const active = (frame == state.current_frame || |
| 40 |
state.most_recent_path_for_host(frame.host) == frame.path); |
| 41 |
result.push({ |
| 42 |
frame, |
| 43 |
active, |
| 44 |
selected: active, |
| 45 |
displayName: frame.host === "localhost" ? "/" + frame.path : frame.host + ":/" + frame.path, |
| 46 |
}); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// sort the frames by displayName, active ones first |
| 51 |
result.sort(function(a, b) { |
| 52 |
return (a.active ? -2 : 0) + (b.active ? 2 : 0) + |
| 53 |
((a.displayName < b.displayName) ? -1 : 0) + ((b.displayName < a.displayName) ? 1 : 0); |
| 54 |
}); |
| 55 |
|
| 56 |
return result; |
| 57 |
} |
| 58 |
|
| 59 |
const init_pages = useInit(get_pages, [frames]); |
| 60 |
const [pages, setPages] = useState(init_pages); |
| 61 |
|
| 62 |
function onRemove() { |
| 63 |
pages.forEach(element => { |
| 64 |
if (element.selected) { |
| 65 |
state.remove_frame(element.frame.name); |
| 66 |
} |
| 67 |
}); |
| 68 |
dialogResult.resolve(); |
| 69 |
} |
| 70 |
|
| 71 |
const rows = pages.map(page => { |
| 72 |
const columns = [{ |
| 73 |
title: <Split> |
| 74 |
<SplitItem isFilled> |
| 75 |
{page.displayName} |
| 76 |
</SplitItem> |
| 77 |
<SplitItem> |
| 78 |
{page.active && <Label color="blue">{_("active")}</Label>} |
| 79 |
</SplitItem> |
| 80 |
</Split>, |
| 81 |
}]; |
| 82 |
return ({ |
| 83 |
props: { |
| 84 |
key: page.frame.name, |
| 85 |
'data-row-id': page.frame.name |
| 86 |
}, |
| 87 |
columns, |
| 88 |
selected: page.selected, |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
return ( |
| 93 |
<Modal isOpen position="top" variant="small" |
| 94 |
id="active-pages-dialog" |
| 95 |
onClose={() => dialogResult.resolve()} |
| 96 |
> |
| 97 |
<ModalHeader title={_("Active pages")} /> |
| 98 |
<ModalBody> |
| 99 |
<ListingTable showHeader={false} |
| 100 |
columns={[{ title: _("Page name") }]} |
| 101 |
aria-label={_("Active pages")} |
| 102 |
emptyCaption={ _("There are currently no active pages") } |
| 103 |
onSelect={(_event, isSelected, rowIndex) => { |
| 104 |
const new_pages = [...pages]; |
| 105 |
new_pages[rowIndex].selected = isSelected; |
| 106 |
setPages(new_pages); |
| 107 |
}} |
| 108 |
rows={rows} /> |
| 109 |
</ModalBody> |
| 110 |
<ModalFooter> |
| 111 |
<Button variant='primary' onClick={onRemove}>{_("Close selected pages")}</Button> |
| 112 |
<Button variant='link' onClick={() => dialogResult.resolve()}>{_("Cancel")}</Button> |
| 113 |
</ModalFooter> |
| 114 |
</Modal> |
| 115 |
); |
| 116 |
}; |