|
| 1 | +import React, { useState, useEffect, useMemo } from "react"; |
| 2 | +import { Tabs, Tab, Collapse } from "react-bootstrap"; |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { useHistory, useParams, useLocation } from "react-router-dom"; |
| 5 | +import AdminDashboard from "../dashboard"; |
| 6 | +import RoleManagement from "../roles"; |
| 7 | +import UserManagement from "../users"; |
| 8 | +import Organization from "../organization"; |
| 9 | +import { StorageService } from "@formsflow/service"; |
| 10 | +import { BreadCrumbs, UpArrowIcon, DownArrowIcon } from "@formsflow/components"; |
| 11 | +import { MULTITENANCY_ENABLED } from "../../constants"; |
| 12 | + |
| 13 | +interface ManageProps { |
| 14 | + props: any; |
| 15 | + setTab: (tab: string) => void; |
| 16 | + setDashboardCount?: React.Dispatch<React.SetStateAction<number | undefined>>; |
| 17 | + setRoleCount?: React.Dispatch<React.SetStateAction<number | undefined>>; |
| 18 | + setUserCount?: React.Dispatch<React.SetStateAction<number | undefined>>; |
| 19 | +} |
| 20 | + |
| 21 | +const Manage: React.FC<ManageProps> = ({ props, setTab, setDashboardCount, setRoleCount, setUserCount }) => { |
| 22 | + const { t } = useTranslation(); |
| 23 | + const history = useHistory(); |
| 24 | + const { tenantId, tab: urlTab } = useParams<{ tenantId?: string; tab?: string }>(); |
| 25 | + const location = useLocation(); |
| 26 | + const [tabContentExpanded, setTabContentExpanded] = useState<boolean>(true); |
| 27 | + |
| 28 | + const userRoles = JSON.parse( |
| 29 | + StorageService.get(StorageService.User.USER_ROLE) || "[]" |
| 30 | + ); |
| 31 | + |
| 32 | + const isDashboardManager = userRoles?.includes("manage_dashboard_authorizations"); |
| 33 | + const isRoleManager = userRoles?.includes("manage_roles"); |
| 34 | + const isUserManager = userRoles?.includes("manage_users"); |
| 35 | + |
| 36 | + const baseUrl = MULTITENANCY_ENABLED ? `/tenant/${tenantId}/` : "/"; |
| 37 | + |
| 38 | + // Get active tab from URL or default to organization |
| 39 | + const activeTab = useMemo((): string => { |
| 40 | + if (urlTab) { |
| 41 | + // Validate that the tab from URL is valid |
| 42 | + const validTabs = ["organization", "dashboard", "users", "roles"]; |
| 43 | + if (validTabs.includes(urlTab)) { |
| 44 | + // Check permissions for restricted tabs |
| 45 | + if (urlTab === "dashboard" && !isDashboardManager) return "organization"; |
| 46 | + if (urlTab === "users" && !isUserManager) return "organization"; |
| 47 | + if (urlTab === "roles" && !isRoleManager) return "organization"; |
| 48 | + return urlTab; |
| 49 | + } |
| 50 | + } |
| 51 | + // If no tab in URL or invalid tab, check if we're at /admin (without tab) |
| 52 | + if (location.pathname === `${baseUrl}admin` || location.pathname === `${baseUrl}admin/`) { |
| 53 | + return "organization"; |
| 54 | + } |
| 55 | + return "organization"; |
| 56 | + }, [urlTab, location.pathname, baseUrl, isDashboardManager, isUserManager, isRoleManager]); |
| 57 | + |
| 58 | + // Redirect to default tab if on /admin without a tab |
| 59 | + useEffect(() => { |
| 60 | + if (location.pathname === `${baseUrl}admin` || location.pathname === `${baseUrl}admin/`) { |
| 61 | + history.replace(`${baseUrl}admin/organization`); |
| 62 | + } |
| 63 | + }, [location.pathname, baseUrl, history]); |
| 64 | + |
| 65 | + const handleTabChange = (key: string | null) => { |
| 66 | + if (key) { |
| 67 | + const tabNameMap: { [key: string]: string } = { |
| 68 | + "organization": "Organization", |
| 69 | + "dashboard": "Dashboard", |
| 70 | + "users": "Users", |
| 71 | + "roles": "Roles" |
| 72 | + }; |
| 73 | + setTab(tabNameMap[key] || "Organization"); |
| 74 | + // Navigate to the tab route - this will update the URL and activeTab will update via useMemo |
| 75 | + history.push(`${baseUrl}admin/${key}`); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const handleTabContentToggle = () => { |
| 80 | + setTabContentExpanded(!tabContentExpanded); |
| 81 | + }; |
| 82 | + |
| 83 | + const breadcrumbItems = [ |
| 84 | + { label: t("Manage"), id: "manage" } |
| 85 | + ]; |
| 86 | + |
| 87 | + return ( |
| 88 | + <div className="manage-container"> |
| 89 | + <div className="header-section-1"> |
| 90 | + <div className="section-seperation-left"> |
| 91 | + <BreadCrumbs |
| 92 | + items={breadcrumbItems} |
| 93 | + variant="default" |
| 94 | + dataTestId="manage-breadcrumbs" |
| 95 | + /> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div className="manage-tabs-wrapper"> |
| 100 | + <div className="manage-tabs-header"> |
| 101 | + <Tabs |
| 102 | + activeKey={activeTab} |
| 103 | + onSelect={handleTabChange} |
| 104 | + id="manage-tabs" |
| 105 | + className="pill-tabs" |
| 106 | + > |
| 107 | + <Tab eventKey="organization" title={t("Organization")} /> |
| 108 | + {isDashboardManager && ( |
| 109 | + <Tab eventKey="dashboard" title={t("Dashboards")} /> |
| 110 | + )} |
| 111 | + {isUserManager && ( |
| 112 | + <Tab eventKey="users" title={t("Users")} /> |
| 113 | + )} |
| 114 | + {isRoleManager && ( |
| 115 | + <Tab eventKey="roles" title={t("Roles")} /> |
| 116 | + )} |
| 117 | + </Tabs> |
| 118 | + <div |
| 119 | + className="manage-tabs-chevron" |
| 120 | + onClick={handleTabContentToggle} |
| 121 | + role="button" |
| 122 | + tabIndex={0} |
| 123 | + onKeyDown={(e) => { |
| 124 | + if (e.key === "Enter" || e.key === " ") { |
| 125 | + e.preventDefault(); |
| 126 | + handleTabContentToggle(); |
| 127 | + } |
| 128 | + }} |
| 129 | + > |
| 130 | + {tabContentExpanded ? ( |
| 131 | + <UpArrowIcon className="svgIcon-medium-dark" /> |
| 132 | + ) : ( |
| 133 | + <DownArrowIcon className="svgIcon-medium-dark" /> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + <Collapse in={tabContentExpanded}> |
| 138 | + <div> |
| 139 | + <div className="tab-content"> |
| 140 | + {activeTab === "organization" && ( |
| 141 | + <div className="manage-content"> |
| 142 | + <Organization {...props} /> |
| 143 | + </div> |
| 144 | + )} |
| 145 | + {activeTab === "dashboard" && isDashboardManager && ( |
| 146 | + <div className="manage-content"> |
| 147 | + <AdminDashboard |
| 148 | + {...props} |
| 149 | + setTab={setTab} |
| 150 | + setCount={setDashboardCount} |
| 151 | + /> |
| 152 | + </div> |
| 153 | + )} |
| 154 | + {activeTab === "users" && isUserManager && ( |
| 155 | + <div className="manage-content"> |
| 156 | + <UserManagement |
| 157 | + {...props} |
| 158 | + setTab={setTab} |
| 159 | + setCount={setUserCount} |
| 160 | + /> |
| 161 | + </div> |
| 162 | + )} |
| 163 | + {activeTab === "roles" && isRoleManager && ( |
| 164 | + <div className="manage-content"> |
| 165 | + <RoleManagement |
| 166 | + {...props} |
| 167 | + setTab={setTab} |
| 168 | + setCount={setRoleCount} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + )} |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + </Collapse> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + ); |
| 178 | +}; |
| 179 | + |
| 180 | +export default Manage; |
0 commit comments