@@ -11,40 +11,69 @@ import {
11
11
} from "react-router-dom" ;
12
12
import { getErrorMessage } from "api/errors" ;
13
13
import { groups } from "api/queries/groups" ;
14
+ import { organizationPermissions } from "api/queries/organizations" ;
14
15
import type { Organization } from "api/typesGenerated" ;
16
+ import { EmptyState } from "components/EmptyState/EmptyState" ;
15
17
import { displayError } from "components/GlobalSnackbar/utils" ;
18
+ import { Loader } from "components/Loader/Loader" ;
16
19
import { PageHeader , PageHeaderTitle } from "components/PageHeader/PageHeader" ;
17
- import { useAuthenticated } from "contexts/auth/RequireAuth" ;
18
- import { useDashboard } from "modules/dashboard/useDashboard" ;
19
20
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility" ;
20
21
import { pageTitle } from "utils/page" ;
21
22
import { useOrganizationSettings } from "../ManagementSettingsLayout" ;
22
23
import GroupsPageView from "./GroupsPageView" ;
23
24
24
25
export const GroupsPage : FC = ( ) => {
25
- const { permissions } = useAuthenticated ( ) ;
26
- const { createGroup : canCreateGroup } = permissions ;
27
26
const feats = useFeatureVisibility ( ) ;
28
- const { experiments } = useDashboard ( ) ;
29
27
const location = useLocation ( ) ;
30
- const { organization = "default" } = useParams ( ) as { organization : string } ;
31
- const groupsQuery = useQuery ( groups ( organization ) ) ;
28
+ const { organization : organizationName } = useParams ( ) as {
29
+ organization : string ;
30
+ } ;
31
+ const groupsQuery = useQuery (
32
+ organizationName
33
+ ? groups ( organizationName )
34
+ : { enabled : false } ,
35
+ ) ;
32
36
const { organizations } = useOrganizationSettings ( ) ;
37
+ // TODO: If we could query permissions based on the name then we would not
38
+ // have to cascade off the organizations query.
39
+ const organization = organizations ?. find ( ( o ) => o . name === organizationName ) ;
40
+ const permissionsQuery = useQuery (
41
+ organization
42
+ ? organizationPermissions ( organization . id )
43
+ : { enabled : false } ,
44
+ ) ;
33
45
34
46
useEffect ( ( ) => {
35
47
if ( groupsQuery . error ) {
36
48
displayError (
37
- getErrorMessage ( groupsQuery . error , "Error on loading groups." ) ,
49
+ getErrorMessage ( groupsQuery . error , "Unable to load groups." ) ,
38
50
) ;
39
51
}
40
52
} , [ groupsQuery . error ] ) ;
41
53
42
- const canViewOrganizations =
43
- feats . multiple_organizations && experiments . includes ( "multi-organization" ) ;
44
- if ( canViewOrganizations && location . pathname === "/deployment/groups" ) {
45
- const defaultName =
46
- getOrganizationNameByDefault ( organizations ) ?? "default" ;
47
- return < Navigate to = { `/organizations/${ defaultName } /groups` } replace /> ;
54
+ useEffect ( ( ) => {
55
+ if ( permissionsQuery . error ) {
56
+ displayError (
57
+ getErrorMessage ( permissionsQuery . error , "Unable to load permissions." ) ,
58
+ ) ;
59
+ }
60
+ } , [ permissionsQuery . error ] ) ;
61
+
62
+ if ( ! organizations ) {
63
+ return < Loader /> ;
64
+ }
65
+
66
+ if ( ! organizationName ) {
67
+ const defaultName = getOrganizationNameByDefault ( organizations ) ;
68
+ if ( defaultName ) {
69
+ return < Navigate to = { `/organizations/${ defaultName } /groups` } replace /> ;
70
+ }
71
+ return < EmptyState message = "No default organization found" /> ;
72
+ }
73
+
74
+ const permissions = permissionsQuery . data
75
+ if ( ! permissions ) {
76
+ return < Loader /> ;
48
77
}
49
78
50
79
return (
@@ -56,7 +85,7 @@ export const GroupsPage: FC = () => {
56
85
< PageHeader
57
86
actions = {
58
87
< >
59
- { canCreateGroup && feats . template_rbac && (
88
+ { permissions . createGroup && feats . template_rbac && (
60
89
< Button
61
90
component = { RouterLink }
62
91
startIcon = { < GroupAdd /> }
@@ -73,7 +102,7 @@ export const GroupsPage: FC = () => {
73
102
74
103
< GroupsPageView
75
104
groups = { groupsQuery . data }
76
- canCreateGroup = { canCreateGroup }
105
+ canCreateGroup = { permissions . createGroup }
77
106
isTemplateRBACEnabled = { feats . template_rbac }
78
107
/>
79
108
</ >
0 commit comments