1
1
import React from 'react' ;
2
2
3
- import { ArrowUpRightFromSquare , CirclePlus , PlugConnection } from '@gravity-ui/icons' ;
4
- import { Breadcrumbs , Button , Divider , Flex , Icon } from '@gravity-ui/uikit' ;
5
- import { useLocation } from 'react-router-dom' ;
3
+ import {
4
+ ArrowUpRightFromSquare ,
5
+ ChevronDown ,
6
+ CirclePlus ,
7
+ Pencil ,
8
+ PlugConnection ,
9
+ TrashBin ,
10
+ } from '@gravity-ui/icons' ;
11
+ import type { DropdownMenuItem , DropdownMenuProps } from '@gravity-ui/uikit' ;
12
+ import { Breadcrumbs , Button , Divider , DropdownMenu , Flex , Icon } from '@gravity-ui/uikit' ;
13
+ import { skipToken } from '@reduxjs/toolkit/query' ;
14
+ import { useHistory , useLocation } from 'react-router-dom' ;
6
15
7
16
import { getConnectToDBDialog } from '../../components/ConnectToDB/ConnectToDBDialog' ;
8
17
import { InternalLink } from '../../components/InternalLink' ;
9
- import { useAddClusterFeatureAvailable } from '../../store/reducers/capabilities/hooks' ;
18
+ import {
19
+ useAddClusterFeatureAvailable ,
20
+ useDeleteDatabaseFeatureAvailable ,
21
+ useEditDatabaseFeatureAvailable ,
22
+ } from '../../store/reducers/capabilities/hooks' ;
10
23
import { useClusterBaseInfo } from '../../store/reducers/cluster/cluster' ;
24
+ import { tenantApi } from '../../store/reducers/tenant/tenant' ;
11
25
import { uiFactory } from '../../uiFactory/uiFactory' ;
12
26
import { cn } from '../../utils/cn' ;
13
27
import { DEVELOPER_UI_TITLE } from '../../utils/constants' ;
14
28
import { createDeveloperUIInternalPageHref } from '../../utils/developerUI/developerUI' ;
15
29
import { useTypedSelector } from '../../utils/hooks' ;
16
- import { useDatabaseFromQuery } from '../../utils/hooks/useDatabaseFromQuery' ;
30
+ import {
31
+ useClusterNameFromQuery ,
32
+ useDatabaseFromQuery ,
33
+ } from '../../utils/hooks/useDatabaseFromQuery' ;
17
34
import {
18
35
useIsUserAllowedToMakeChanges ,
19
36
useIsViewerUser ,
20
37
} from '../../utils/hooks/useIsUserAllowedToMakeChanges' ;
38
+ import { getClusterPath } from '../Cluster/utils' ;
21
39
22
40
import { getBreadcrumbs } from './breadcrumbs' ;
23
41
import { headerKeyset } from './i18n' ;
@@ -35,13 +53,29 @@ function Header() {
35
53
const { title : clusterTitle } = useClusterBaseInfo ( ) ;
36
54
37
55
const database = useDatabaseFromQuery ( ) ;
56
+ const clusterName = useClusterNameFromQuery ( ) ;
57
+
38
58
const location = useLocation ( ) ;
59
+ const history = useHistory ( ) ;
60
+
39
61
const isDatabasePage = location . pathname === '/tenant' ;
40
62
const isClustersPage = location . pathname === '/clusters' ;
41
63
42
64
const isAddClusterAvailable =
43
65
useAddClusterFeatureAvailable ( ) && uiFactory . onAddCluster !== undefined ;
44
66
67
+ const isEditDBAvailable = useEditDatabaseFeatureAvailable ( ) && uiFactory . onEditDB !== undefined ;
68
+ const isDeleteDBAvailable =
69
+ useDeleteDatabaseFeatureAvailable ( ) && uiFactory . onDeleteDB !== undefined ;
70
+
71
+ const shouldRequestTenantData =
72
+ database && isDatabasePage && ( isEditDBAvailable || isDeleteDBAvailable ) ;
73
+
74
+ const params = shouldRequestTenantData ? { path : database , clusterName} : skipToken ;
75
+
76
+ const { currentData : databaseData , isLoading : isDatabaseDataLoading } =
77
+ tenantApi . useGetTenantInfoQuery ( params ) ;
78
+
45
79
const breadcrumbItems = React . useMemo ( ( ) => {
46
80
let options = {
47
81
...pageBreadcrumbsOptions ,
@@ -82,6 +116,57 @@ function Header() {
82
116
{ headerKeyset ( 'connect' ) }
83
117
</ Button > ,
84
118
) ;
119
+
120
+ const menuItems : DropdownMenuItem [ ] = [ ] ;
121
+
122
+ const { onEditDB, onDeleteDB} = uiFactory ;
123
+
124
+ const isEnoughData = clusterName && databaseData ;
125
+
126
+ if ( isEditDBAvailable && onEditDB && isEnoughData ) {
127
+ menuItems . push ( {
128
+ text : headerKeyset ( 'action_edit-db' ) ,
129
+ iconStart : < Pencil /> ,
130
+ action : ( ) => {
131
+ onEditDB ( { clusterName, databaseData} ) ;
132
+ } ,
133
+ } ) ;
134
+ }
135
+ if ( isDeleteDBAvailable && onDeleteDB && isEnoughData ) {
136
+ menuItems . push ( {
137
+ text : headerKeyset ( 'action_delete-db' ) ,
138
+ iconStart : < TrashBin /> ,
139
+ action : ( ) => {
140
+ onDeleteDB ( { clusterName, databaseData} ) . then ( ( isDeleted ) => {
141
+ if ( isDeleted ) {
142
+ const path = getClusterPath ( 'tenants' ) ;
143
+ history . push ( path ) ;
144
+ }
145
+ } ) ;
146
+ } ,
147
+ theme : 'danger' ,
148
+ } ) ;
149
+ }
150
+
151
+ if ( menuItems . length ) {
152
+ const renderSwitcher : DropdownMenuProps < unknown > [ 'renderSwitcher' ] = ( props ) => {
153
+ return (
154
+ < Button { ...props } loading = { isDatabaseDataLoading } view = "flat" size = "m" >
155
+ { headerKeyset ( 'action_manage' ) }
156
+ < Icon data = { ChevronDown } />
157
+ </ Button >
158
+ ) ;
159
+ } ;
160
+
161
+ elements . push (
162
+ < DropdownMenu
163
+ items = { menuItems }
164
+ renderSwitcher = { renderSwitcher }
165
+ menuProps = { { size : 'l' } }
166
+ popupProps = { { placement : 'bottom-end' } }
167
+ /> ,
168
+ ) ;
169
+ }
85
170
}
86
171
87
172
if ( ! isClustersPage && isUserAllowedToMakeChanges ) {
0 commit comments