Debian Codegraph @

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