11import React from 'react' ;
22
3+ import { CirclePlus , TrashBin } from '@gravity-ui/icons' ;
34import type { Column } from '@gravity-ui/react-data-table' ;
45import DataTable from '@gravity-ui/react-data-table' ;
5- import { Button } from '@gravity-ui/uikit' ;
6+ import type { DropdownMenuItem } from '@gravity-ui/uikit' ;
7+ import { Button , DropdownMenu , Icon } from '@gravity-ui/uikit' ;
68
79import { EntitiesCount } from '../../components/EntitiesCount' ;
810import { ResponseError } from '../../components/Errors/ResponseError' ;
@@ -13,7 +15,10 @@ import {ResizeableDataTable} from '../../components/ResizeableDataTable/Resizeab
1315import { Search } from '../../components/Search' ;
1416import { TableWithControlsLayout } from '../../components/TableWithControlsLayout/TableWithControlsLayout' ;
1517import { TenantNameWrapper } from '../../components/TenantNameWrapper/TenantNameWrapper' ;
16- import { clusterName } from '../../store' ;
18+ import {
19+ useCreateDatabaseFeatureAvailable ,
20+ useDeleteDatabaseFeatureAvailable ,
21+ } from '../../store/reducers/capabilities/hooks' ;
1722import {
1823 ProblemFilterValues ,
1924 changeFilter ,
@@ -28,6 +33,7 @@ import {
2833import { setSearchValue , tenantsApi } from '../../store/reducers/tenants/tenants' ;
2934import type { PreparedTenant } from '../../store/reducers/tenants/types' ;
3035import type { AdditionalTenantsProps } from '../../types/additionalProps' ;
36+ import { uiFactory } from '../../uiFactory/uiFactory' ;
3137import { cn } from '../../utils/cn' ;
3238import { DEFAULT_TABLE_SETTINGS } from '../../utils/constants' ;
3339import {
@@ -36,6 +42,9 @@ import {
3642 formatStorageValuesToGb ,
3743} from '../../utils/dataFormatters/dataFormatters' ;
3844import { useAutoRefreshInterval , useTypedDispatch , useTypedSelector } from '../../utils/hooks' ;
45+ import { useClusterNameFromQuery } from '../../utils/hooks/useDatabaseFromQuery' ;
46+
47+ import i18n from './i18n' ;
3948
4049import './Tenants.scss' ;
4150
@@ -50,13 +59,20 @@ interface TenantsProps {
5059export const Tenants = ( { additionalTenantsProps} : TenantsProps ) => {
5160 const dispatch = useTypedDispatch ( ) ;
5261
62+ const clusterName = useClusterNameFromQuery ( ) ;
63+
5364 const [ autoRefreshInterval ] = useAutoRefreshInterval ( ) ;
5465 const { currentData, isFetching, error} = tenantsApi . useGetTenantsInfoQuery (
5566 { clusterName} ,
5667 { pollingInterval : autoRefreshInterval } ,
5768 ) ;
5869 const loading = isFetching && currentData === undefined ;
5970
71+ const isCreateDBAvailable =
72+ useCreateDatabaseFeatureAvailable ( ) && uiFactory . onCreateDB !== undefined ;
73+ const isDeleteDBAvailable =
74+ useDeleteDatabaseFeatureAvailable ( ) && uiFactory . onDeleteDB !== undefined ;
75+
6076 const tenants = useTypedSelector ( ( state ) => selectTenants ( state , clusterName ) ) ;
6177 const searchValue = useTypedSelector ( selectTenantsSearchValue ) ;
6278 const filteredTenants = useTypedSelector ( ( state ) => selectFilteredTenants ( state , clusterName ) ) ;
@@ -70,6 +86,23 @@ export const Tenants = ({additionalTenantsProps}: TenantsProps) => {
7086 dispatch ( setSearchValue ( value ) ) ;
7187 } ;
7288
89+ const renderCreateDBButton = ( ) => {
90+ if ( isCreateDBAvailable && clusterName ) {
91+ return (
92+ < Button
93+ view = "action"
94+ onClick = { ( ) => uiFactory . onCreateDB ?.( { clusterName} ) }
95+ className = { b ( 'create-database' ) }
96+ >
97+ < Icon data = { CirclePlus } />
98+ { i18n ( 'create-database' ) }
99+ </ Button >
100+ ) ;
101+ }
102+
103+ return null ;
104+ } ;
105+
73106 const renderControls = ( ) => {
74107 return (
75108 < React . Fragment >
@@ -86,6 +119,7 @@ export const Tenants = ({additionalTenantsProps}: TenantsProps) => {
86119 label = { 'Databases' }
87120 loading = { loading }
88121 />
122+ { renderCreateDBButton ( ) }
89123 </ React . Fragment >
90124 ) ;
91125 } ;
@@ -202,6 +236,53 @@ export const Tenants = ({additionalTenantsProps}: TenantsProps) => {
202236 } ,
203237 ] ;
204238
239+ if ( isDeleteDBAvailable ) {
240+ columns . push ( {
241+ name : 'actions' ,
242+ header : '' ,
243+ width : 40 ,
244+ resizeable : false ,
245+ align : DataTable . CENTER ,
246+ render : ( { row} ) => {
247+ const databaseId = row . UserAttributes ?. database_id ;
248+ const databaseName = row . Name ;
249+
250+ let menuItems : ( DropdownMenuItem | DropdownMenuItem [ ] ) [ ] = [ ] ;
251+
252+ if ( clusterName && databaseName && databaseId ) {
253+ menuItems = [
254+ {
255+ text : i18n ( 'remove' ) ,
256+ iconStart : < TrashBin /> ,
257+ action : ( ) => {
258+ uiFactory . onDeleteDB ?.( {
259+ clusterName,
260+ databaseId,
261+ databaseName,
262+ } ) ;
263+ } ,
264+ className : b ( 'remove-db' ) ,
265+ } ,
266+ ] ;
267+ }
268+
269+ if ( ! menuItems . length ) {
270+ return null ;
271+ }
272+ return (
273+ < DropdownMenu
274+ defaultSwitcherProps = { {
275+ view : 'flat' ,
276+ size : 's' ,
277+ pin : 'brick-brick' ,
278+ } }
279+ items = { menuItems }
280+ />
281+ ) ;
282+ } ,
283+ } ) ;
284+ }
285+
205286 if ( filteredTenants . length === 0 && problemFilter !== ProblemFilterValues . ALL ) {
206287 return < Illustration name = "thumbsUp" width = "200" /> ;
207288 }
@@ -218,12 +299,16 @@ export const Tenants = ({additionalTenantsProps}: TenantsProps) => {
218299 } ;
219300
220301 return (
221- < TableWithControlsLayout >
222- < TableWithControlsLayout . Controls > { renderControls ( ) } </ TableWithControlsLayout . Controls >
223- { error ? < ResponseError error = { error } /> : null }
224- < TableWithControlsLayout . Table loading = { loading } >
225- { currentData ? renderTable ( ) : null }
226- </ TableWithControlsLayout . Table >
227- </ TableWithControlsLayout >
302+ < div className = { b ( 'table-wrapper' ) } >
303+ < TableWithControlsLayout >
304+ < TableWithControlsLayout . Controls className = { b ( 'controls' ) } >
305+ { renderControls ( ) }
306+ </ TableWithControlsLayout . Controls >
307+ { error ? < ResponseError error = { error } /> : null }
308+ < TableWithControlsLayout . Table loading = { loading } >
309+ { currentData ? renderTable ( ) : null }
310+ </ TableWithControlsLayout . Table >
311+ </ TableWithControlsLayout >
312+ </ div >
228313 ) ;
229314} ;
0 commit comments