|
| 1 | +import { deploymentConfig } from "api/queries/deployment"; |
| 2 | +import type { |
| 3 | + AuthorizationResponse, |
| 4 | + DeploymentConfig, |
| 5 | + Organization, |
| 6 | +} from "api/typesGenerated"; |
| 7 | +import { Loader } from "components/Loader/Loader"; |
| 8 | +import { Margins } from "components/Margins/Margins"; |
| 9 | +import { Stack } from "components/Stack/Stack"; |
| 10 | +import { useAuthenticated } from "contexts/auth/RequireAuth"; |
| 11 | +import { RequirePermission } from "contexts/auth/RequirePermission"; |
| 12 | +import { useDashboard } from "modules/dashboard/useDashboard"; |
| 13 | +import { createContext, type FC, Suspense, useContext } from "react"; |
| 14 | +import { useQuery } from "react-query"; |
| 15 | +import { Navigate, Outlet, useNavigate, useParams } from "react-router-dom"; |
| 16 | +import { Sidebar } from "./Sidebar"; |
| 17 | +import { ErrorAlert } from "components/Alert/ErrorAlert"; |
| 18 | + |
| 19 | +export const ManagementSettingsContext = createContext< |
| 20 | + { deploymentValues: DeploymentConfig } | undefined |
| 21 | +>(undefined); |
| 22 | + |
| 23 | +type ManagementSettingsValue = Readonly<{ |
| 24 | + deploymentValues: DeploymentConfig; |
| 25 | + organizations: readonly Organization[]; |
| 26 | + organization?: Organization; |
| 27 | +}>; |
| 28 | + |
| 29 | +export const useManagementSettings = (): ManagementSettingsValue => { |
| 30 | + const context = useContext(ManagementSettingsContext); |
| 31 | + if (!context) { |
| 32 | + throw new Error( |
| 33 | + "useManagementSettings should be used inside of ManagementSettingsLayout", |
| 34 | + ); |
| 35 | + } |
| 36 | + const { organizations } = useDashboard(); |
| 37 | + const { organization: orgName } = useParams() as { |
| 38 | + organization?: string; |
| 39 | + }; |
| 40 | + |
| 41 | + const organization = |
| 42 | + organizations && orgName |
| 43 | + ? organizations.find((org) => org.name === orgName) |
| 44 | + : undefined; |
| 45 | + |
| 46 | + return { |
| 47 | + deploymentValues: context.deploymentValues, |
| 48 | + organizations, |
| 49 | + organization, |
| 50 | + }; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Return true if the user can edit the organization settings or its members. |
| 55 | + */ |
| 56 | +export const canEditOrganization = ( |
| 57 | + permissions: AuthorizationResponse | undefined, |
| 58 | +) => { |
| 59 | + return ( |
| 60 | + permissions !== undefined && |
| 61 | + (permissions.editOrganization || |
| 62 | + permissions.editMembers || |
| 63 | + permissions.editGroups) |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * A multi-org capable settings page layout. |
| 69 | + * |
| 70 | + * If multi-org is not enabled or licensed, this is the wrong layout to use. |
| 71 | + * See DeploySettingsLayoutInner instead. |
| 72 | + */ |
| 73 | +export const ManagementSettingsLayout: FC = () => { |
| 74 | + const { permissions } = useAuthenticated(); |
| 75 | + const deploymentConfigQuery = useQuery(deploymentConfig()); |
| 76 | + |
| 77 | + // The deployment settings page also contains users, audit logs, groups and |
| 78 | + // organizations, so this page must be visible if you can see any of these. |
| 79 | + const canViewDeploymentSettingsPage = |
| 80 | + permissions.viewDeploymentValues || |
| 81 | + permissions.viewAllUsers || |
| 82 | + permissions.editAnyOrganization || |
| 83 | + permissions.viewAnyAuditLog; |
| 84 | + |
| 85 | + if (deploymentConfigQuery.error) { |
| 86 | + return <ErrorAlert error={deploymentConfigQuery.error} />; |
| 87 | + } |
| 88 | + |
| 89 | + if (!deploymentConfigQuery.data) { |
| 90 | + return <Loader />; |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <Margins> |
| 95 | + <Stack css={{ padding: "48px 0" }} direction="row" spacing={6}> |
| 96 | + <Sidebar /> |
| 97 | + <main css={{ width: "100%" }}> |
| 98 | + <Suspense fallback={<Loader />}> |
| 99 | + <RequirePermission isFeatureVisible={canViewDeploymentSettingsPage}> |
| 100 | + <ManagementSettingsContext.Provider |
| 101 | + value={{ deploymentValues: deploymentConfigQuery.data }} |
| 102 | + > |
| 103 | + <Outlet /> |
| 104 | + </ManagementSettingsContext.Provider> |
| 105 | + </RequirePermission> |
| 106 | + </Suspense> |
| 107 | + </main> |
| 108 | + </Stack> |
| 109 | + </Margins> |
| 110 | + ); |
| 111 | +}; |
0 commit comments