| 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 ServicesSettings from "./ServicesSettings"; |
| 8 |
import ServicesMemberOf from "./ServicesMemberOf"; |
| 9 |
import ServicesManagedBy from "./ServicesManagedBy"; |
| 10 |
import BreadCrumb, { BreadCrumbItem } from "src/components/layouts/BreadCrumb"; |
| 11 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 12 |
import { partialServiceToService } from "src/utils/serviceUtils"; |
| 13 |
import ContextualHelpPanel from "src/components/ContextualHelpPanel/ContextualHelpPanel"; |
| 14 |
// Hooks |
| 15 |
import { useServiceSettings } from "src/hooks/useServiceSettingsData"; |
| 16 |
// Redux |
| 17 |
import { useAppDispatch } from "src/store/hooks"; |
| 18 |
import { updateBreadCrumbPath } from "src/store/Global/routes-slice"; |
| 19 |
import DataSpinner from "src/components/layouts/DataSpinner"; |
| 20 |
// Navigation |
| 21 |
import { URL_PREFIX } from "src/navigation/NavRoutes"; |
| 22 |
import { NotFound } from "src/components/errors/PageErrors"; |
| 23 |
import { useSafeParams } from "src/utils/paramsUtils"; |
| 24 |
|
| 25 |
type ServicesParams = { |
| 26 |
id: string; |
| 27 |
}; |
| 28 |
|
| 29 |
// eslint-disable-next-line react/prop-types |
| 30 |
const ServicesTabs = ({ section }) => { |
| 31 |
const { id } = useSafeParams<ServicesParams>(["id"]); |
| 32 |
|
| 33 |
// As the id is sent by React Router DOM partially decoded, this should be fixed |
| 34 |
const decodedId = decodeURIComponent(id); // original ID |
| 35 |
const doubleEncodedId = encodeURIComponent(encodeURIComponent(decodedId)); // double encoded ID |
| 36 |
|
| 37 |
const navigate = useNavigate(); |
| 38 |
const dispatch = useAppDispatch(); |
| 39 |
|
| 40 |
const [breadcrumbItems, setBreadcrumbItems] = React.useState< |
| 41 |
BreadCrumbItem[] |
| 42 |
>([]); |
| 43 |
|
| 44 |
// Contextual links panel |
| 45 |
const [fromPageSelected, setFromPageSelected] = |
| 46 |
React.useState("services-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 serviceSettingsData = useServiceSettings(decodedId as string); |
| 69 |
|
| 70 |
// Tab |
| 71 |
const [activeTabKey, setActiveTabKey] = useState("settings"); |
| 72 |
|
| 73 |
const handleTabClick = ( |
| 74 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 75 |
tabIndex: number | string |
| 76 |
) => { |
| 77 |
if (tabIndex === "settings") { |
| 78 |
navigate("/services/" + doubleEncodedId); |
| 79 |
} else if (tabIndex === "memberof") { |
| 80 |
navigate("/services/" + doubleEncodedId + "/memberof_role"); |
| 81 |
} else if (tabIndex === "managedby") { |
| 82 |
navigate("/services/" + doubleEncodedId + "/managedby_host"); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
React.useEffect(() => { |
| 87 |
// Update breadcrumb route |
| 88 |
const currentPath: BreadCrumbItem[] = [ |
| 89 |
{ |
| 90 |
name: "Services", |
| 91 |
url: URL_PREFIX + "/services", |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: decodedId, |
| 95 |
url: URL_PREFIX + "/services/" + doubleEncodedId, |
| 96 |
isActive: true, |
| 97 |
}, |
| 98 |
]; |
| 99 |
setBreadcrumbItems(currentPath); |
| 100 |
setActiveTabKey("settings"); |
| 101 |
dispatch(updateBreadCrumbPath(currentPath)); |
| 102 |
}, [id]); |
| 103 |
|
| 104 |
// Redirect to the settings page if the section is not defined |
| 105 |
React.useEffect(() => { |
| 106 |
if (!section) { |
| 107 |
navigate(URL_PREFIX + "/services/" + doubleEncodedId); |
| 108 |
} |
| 109 |
setActiveTabKey(section); |
| 110 |
}, [section]); |
| 111 |
|
| 112 |
if (serviceSettingsData.isLoading || !serviceSettingsData.service) { |
| 113 |
return <DataSpinner />; |
| 114 |
} |
| 115 |
|
| 116 |
// Show the 'NotFound' page if the service is not found |
| 117 |
if ( |
| 118 |
!serviceSettingsData.isLoading && |
| 119 |
Object.keys(serviceSettingsData.service).length === 0 |
| 120 |
) { |
| 121 |
return <NotFound />; |
| 122 |
} |
| 123 |
|
| 124 |
const service = partialServiceToService(serviceSettingsData.service); |
| 125 |
const certificates = serviceSettingsData.certData; |
| 126 |
|
| 127 |
return ( |
| 128 |
<> |
| 129 |
<ContextualHelpPanel |
| 130 |
fromPage={fromPageSelected} |
| 131 |
isExpanded={isContextualPanelExpanded} |
| 132 |
onClose={onCloseContextualPanel} |
| 133 |
> |
| 134 |
<PageSection hasBodyWrapper={false}> |
| 135 |
<BreadCrumb breadcrumbItems={breadcrumbItems} /> |
| 136 |
<TitleLayout |
| 137 |
id={service.krbcanonicalname} |
| 138 |
preText="Service:" |
| 139 |
text={service.krbcanonicalname} |
| 140 |
headingLevel="h1" |
| 141 |
/> |
| 142 |
</PageSection> |
| 143 |
<PageSection hasBodyWrapper={false} type="tabs" isFilled> |
| 144 |
<Tabs |
| 145 |
activeKey={activeTabKey} |
| 146 |
onSelect={handleTabClick} |
| 147 |
variant="secondary" |
| 148 |
isBox |
| 149 |
className="pf-v6-u-ml-lg" |
| 150 |
mountOnEnter |
| 151 |
unmountOnExit |
| 152 |
> |
| 153 |
<Tab |
| 154 |
eventKey={"settings"} |
| 155 |
name="details" |
| 156 |
title={<TabTitleText>Settings</TabTitleText>} |
| 157 |
> |
| 158 |
<ServicesSettings |
| 159 |
service={service} |
| 160 |
originalService={serviceSettingsData.originalService} |
| 161 |
metadata={serviceSettingsData.metadata} |
| 162 |
onServiceChange={serviceSettingsData.setService} |
| 163 |
isDataLoading={serviceSettingsData.isLoading} |
| 164 |
onRefresh={serviceSettingsData.refetch} |
| 165 |
isModified={serviceSettingsData.modified} |
| 166 |
onResetValues={serviceSettingsData.resetValues} |
| 167 |
modifiedValues={serviceSettingsData.modifiedValues} |
| 168 |
certData={certificates} |
| 169 |
changeFromPage={changeFromPage} |
| 170 |
onOpenContextualPanel={onOpenContextualPanel} |
| 171 |
/> |
| 172 |
</Tab> |
| 173 |
<Tab |
| 174 |
eventKey={"memberof"} |
| 175 |
name="details" |
| 176 |
title={<TabTitleText>Is a member of</TabTitleText>} |
| 177 |
> |
| 178 |
<ServicesMemberOf service={service} tabSection={section} /> |
| 179 |
</Tab> |
| 180 |
<Tab |
| 181 |
eventKey={"managedby"} |
| 182 |
name="details" |
| 183 |
title={<TabTitleText>Is managed by</TabTitleText>} |
| 184 |
> |
| 185 |
<ServicesManagedBy service={service} /> |
| 186 |
</Tab> |
| 187 |
</Tabs> |
| 188 |
</PageSection> |
| 189 |
</ContextualHelpPanel> |
| 190 |
</> |
| 191 |
); |
| 192 |
}; |
| 193 |
|
| 194 |
export default ServicesTabs; |