Debian Codegraph @

install/freeipa-webui/src/pages/SudoRules/SudoRulesTable.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 { SudoRule } from "../../utils/datatypes/globalDataTypes";
8 // Utils
9 import { checkEqualStatusSudoRule } from "src/utils/utils";
10 // Layouts
11 import SkeletonOnTableLayout from "../../components/layouts/Skeleton/SkeletonOnTableLayout";
12 // React Router DOM
13 import { Link } from "react-router";
14
15 interface RulesData {
16 isSudoRuleSelectable: (rule: SudoRule) => boolean;
17 selectedRules: SudoRule[];
18 selectableRulesTable: SudoRule[];
19 setRulesSelected: (rule: SudoRule, isSelecting?: boolean) => void;
20 clearSelectedRules: () => void;
21 }
22
23 interface ButtonsData {
24 updateIsDeleteButtonDisabled: (value: boolean) => void;
25 isDeletion: boolean;
26 updateIsDeletion: (value: boolean) => void;
27 updateIsEnableButtonDisabled?: (value: boolean) => void;
28 updateIsDisableButtonDisabled?: (value: boolean) => void;
29 isDisableEnableOp?: boolean;
30 updateIsDisableEnableOp?: (value: boolean) => void;
31 }
32
33 interface PaginationData {
34 selectedPerPage: number;
35 updateSelectedPerPage: (selected: number) => void;
36 }
37
38 interface PropsToTable {
39 shownElementsList: SudoRule[];
40 showTableRows: boolean;
41 rulesData: RulesData;
42 buttonsData: ButtonsData;
43 paginationData: PaginationData;
44 searchValue: string;
45 }
46
47 const SudoRulesTable = (props: PropsToTable) => {
48 // Retrieve groups data from props
49 const shownRulesList = [...props.shownElementsList];
50
51 // Column names
52 const columnNames = {
53 cn: "Rule name",
54 sudoorder: "Sudo order",
55 ipaenabledflag: "Status",
56 description: "Description",
57 };
58
59 // When user status is updated, unselect selected rows
60 useEffect(() => {
61 if (props.buttonsData.isDisableEnableOp) {
62 props.rulesData.clearSelectedRules();
63 }
64 }, [props.buttonsData.isDisableEnableOp]);
65
66 const isRuleSelected = (rule: SudoRule) => {
67 if (
68 props.rulesData.selectedRules.find(
69 (selectedRule) => selectedRule.cn[0] === rule.cn[0]
70 )
71 ) {
72 return true;
73 } else {
74 return false;
75 }
76 };
77
78 // To allow shift+click to select/deselect multiple rows
79 const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState<
80 number | null
81 >(null);
82 const [shifting, setShifting] = useState(false);
83
84 // On selecting one single row
85 const onSelectRule = (
86 rule: SudoRule,
87 rowIndex: number,
88 isSelecting: boolean
89 ) => {
90 // If the host is shift + selecting the checkboxes, then all intermediate checkboxes should be selected
91 if (shifting && recentSelectedRowIndex !== null) {
92 const numberSelected = rowIndex - recentSelectedRowIndex;
93 const intermediateIndexes =
94 numberSelected > 0
95 ? Array.from(
96 new Array(numberSelected + 1),
97 (_x, i) => i + recentSelectedRowIndex
98 )
99 : Array.from(
100 new Array(Math.abs(numberSelected) + 1),
101 (_x, i) => i + rowIndex
102 );
103 intermediateIndexes.forEach((index) =>
104 props.rulesData.setRulesSelected(shownRulesList[index], isSelecting)
105 );
106 } else {
107 props.rulesData.setRulesSelected(rule, isSelecting);
108 }
109 setRecentSelectedRowIndex(rowIndex);
110
111 // Resetting 'isDisableEnableOp'
112 props.buttonsData.updateIsDeleteButtonDisabled(false);
113
114 // Update hostIdsSelected array
115 if (isSelecting) {
116 // Increment the elements selected per page (++)
117 props.paginationData.updateSelectedPerPage(
118 props.paginationData.selectedPerPage + 1
119 );
120 } else {
121 // Decrement the elements selected per page (--)
122 props.paginationData.updateSelectedPerPage(
123 props.paginationData.selectedPerPage - 1
124 );
125 }
126 };
127
128 // Reset 'selectedRoles array if a delete operation has been done
129 useEffect(() => {
130 if (props.buttonsData.isDeletion) {
131 props.rulesData.clearSelectedRules();
132 props.buttonsData.updateIsDeletion(false);
133 }
134 }, [props.buttonsData.isDeletion]);
135
136 // Enable 'Delete' button (if any host selected)
137 useEffect(() => {
138 if (props.rulesData.selectedRules.length > 0) {
139 props.buttonsData.updateIsDeleteButtonDisabled(false);
140 }
141
142 if (props.rulesData.selectedRules.length === 0) {
143 props.buttonsData.updateIsDeleteButtonDisabled(true);
144 }
145 }, [props.rulesData.selectedRules]);
146
147 // Enable 'Delete' and 'Enable|Disable' option buttons (if any rule selected)
148 useEffect(() => {
149 if (props.rulesData.selectedRules.length > 0) {
150 props.buttonsData.updateIsDeleteButtonDisabled(false);
151 const selectedRules: SudoRule[] = props.rulesData.selectedRules;
152 if (
153 props.buttonsData.updateIsDisableButtonDisabled !== undefined &&
154 props.buttonsData.updateIsEnableButtonDisabled !== undefined
155 ) {
156 // Check if selected users have the same status
157 if (selectedRules.length > 0) {
158 const equalStatus = checkEqualStatusSudoRule(
159 selectedRules[0].ipaenabledflag[0],
160 selectedRules
161 );
162 if (equalStatus) {
163 if (selectedRules[0].ipaenabledflag[0]) {
164 // Enabled
165 props.buttonsData.updateIsDisableButtonDisabled(false);
166 props.buttonsData.updateIsEnableButtonDisabled(true);
167 } else {
168 // Disabled
169 props.buttonsData.updateIsDisableButtonDisabled(true);
170 props.buttonsData.updateIsEnableButtonDisabled(false);
171 }
172 } else {
173 // Different status selected -> Disable all buttons
174 props.buttonsData.updateIsDisableButtonDisabled(true);
175 props.buttonsData.updateIsEnableButtonDisabled(true);
176 }
177 }
178 }
179 }
180
181 if (props.rulesData.selectedRules.length === 0) {
182 props.buttonsData.updateIsDeleteButtonDisabled(true);
183 if (
184 props.buttonsData.updateIsEnableButtonDisabled !== undefined &&
185 props.buttonsData.updateIsDisableButtonDisabled !== undefined
186 ) {
187 props.buttonsData.updateIsDisableButtonDisabled(true);
188 props.buttonsData.updateIsEnableButtonDisabled(true);
189 }
190 }
191 }, [props.rulesData.selectedRules]);
192
193 // Keyboard event
194 useEffect(() => {
195 const onKeyDown = (e: KeyboardEvent) => {
196 if (e.key === "Shift") {
197 setShifting(true);
198 }
199 };
200 const onKeyUp = (e: KeyboardEvent) => {
201 if (e.key === "Shift") {
202 setShifting(false);
203 }
204 };
205
206 document.addEventListener("keydown", onKeyDown);
207 document.addEventListener("keyup", onKeyUp);
208
209 return () => {
210 document.removeEventListener("keydown", onKeyDown);
211 document.removeEventListener("keyup", onKeyUp);
212 };
213 }, []);
214
215 // Defining table header and body from here to avoid passing specific names to the Table Layout
216 const header = (
217 <Tr>
218 <Th modifier="wrap" aria-label="Select rows"></Th>
219 <Th modifier="wrap">{columnNames.cn}</Th>
220 <Th modifier="wrap">{columnNames.sudoorder}</Th>
221 <Th modifier="wrap">{columnNames.ipaenabledflag}</Th>
222 <Th modifier="wrap">{columnNames.description}</Th>
223 </Tr>
224 );
225
226 const body = shownRulesList.map((rule, rowIndex) => (
227 <Tr key={rule.cn} id={rule.cn}>
228 <Td
229 dataLabel="checkbox"
230 select={{
231 rowIndex,
232 onSelect: (_event, isSelecting) =>
233 onSelectRule(rule, rowIndex, isSelecting),
234 isSelected: isRuleSelected(rule),
235 isDisabled: !props.rulesData.isSudoRuleSelectable(rule),
236 }}
237 />
238 <Td dataLabel={columnNames.cn}>
239 <Link to={"/sudo-rules/" + rule.cn} state={rule}>
240 {rule.cn}
241 </Link>
242 </Td>
243 <Td dataLabel={columnNames.sudoorder}>{rule.sudoorder}</Td>
244 <Td dataLabel={columnNames.ipaenabledflag}>
245 {rule.ipaenabledflag[0] ? "Enabled" : "Disabled"}
246 </Td>
247 <Td dataLabel={columnNames.description}>{rule.description}</Td>
248 </Tr>
249 ));
250
251 const skeleton = (
252 <SkeletonOnTableLayout
253 rows={4}
254 colSpan={9}
255 screenreaderText={"Loading table rows"}
256 />
257 );
258
259 return (
260 <TableLayout
261 ariaLabel={"Sudo rules table"}
262 variant={"compact"}
263 hasBorders={true}
264 classes={"pf-v6-u-mt-md"}
265 tableId={"sudorules-table"}
266 isStickyHeader={true}
267 tableHeader={header}
268 tableBody={!props.showTableRows ? skeleton : body}
269 />
270 );
271 };
272
273 export default SudoRulesTable;