| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Badge, Tab, Tabs, TabTitleText } from "@patternfly/react-core"; |
| 4 |
// Data types |
| 5 |
import { HostGroup } from "src/utils/datatypes/globalDataTypes"; |
| 6 |
import { MembershipDirection } from "src/components/MemberOf/MemberOfToolbar"; |
| 7 |
// Layouts |
| 8 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 9 |
// Navigation |
| 10 |
import { useNavigate } from "react-router"; |
| 11 |
// Hooks |
| 12 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 13 |
// RPC |
| 14 |
import { useGetHostGroupByIdQuery } from "src/services/rpcHostGroups"; |
| 15 |
// 'Members' sections |
| 16 |
import MembersHosts from "src/components/Members/MembersHosts"; |
| 17 |
import MembersHostGroups from "src/components/Members/MembersHostGroups"; |
| 18 |
|
| 19 |
interface PropsToHostGroupsMembers { |
| 20 |
hostGroup: HostGroup; |
| 21 |
tabSection: string; |
| 22 |
} |
| 23 |
|
| 24 |
const HostGroupsMembers = (props: PropsToHostGroupsMembers) => { |
| 25 |
const navigate = useNavigate(); |
| 26 |
|
| 27 |
const hostGroupQuery = useGetHostGroupByIdQuery(props.hostGroup.cn); |
| 28 |
const hostGroupData = hostGroupQuery.data || {}; |
| 29 |
const [hostGroup, setHostGroup] = useState<Partial<HostGroup>>({}); |
| 30 |
|
| 31 |
React.useEffect(() => { |
| 32 |
if (!hostGroupQuery.isFetching && hostGroupData) { |
| 33 |
setHostGroup({ ...hostGroupData }); |
| 34 |
} |
| 35 |
}, [hostGroupData, hostGroupQuery.isFetching]); |
| 36 |
|
| 37 |
const onRefreshHostGroupData = () => { |
| 38 |
hostGroupQuery.refetch(); |
| 39 |
}; |
| 40 |
|
| 41 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 42 |
useUpdateRoute({ pathname: "host-groups", noBreadcrumb: true }); |
| 43 |
|
| 44 |
// Tab counters |
| 45 |
const [hostCount, setHostCount] = React.useState(0); |
| 46 |
const [groupCount, setGroupCount] = React.useState(0); |
| 47 |
|
| 48 |
// group Directions |
| 49 |
const [hostDirection, setHostDirection] = React.useState( |
| 50 |
"direct" as MembershipDirection |
| 51 |
); |
| 52 |
const [groupDirection, setGroupDirection] = React.useState( |
| 53 |
"direct" as MembershipDirection |
| 54 |
); |
| 55 |
|
| 56 |
const updateHostDirection = (direction: MembershipDirection) => { |
| 57 |
if (direction === "direct") { |
| 58 |
setHostCount( |
| 59 |
hostGroup && hostGroup.member_host ? hostGroup.member_host.length : 0 |
| 60 |
); |
| 61 |
} else { |
| 62 |
setHostCount( |
| 63 |
hostGroup && hostGroup.memberindirect_host |
| 64 |
? hostGroup.memberindirect_host.length |
| 65 |
: 0 |
| 66 |
); |
| 67 |
} |
| 68 |
setHostDirection(direction); |
| 69 |
}; |
| 70 |
const updateGroupDirection = (direction: MembershipDirection) => { |
| 71 |
if (direction === "direct") { |
| 72 |
setGroupCount( |
| 73 |
hostGroup && hostGroup.member_hostgroup |
| 74 |
? hostGroup.member_hostgroup.length |
| 75 |
: 0 |
| 76 |
); |
| 77 |
} else { |
| 78 |
setGroupCount( |
| 79 |
hostGroup && hostGroup.memberindirect_hostgroup |
| 80 |
? hostGroup.memberindirect_hostgroup.length |
| 81 |
: 0 |
| 82 |
); |
| 83 |
} |
| 84 |
setGroupDirection(direction); |
| 85 |
}; |
| 86 |
|
| 87 |
React.useEffect(() => { |
| 88 |
if (hostDirection === "direct") { |
| 89 |
setHostCount( |
| 90 |
hostGroup && hostGroup.member_host ? hostGroup.member_host.length : 0 |
| 91 |
); |
| 92 |
} else { |
| 93 |
setHostCount( |
| 94 |
hostGroup && hostGroup.memberindirect_host |
| 95 |
? hostGroup.memberindirect_host.length |
| 96 |
: 0 |
| 97 |
); |
| 98 |
} |
| 99 |
if (groupDirection === "direct") { |
| 100 |
setGroupCount( |
| 101 |
hostGroup && hostGroup.member_hostgroup |
| 102 |
? hostGroup.member_hostgroup.length |
| 103 |
: 0 |
| 104 |
); |
| 105 |
} else { |
| 106 |
setGroupCount( |
| 107 |
hostGroup && hostGroup.memberindirect_hostgroup |
| 108 |
? hostGroup.memberindirect_hostgroup.length |
| 109 |
: 0 |
| 110 |
); |
| 111 |
} |
| 112 |
}, [hostGroup]); |
| 113 |
|
| 114 |
const handleTabClick = ( |
| 115 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 116 |
tabIndex: number | string |
| 117 |
) => { |
| 118 |
navigate("/host-groups/" + props.hostGroup.cn + "/" + tabIndex); |
| 119 |
}; |
| 120 |
|
| 121 |
return ( |
| 122 |
<div style={{ height: `var(--subsettings-calc)` }}> |
| 123 |
<TabLayout id="members"> |
| 124 |
<Tabs |
| 125 |
activeKey={props.tabSection} |
| 126 |
onSelect={handleTabClick} |
| 127 |
isBox={false} |
| 128 |
mountOnEnter |
| 129 |
unmountOnExit |
| 130 |
> |
| 131 |
<Tab |
| 132 |
eventKey={"member_host"} |
| 133 |
name="member_host" |
| 134 |
title={ |
| 135 |
<TabTitleText> |
| 136 |
Hosts{" "} |
| 137 |
<Badge key={0} id="host_count" isRead> |
| 138 |
{hostCount} |
| 139 |
</Badge> |
| 140 |
</TabTitleText> |
| 141 |
} |
| 142 |
> |
| 143 |
<MembersHosts |
| 144 |
entity={hostGroup} |
| 145 |
id={hostGroup.cn as string} |
| 146 |
from="host-groups" |
| 147 |
isDataLoading={hostGroupQuery.isFetching} |
| 148 |
onRefreshData={onRefreshHostGroupData} |
| 149 |
member_host={hostGroup.member_host || []} |
| 150 |
memberindirect_host={hostGroup.memberindirect_host || []} |
| 151 |
setDirection={updateHostDirection} |
| 152 |
direction={hostDirection} |
| 153 |
/> |
| 154 |
</Tab> |
| 155 |
<Tab |
| 156 |
eventKey={"member_hostgroup"} |
| 157 |
name="member_hostgroup" |
| 158 |
title={ |
| 159 |
<TabTitleText> |
| 160 |
Host groups{" "} |
| 161 |
<Badge key={1} id="hostgroup_count" isRead> |
| 162 |
{groupCount} |
| 163 |
</Badge> |
| 164 |
</TabTitleText> |
| 165 |
} |
| 166 |
> |
| 167 |
<MembersHostGroups |
| 168 |
entity={hostGroup} |
| 169 |
id={hostGroup.cn as string} |
| 170 |
from="host-groups" |
| 171 |
isDataLoading={hostGroupQuery.isFetching} |
| 172 |
onRefreshData={onRefreshHostGroupData} |
| 173 |
member_hostgroup={hostGroup.member_hostgroup || []} |
| 174 |
memberindirect_hostgroup={ |
| 175 |
hostGroup.memberindirect_hostgroup || [] |
| 176 |
} |
| 177 |
setDirection={updateGroupDirection} |
| 178 |
direction={groupDirection} |
| 179 |
/> |
| 180 |
</Tab> |
| 181 |
</Tabs> |
| 182 |
</TabLayout> |
| 183 |
</div> |
| 184 |
); |
| 185 |
}; |
| 186 |
|
| 187 |
export default HostGroupsMembers; |