1
- import type { DeploymentConfig } from "api/api" ;
2
- import { deploymentConfig } from "api/queries/deployment" ;
3
- import type { AuthorizationResponse } from "api/typesGenerated" ;
4
- import { ErrorAlert } from "components/Alert/ErrorAlert" ;
1
+ import type { AuthorizationResponse , Organization } from "api/typesGenerated" ;
5
2
import { Loader } from "components/Loader/Loader" ;
6
3
import { Margins } from "components/Margins/Margins" ;
7
4
import { Stack } from "components/Stack/Stack" ;
8
5
import { useAuthenticated } from "contexts/auth/RequireAuth" ;
9
6
import { RequirePermission } from "contexts/auth/RequirePermission" ;
10
- import type { Permissions } from "contexts/auth/permissions" ;
11
7
import { useDashboard } from "modules/dashboard/useDashboard" ;
12
8
import { type FC , Suspense , createContext , useContext } from "react" ;
13
- import { useQuery } from "react-query" ;
14
- import { Outlet , useLocation } from "react-router-dom" ;
9
+ import { Outlet , useParams } from "react-router-dom" ;
15
10
import { Sidebar } from "./Sidebar" ;
16
11
17
- type ManagementSettingsValue = Readonly < {
18
- deploymentValues : DeploymentConfig | undefined ;
19
- } > ;
20
-
21
12
export const ManagementSettingsContext = createContext <
22
13
ManagementSettingsValue | undefined
23
14
> ( undefined ) ;
24
15
16
+ type ManagementSettingsValue = Readonly < {
17
+ organizations : readonly Organization [ ] ;
18
+ organization ?: Organization ;
19
+ } > ;
20
+
25
21
export const useManagementSettings = ( ) : ManagementSettingsValue => {
26
22
const context = useContext ( ManagementSettingsContext ) ;
27
23
if ( ! context ) {
@@ -47,134 +43,47 @@ export const canEditOrganization = (
47
43
) ;
48
44
} ;
49
45
50
- export const isManagementRoutePermitted = (
51
- locationPath : string ,
52
- permissions : Permissions ,
53
- showOrganizations : boolean ,
54
- ) : boolean => {
55
- if ( ! locationPath . startsWith ( "/" ) ) {
56
- return false ;
57
- }
58
-
59
- if ( locationPath . startsWith ( "/organizations" ) ) {
60
- return showOrganizations ;
61
- }
62
-
63
- if ( ! locationPath . startsWith ( "/deployment" ) ) {
64
- return false ;
65
- }
66
-
67
- // Logic for deployment routes should mirror the conditions used to display
68
- // the sidebar tabs from SidebarView.tsx
69
- const href = locationPath . replace ( / ^ \/ d e p l o y m e n t / , "" ) ;
70
-
71
- if ( href === "/" || href === "" ) {
72
- const hasAtLeastOnePermission = Object . values ( permissions ) . some ( ( v ) => v ) ;
73
- return hasAtLeastOnePermission ;
74
- }
75
- if ( href . startsWith ( "/general" ) ) {
76
- return permissions . viewDeploymentValues ;
77
- }
78
- if ( href . startsWith ( "/licenses" ) ) {
79
- return permissions . viewAllLicenses ;
80
- }
81
- if ( href . startsWith ( "/appearance" ) ) {
82
- return permissions . editDeploymentValues ;
83
- }
84
- if ( href . startsWith ( "/userauth" ) ) {
85
- return permissions . viewDeploymentValues ;
86
- }
87
- if ( href . startsWith ( "/external-auth" ) ) {
88
- return permissions . viewDeploymentValues ;
89
- }
90
- if ( href . startsWith ( "/network" ) ) {
91
- return permissions . viewDeploymentValues ;
92
- }
93
- if ( href . startsWith ( "/workspace-proxies" ) ) {
94
- return permissions . readWorkspaceProxies ;
95
- }
96
- if ( href . startsWith ( "/security" ) ) {
97
- return permissions . viewDeploymentValues ;
98
- }
99
- if ( href . startsWith ( "/observability" ) ) {
100
- return permissions . viewDeploymentValues ;
101
- }
102
- if ( href . startsWith ( "/users" ) ) {
103
- return permissions . viewAllUsers ;
104
- }
105
- if ( href . startsWith ( "/notifications" ) ) {
106
- return permissions . viewNotificationTemplate ;
107
- }
108
- if ( href . startsWith ( "/oauth2-provider" ) ) {
109
- return permissions . viewExternalAuthConfig ;
110
- }
111
-
112
- return false ;
113
- } ;
114
-
115
- /**
116
- * A multi-org capable settings page layout.
117
- *
118
- * If multi-org is not enabled or licensed, this is the wrong layout to use.
119
- * See DeploySettingsLayoutInner instead.
120
- */
121
- export const ManagementSettingsLayout : FC = ( ) => {
122
- const location = useLocation ( ) ;
123
- const { showOrganizations } = useDashboard ( ) ;
46
+ const ManagementSettingsLayout : FC = ( ) => {
124
47
const { permissions } = useAuthenticated ( ) ;
125
- const deploymentConfigQuery = useQuery ( {
126
- ...deploymentConfig ( ) ,
127
- enabled : permissions . viewDeploymentValues ,
128
- } ) ;
129
-
130
- // Have to make check more specific, because if the query is disabled, it
131
- // will be forever stuck in the loading state. The loading state is only
132
- // relevant to owners right now
133
- if ( permissions . viewDeploymentValues && deploymentConfigQuery . isLoading ) {
134
- return < Loader /> ;
135
- }
136
-
137
- const canViewAtLeastOneTab =
48
+ const { organizations } = useDashboard ( ) ;
49
+ const { organization : orgName } = useParams ( ) as {
50
+ organization ?: string ;
51
+ } ;
52
+
53
+ // The deployment settings page also contains users, audit logs, groups and
54
+ // organizations, so this page must be visible if you can see any of these.
55
+ const canViewDeploymentSettingsPage =
138
56
permissions . viewDeploymentValues ||
139
57
permissions . viewAllUsers ||
140
58
permissions . editAnyOrganization ||
141
59
permissions . viewAnyAuditLog ;
142
60
143
- return (
144
- < RequirePermission
145
- permitted = {
146
- canViewAtLeastOneTab &&
147
- isManagementRoutePermitted (
148
- location . pathname ,
149
- permissions ,
150
- showOrganizations ,
151
- )
152
- }
153
- unpermittedRedirect = {
154
- canViewAtLeastOneTab && ! location . pathname . startsWith ( "/deployment" )
155
- ? "/deployment"
156
- : "/workspaces"
157
- }
158
- >
159
- < Margins >
160
- < Stack css = { { padding : "48px 0" } } direction = "row" spacing = { 6 } >
161
- < Sidebar />
162
-
163
- < main css = { { flexGrow : 1 } } >
164
- { deploymentConfigQuery . isError && (
165
- < ErrorAlert error = { deploymentConfigQuery . error } />
166
- ) }
61
+ const organization =
62
+ organizations && orgName
63
+ ? organizations . find ( ( org ) => org . name === orgName )
64
+ : undefined ;
167
65
168
- < ManagementSettingsContext . Provider
169
- value = { { deploymentValues : deploymentConfigQuery . data } }
170
- >
66
+ return (
67
+ < RequirePermission isFeatureVisible = { canViewDeploymentSettingsPage } >
68
+ < ManagementSettingsContext . Provider
69
+ value = { {
70
+ organizations,
71
+ organization,
72
+ } }
73
+ >
74
+ < Margins >
75
+ < Stack css = { { padding : "48px 0" } } direction = "row" spacing = { 6 } >
76
+ < Sidebar />
77
+ < main css = { { flexGrow : 1 } } >
171
78
< Suspense fallback = { < Loader /> } >
172
79
< Outlet />
173
80
</ Suspense >
174
- </ ManagementSettingsContext . Provider >
175
- </ main >
176
- </ Stack >
177
- </ Margins >
81
+ </ main >
82
+ </ Stack >
83
+ </ Margins >
84
+ </ ManagementSettingsContext . Provider >
178
85
</ RequirePermission >
179
86
) ;
180
87
} ;
88
+
89
+ export default ManagementSettingsLayout ;
0 commit comments