11import React from 'react' ;
22
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' ;
615
716import { getConnectToDBDialog } from '../../components/ConnectToDB/ConnectToDBDialog' ;
817import { 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' ;
1023import { useClusterBaseInfo } from '../../store/reducers/cluster/cluster' ;
24+ import { tenantApi } from '../../store/reducers/tenant/tenant' ;
1125import { uiFactory } from '../../uiFactory/uiFactory' ;
1226import { cn } from '../../utils/cn' ;
1327import { DEVELOPER_UI_TITLE } from '../../utils/constants' ;
1428import { createDeveloperUIInternalPageHref } from '../../utils/developerUI/developerUI' ;
1529import { useTypedSelector } from '../../utils/hooks' ;
16- import { useDatabaseFromQuery } from '../../utils/hooks/useDatabaseFromQuery' ;
30+ import {
31+ useClusterNameFromQuery ,
32+ useDatabaseFromQuery ,
33+ } from '../../utils/hooks/useDatabaseFromQuery' ;
1734import {
1835 useIsUserAllowedToMakeChanges ,
1936 useIsViewerUser ,
2037} from '../../utils/hooks/useIsUserAllowedToMakeChanges' ;
38+ import { getClusterPath } from '../Cluster/utils' ;
2139
2240import { getBreadcrumbs } from './breadcrumbs' ;
2341import { headerKeyset } from './i18n' ;
@@ -35,13 +53,29 @@ function Header() {
3553 const { title : clusterTitle } = useClusterBaseInfo ( ) ;
3654
3755 const database = useDatabaseFromQuery ( ) ;
56+ const clusterName = useClusterNameFromQuery ( ) ;
57+
3858 const location = useLocation ( ) ;
59+ const history = useHistory ( ) ;
60+
3961 const isDatabasePage = location . pathname === '/tenant' ;
4062 const isClustersPage = location . pathname === '/clusters' ;
4163
4264 const isAddClusterAvailable =
4365 useAddClusterFeatureAvailable ( ) && uiFactory . onAddCluster !== undefined ;
4466
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+
4579 const breadcrumbItems = React . useMemo ( ( ) => {
4680 let options = {
4781 ...pageBreadcrumbsOptions ,
@@ -82,6 +116,57 @@ function Header() {
82116 { headerKeyset ( 'connect' ) }
83117 </ Button > ,
84118 ) ;
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+ }
85170 }
86171
87172 if ( ! isClustersPage && isUserAllowedToMakeChanges ) {
0 commit comments