Debian Codegraph @

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