Debian Codegraph @

install/freeipa-webui/src/pages/Hosts/HostsTabs.tsx

raw
1 import React, { useState } from "react";
2 // PatternFly
3 import { PageSection, Tabs, Tab, TabTitleText } from "@patternfly/react-core";
4 // React Router DOM
5 import { useNavigate } from "react-router";
6 // Components
7 import HostsSettings from "./HostsSettings";
8 import HostsMemberOf from "./HostsMemberOf";
9 import HostsManagedBy from "./HostsManagedBy";
10 import ContextualHelpPanel from "src/components/ContextualHelpPanel/ContextualHelpPanel";
11 // Layouts
12 import TitleLayout from "src/components/layouts/TitleLayout";
13 import DataSpinner from "src/components/layouts/DataSpinner";
14 import BreadCrumb, { BreadCrumbItem } from "src/components/layouts/BreadCrumb";
15 // Data types
16 import { Host } from "src/utils/datatypes/globalDataTypes";
17 // Hooks
18 import { useHostSettings } from "src/hooks/useHostSettingsData";
19 // Redux
20 import { useAppDispatch } from "src/store/hooks";
21 import { updateBreadCrumbPath } from "src/store/Global/routes-slice";
22 import { partialHostToHost } from "src/utils/hostUtils";
23 // Navigation
24 import { URL_PREFIX } from "src/navigation/NavRoutes";
25 import { NotFound } from "src/components/errors/PageErrors";
26 import { useSafeParams } from "src/utils/paramsUtils";
27
28 type HostsParams = {
29 fqdn: string;
30 };
31
32 // eslint-disable-next-line react/prop-types
33 const HostsTabs = ({ section }) => {
34 const { fqdn } = useSafeParams<HostsParams>(["fqdn"]);
35 const navigate = useNavigate();
36 const dispatch = useAppDispatch();
37
38 const [breadcrumbItems, setBreadcrumbItems] = React.useState<
39 BreadCrumbItem[]
40 >([]);
41
42 const [hostId, setHostId] = useState("");
43
44 // Contextual links panel
45 const [fromPageSelected, setFromPageSelected] =
46 React.useState("hosts-settings");
47 const [isContextualPanelExpanded, setIsContextualPanelExpanded] =
48 React.useState(false);
49
50 const changeFromPage = (fromPage: string) => {
51 setFromPageSelected(fromPage);
52 };
53
54 const onOpenContextualPanel = () => {
55 setIsContextualPanelExpanded(!isContextualPanelExpanded);
56 };
57
58 const onCloseContextualPanel = () => {
59 setIsContextualPanelExpanded(false);
60 };
61
62 // - Close links panel when tab section is changed
63 React.useEffect(() => {
64 setIsContextualPanelExpanded(false);
65 }, [section]);
66
67 // Data loaded from DB
68 const hostSettingsData = useHostSettings(fqdn);
69
70 // Tab
71 const [activeTabKey, setActiveTabKey] = useState(section);
72
73 const handleTabClick = (
74 _event: React.MouseEvent<HTMLElement, MouseEvent>,
75 tabIndex: number | string
76 ) => {
77 const tabIdx = tabIndex.toString();
78 if (tabIndex === "settings") {
79 navigate("/hosts/" + hostId);
80 } else if (tabIdx.startsWith("memberof")) {
81 if (tabIdx === "memberof") {
82 navigate("/hosts/" + hostId + "/" + tabIdx + "_hostgroup");
83 } else {
84 navigate("/hosts/" + hostId + "/" + tabIdx);
85 }
86 } else if (tabIndex === "managedby") {
87 navigate("/hosts/" + hostId + "/managedby_host");
88 }
89 };
90
91 React.useEffect(() => {
92 setHostId(fqdn);
93 // Update breadcrumb route
94 const currentPath: BreadCrumbItem[] = [
95 {
96 name: "Hosts",
97 url: URL_PREFIX + "/hosts",
98 },
99 {
100 name: fqdn,
101 url: URL_PREFIX + "/hosts/" + fqdn,
102 isActive: true,
103 },
104 ];
105 setBreadcrumbItems(currentPath);
106 setActiveTabKey("settings");
107 dispatch(updateBreadCrumbPath(currentPath));
108 }, [fqdn]);
109
110 // Redirect to the settings page if the section is not defined
111 React.useEffect(() => {
112 if (!section) {
113 navigate(URL_PREFIX + "/hosts/" + hostId);
114 }
115
116 // Case: any of the 'member of' sections is clicked
117 if (section !== "settings" && section !== "managedby") {
118 setActiveTabKey("memberof_hostgroup");
119 } else {
120 setActiveTabKey(section);
121 }
122 }, [section]);
123
124 if (hostSettingsData.isLoading || !hostSettingsData.host) {
125 return <DataSpinner />;
126 }
127
128 // Show the 'NotFound' page if the host is not found
129 if (
130 !hostSettingsData.isLoading &&
131 Object.keys(hostSettingsData.host).length === 0
132 ) {
133 return <NotFound />;
134 }
135
136 const host = hostSettingsData.host;
137
138 return (
139 <>
140 <ContextualHelpPanel
141 fromPage={fromPageSelected}
142 isExpanded={isContextualPanelExpanded}
143 onClose={onCloseContextualPanel}
144 >
145 <PageSection hasBodyWrapper={false}>
146 <BreadCrumb breadcrumbItems={breadcrumbItems} />
147 <TitleLayout
148 id={hostId}
149 preText="Host:"
150 text={hostId}
151 headingLevel="h1"
152 />
153 </PageSection>
154 <PageSection hasBodyWrapper={false} type="tabs" isFilled>
155 <Tabs
156 activeKey={activeTabKey}
157 onSelect={handleTabClick}
158 variant="secondary"
159 isBox
160 className="pf-v6-u-ml-lg"
161 mountOnEnter
162 unmountOnExit
163 >
164 <Tab
165 eventKey={"settings"}
166 name="settings-details"
167 title={<TabTitleText>Settings</TabTitleText>}
168 >
169 <HostsSettings
170 host={host}
171 originalHost={hostSettingsData.originalHost}
172 metadata={hostSettingsData.metadata}
173 certData={hostSettingsData.certData}
174 onHostChange={hostSettingsData.setHost}
175 isDataLoading={hostSettingsData.isFetching}
176 onRefresh={hostSettingsData.refetch}
177 isModified={hostSettingsData.modified}
178 onResetValues={hostSettingsData.resetValues}
179 modifiedValues={hostSettingsData.modifiedValues}
180 changeFromPage={changeFromPage}
181 onOpenContextualPanel={onOpenContextualPanel}
182 />
183 </Tab>
184 <Tab
185 eventKey={"memberof_hostgroup"}
186 name="memberof-details"
187 title={<TabTitleText>Is a member of</TabTitleText>}
188 >
189 <HostsMemberOf
190 host={partialHostToHost(host)}
191 tabSection={section}
192 />
193 </Tab>
194 <Tab
195 eventKey={"managedby"}
196 name="managedby-details"
197 title={<TabTitleText>Is managed by</TabTitleText>}
198 >
199 <HostsManagedBy host={host as Host} />
200 </Tab>
201 </Tabs>
202 </PageSection>
203 </ContextualHelpPanel>
204 </>
205 );
206 };
207
208 export default HostsTabs;