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