Debian Codegraph @

install/freeipa-webui/src/pages/HBACServiceGroups/HBACServiceGroupsTable.tsx

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