| 1 |
import React, { useEffect, useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Td, Th, Tr } from "@patternfly/react-table"; |
| 4 |
// Tables |
| 5 |
import TableLayout from "../../components/layouts/TableLayout"; |
| 6 |
// Data types |
| 7 |
import { Host } from "../../utils/datatypes/globalDataTypes"; |
| 8 |
// Layouts |
| 9 |
import SkeletonOnTableLayout from "../../components/layouts/Skeleton/SkeletonOnTableLayout"; |
| 10 |
// React Router DOM |
| 11 |
import { Link } from "react-router"; |
| 12 |
|
| 13 |
interface HostsData { |
| 14 |
isHostSelectable: (host: Host) => boolean; |
| 15 |
selectedHosts: Host[]; |
| 16 |
selectableHostsTable: Host[]; |
| 17 |
setHostSelected: (host: Host, isSelecting?: boolean) => void; |
| 18 |
clearSelectedHosts: () => void; |
| 19 |
} |
| 20 |
|
| 21 |
interface ButtonsData { |
| 22 |
updateIsDeleteButtonDisabled: (value: boolean) => void; |
| 23 |
isDeletion: boolean; |
| 24 |
updateIsDeletion: (value: boolean) => void; |
| 25 |
} |
| 26 |
|
| 27 |
interface PaginationData { |
| 28 |
selectedPerPage: number; |
| 29 |
updateSelectedPerPage: (selected: number) => void; |
| 30 |
} |
| 31 |
|
| 32 |
interface PropsToTable { |
| 33 |
elementsList: Host[]; |
| 34 |
shownElementsList: Host[]; |
| 35 |
showTableRows: boolean; |
| 36 |
hostsData: HostsData; |
| 37 |
buttonsData: ButtonsData; |
| 38 |
paginationData: PaginationData; |
| 39 |
searchValue: string; |
| 40 |
} |
| 41 |
|
| 42 |
const HostsTable = (props: PropsToTable) => { |
| 43 |
// Retrieve hosts data from props |
| 44 |
const shownHostsList = [...props.shownElementsList]; |
| 45 |
|
| 46 |
// Column names |
| 47 |
const columnNames = { |
| 48 |
fqdn: "Host name", |
| 49 |
description: "Description", |
| 50 |
enrolled: "Enrolled", |
| 51 |
}; |
| 52 |
|
| 53 |
const isHostSelected = (host: Host) => { |
| 54 |
if ( |
| 55 |
props.hostsData.selectedHosts.find( |
| 56 |
(selectedHost) => selectedHost.fqdn[0] === host.fqdn[0] |
| 57 |
) |
| 58 |
) { |
| 59 |
return true; |
| 60 |
} else { |
| 61 |
return false; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
// To allow shift+click to select/deselect multiple rows |
| 66 |
const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState< |
| 67 |
number | null |
| 68 |
>(null); |
| 69 |
const [shifting, setShifting] = useState(false); |
| 70 |
|
| 71 |
// On selecting one single row |
| 72 |
const onSelectHost = (host: Host, rowIndex: number, isSelecting: boolean) => { |
| 73 |
// If the host is shift + selecting the checkboxes, then all intermediate checkboxes should be selected |
| 74 |
if (shifting && recentSelectedRowIndex !== null) { |
| 75 |
const numberSelected = rowIndex - recentSelectedRowIndex; |
| 76 |
const intermediateIndexes = |
| 77 |
numberSelected > 0 |
| 78 |
? Array.from( |
| 79 |
new Array(numberSelected + 1), |
| 80 |
(_x, i) => i + recentSelectedRowIndex |
| 81 |
) |
| 82 |
: Array.from( |
| 83 |
new Array(Math.abs(numberSelected) + 1), |
| 84 |
(_x, i) => i + rowIndex |
| 85 |
); |
| 86 |
intermediateIndexes.forEach((index) => |
| 87 |
props.hostsData.setHostSelected(shownHostsList[index], isSelecting) |
| 88 |
); |
| 89 |
} else { |
| 90 |
props.hostsData.setHostSelected(host, isSelecting); |
| 91 |
} |
| 92 |
setRecentSelectedRowIndex(rowIndex); |
| 93 |
|
| 94 |
// Resetting 'isDisableEnableOp' |
| 95 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 96 |
|
| 97 |
// Update hostIdsSelected array |
| 98 |
if (isSelecting) { |
| 99 |
// Increment the elements selected per page (++) |
| 100 |
props.paginationData.updateSelectedPerPage( |
| 101 |
props.paginationData.selectedPerPage + 1 |
| 102 |
); |
| 103 |
} else { |
| 104 |
// Decrement the elements selected per page (--) |
| 105 |
props.paginationData.updateSelectedPerPage( |
| 106 |
props.paginationData.selectedPerPage - 1 |
| 107 |
); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
// Reset 'selectedHostIds' array if a delete operation has been done |
| 112 |
useEffect(() => { |
| 113 |
if (props.buttonsData.isDeletion) { |
| 114 |
props.hostsData.clearSelectedHosts(); |
| 115 |
props.buttonsData.updateIsDeletion(false); |
| 116 |
} |
| 117 |
}, [props.buttonsData.isDeletion]); |
| 118 |
|
| 119 |
// Enable 'Delete' button (if any host selected) |
| 120 |
useEffect(() => { |
| 121 |
if (props.hostsData.selectedHosts.length > 0) { |
| 122 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 123 |
} |
| 124 |
|
| 125 |
if (props.hostsData.selectedHosts.length === 0) { |
| 126 |
props.buttonsData.updateIsDeleteButtonDisabled(true); |
| 127 |
} |
| 128 |
}, [props.hostsData.selectedHosts]); |
| 129 |
|
| 130 |
// Keyboard event |
| 131 |
useEffect(() => { |
| 132 |
const onKeyDown = (e: KeyboardEvent) => { |
| 133 |
if (e.key === "Shift") { |
| 134 |
setShifting(true); |
| 135 |
} |
| 136 |
}; |
| 137 |
const onKeyUp = (e: KeyboardEvent) => { |
| 138 |
if (e.key === "Shift") { |
| 139 |
setShifting(false); |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
document.addEventListener("keydown", onKeyDown); |
| 144 |
document.addEventListener("keyup", onKeyUp); |
| 145 |
|
| 146 |
return () => { |
| 147 |
document.removeEventListener("keydown", onKeyDown); |
| 148 |
document.removeEventListener("keyup", onKeyUp); |
| 149 |
}; |
| 150 |
}, []); |
| 151 |
|
| 152 |
// Defining table header and body from here to avoid passing specific names to the Table Layout |
| 153 |
const header = ( |
| 154 |
<Tr> |
| 155 |
<Th modifier="wrap" aria-label="Select rows"></Th> |
| 156 |
<Th modifier="wrap">{columnNames.fqdn}</Th> |
| 157 |
<Th modifier="wrap">{columnNames.description}</Th> |
| 158 |
<Th modifier="wrap">{columnNames.enrolled}</Th> |
| 159 |
</Tr> |
| 160 |
); |
| 161 |
|
| 162 |
const body = shownHostsList.map((host, rowIndex) => ( |
| 163 |
<Tr key={host.fqdn} id={host.fqdn}> |
| 164 |
<Td |
| 165 |
dataLabel="checkbox" |
| 166 |
select={{ |
| 167 |
rowIndex, |
| 168 |
onSelect: (_event, isSelecting) => |
| 169 |
onSelectHost(host, rowIndex, isSelecting), |
| 170 |
isSelected: isHostSelected(host), |
| 171 |
isDisabled: !props.hostsData.isHostSelectable(host), |
| 172 |
}} |
| 173 |
/> |
| 174 |
<Td dataLabel={columnNames.fqdn}> |
| 175 |
<Link to={"/hosts/" + host.fqdn} state={host}> |
| 176 |
{host.fqdn} |
| 177 |
</Link> |
| 178 |
</Td> |
| 179 |
<Td dataLabel={columnNames.description}>{host.description}</Td> |
| 180 |
<Td dataLabel={columnNames.enrolled}> |
| 181 |
{host.has_keytab ? "True" : "False"} |
| 182 |
</Td> |
| 183 |
</Tr> |
| 184 |
)); |
| 185 |
|
| 186 |
const skeleton = ( |
| 187 |
<SkeletonOnTableLayout |
| 188 |
rows={4} |
| 189 |
colSpan={9} |
| 190 |
screenreaderText={"Loading table rows"} |
| 191 |
/> |
| 192 |
); |
| 193 |
|
| 194 |
return ( |
| 195 |
<TableLayout |
| 196 |
ariaLabel={"Hosts table"} |
| 197 |
variant={"compact"} |
| 198 |
hasBorders={true} |
| 199 |
classes={"pf-v6-u-mt-md"} |
| 200 |
tableId={"hosts-table"} |
| 201 |
isStickyHeader={true} |
| 202 |
tableHeader={header} |
| 203 |
tableBody={!props.showTableRows ? skeleton : body} |
| 204 |
/> |
| 205 |
); |
| 206 |
}; |
| 207 |
|
| 208 |
export default HostsTable; |