1
1
/* Copyright Contributors to the Open Cluster Management project */
2
2
// Copyright (c) 2021 Red Hat, Inc.
3
3
4
- import { Dropdown , DropdownItem , DropdownToggle } from '@patternfly/react-core'
5
- import { Dispatch , SetStateAction , useEffect , useMemo , useState } from 'react'
4
+ import { Divider , Dropdown , DropdownItem , DropdownToggle } from '@patternfly/react-core'
5
+ import { Dispatch , SetStateAction , useContext , useEffect , useMemo , useState } from 'react'
6
6
import { Link , Outlet , useLocation , useNavigate , useOutletContext } from 'react-router-dom-v5-compat'
7
7
import { Pages , usePageVisitMetricHandler } from '../../../hooks/console-metrics'
8
8
import { useTranslation } from '../../../lib/acm-i18next'
9
9
import { NavigationPath } from '../../../NavigationPath'
10
10
import { IResource } from '../../../resources'
11
11
import { fireManagedClusterView } from '../../../resources/managedclusterview'
12
12
import { getResource } from '../../../resources/utils/resource-request'
13
- import { AcmPage , AcmPageHeader , AcmSecondaryNav , AcmSecondaryNavItem } from '../../../ui-components'
13
+ import { useRecoilValue , useSharedAtoms } from '../../../shared-recoil'
14
+ import { AcmPage , AcmPageHeader , AcmSecondaryNav , AcmSecondaryNavItem , AcmToastContext } from '../../../ui-components'
15
+ import { handleVMActions } from '../SearchResults/utils'
14
16
import { DeleteResourceModal } from './DeleteResourceModal'
15
17
16
18
export type SearchDetailsContext = {
@@ -27,40 +29,23 @@ export type SearchDetailsContext = {
27
29
}
28
30
29
31
export function getResourceParams ( ) {
30
- let cluster = '' ,
31
- kind = '' ,
32
- apiversion = '' ,
33
- namespace = '' ,
34
- name = ''
35
- const urlParams = decodeURIComponent ( window . location . search ) . replace ( '?' , '' ) . split ( '&' )
36
- urlParams . forEach ( ( param ) => {
37
- const paramKey = param . split ( '=' ) [ 0 ]
38
- const paramValue = param . split ( '=' ) [ 1 ]
39
- switch ( paramKey ) {
40
- case 'cluster' :
41
- cluster = paramValue
42
- break
43
- case 'kind' :
44
- kind = paramValue
45
- break
46
- case 'apiversion' :
47
- apiversion = paramValue
48
- break
49
- case 'namespace' :
50
- namespace = paramValue
51
- break
52
- case 'name' :
53
- name = paramValue
54
- break
55
- }
56
- } )
57
- return { cluster, kind, apiversion, namespace, name }
32
+ const params = new URLSearchParams ( decodeURIComponent ( window . location . search ) )
33
+ return {
34
+ cluster : params . get ( 'cluster' ) || '' ,
35
+ kind : params . get ( 'kind' ) || '' ,
36
+ apiversion : params . get ( 'apiversion' ) || '' ,
37
+ namespace : params . get ( 'namespace' ) || '' ,
38
+ name : params . get ( 'name' ) || '' ,
39
+ }
58
40
}
59
41
60
42
export default function DetailsPage ( ) {
61
43
usePageVisitMetricHandler ( Pages . searchDetails )
62
44
const { t } = useTranslation ( )
63
45
const navigate = useNavigate ( )
46
+ const toast = useContext ( AcmToastContext )
47
+ const { settingsState } = useSharedAtoms ( )
48
+ const vmActionsEnabled = useRecoilValue ( settingsState ) ?. VIRTUAL_MACHINE_ACTIONS === 'enabled'
64
49
const [ resource , setResource ] = useState < any > ( undefined )
65
50
const [ containers , setContainers ] = useState < string [ ] > ( )
66
51
const [ resourceVersion , setResourceVersion ] = useState < string > ( '' )
@@ -155,6 +140,59 @@ export default function DetailsPage() {
155
140
[ apiversion , cluster , containers , kind , name , namespace , resource , resourceError ]
156
141
)
157
142
143
+ const getResourceActions = useMemo ( ( ) => {
144
+ const actions = [
145
+ < DropdownItem
146
+ component = "button"
147
+ key = "edit-resource"
148
+ onClick = { ( ) => {
149
+ navigate ( `${ NavigationPath . resourceYAML } ${ window . location . search } ` )
150
+ } }
151
+ >
152
+ { t ( 'Edit {{resourceKind}}' , { resourceKind : kind } ) }
153
+ </ DropdownItem > ,
154
+ < DropdownItem
155
+ component = "button"
156
+ key = "delete-resource"
157
+ onClick = { ( ) => {
158
+ setIsDeleteResourceModalOpen ( true )
159
+ } }
160
+ >
161
+ { t ( 'Delete {{resourceKind}}' , { resourceKind : kind } ) }
162
+ </ DropdownItem > ,
163
+ ]
164
+ if ( vmActionsEnabled && kind . toLowerCase ( ) === 'virtualmachine' ) {
165
+ actions . unshift (
166
+ ...[
167
+ { action : 'Start' , path : '/virtualmachines/start' } ,
168
+ { action : 'Stop' , path : '/virtualmachines/stop' } ,
169
+ { action : 'Restart' , path : '/virtualmachines/restart' } ,
170
+ { action : 'Pause' , path : '/virtualmachineinstances/pause' } ,
171
+ { action : 'Unpause' , path : '/virtualmachineinstances/unpause' } ,
172
+ ] . map ( ( action ) => (
173
+ < DropdownItem
174
+ key = { `${ action . action } -vm-resource` }
175
+ component = "button"
176
+ onClick = { ( ) =>
177
+ handleVMActions (
178
+ action . action . toLowerCase ( ) ,
179
+ action . path ,
180
+ { cluster, name, namespace } ,
181
+ ( ) => setResourceVersion ( '' ) , // trigger resource refetchto update details page data.
182
+ toast ,
183
+ t
184
+ )
185
+ }
186
+ >
187
+ { t ( `{{action}} {{resourceKind}}` , { action : action . action , resourceKind : kind } ) }
188
+ </ DropdownItem >
189
+ ) ) ,
190
+ < Divider key = { 'action-divider' } />
191
+ )
192
+ }
193
+ return actions
194
+ } , [ cluster , kind , name , namespace , vmActionsEnabled , navigate , toast , t ] )
195
+
158
196
return (
159
197
< AcmPage
160
198
header = {
@@ -197,26 +235,7 @@ export default function DetailsPage() {
197
235
{ t ( 'Actions' ) }
198
236
</ DropdownToggle >
199
237
}
200
- dropdownItems = { [
201
- < DropdownItem
202
- component = "button"
203
- key = "edit-resource"
204
- onClick = { ( ) => {
205
- navigate ( `${ NavigationPath . resourceYAML } ${ window . location . search } ` )
206
- } }
207
- >
208
- { t ( 'Edit {{resourceKind}}' , { resourceKind : kind } ) }
209
- </ DropdownItem > ,
210
- < DropdownItem
211
- component = "button"
212
- key = "delete-resource"
213
- onClick = { ( ) => {
214
- setIsDeleteResourceModalOpen ( true )
215
- } }
216
- >
217
- { t ( 'Delete {{resourceKind}}' , { resourceKind : kind } ) }
218
- </ DropdownItem > ,
219
- ] }
238
+ dropdownItems = { getResourceActions }
220
239
onSelect = { ( ) => setResourceActionsOpen ( false ) }
221
240
/>
222
241
}
0 commit comments