@@ -14,7 +14,6 @@ import { HL } from '~/components/HL'
1414import { confirmAction } from '~/stores/confirm-action'
1515import { confirmDelete } from '~/stores/confirm-delete'
1616import { addToast } from '~/stores/toast'
17- import type { MakeActions } from '~/table/columns/action-col'
1817import { pb } from '~/util/path-builder'
1918
2019import { fancifyStates } from './instance/tabs/common'
@@ -31,40 +30,49 @@ type Options = {
3130export const useMakeInstanceActions = (
3231 { project } : { project : string } ,
3332 options : Options = { }
34- ) : MakeActions < Instance > => {
33+ ) => {
3534 const navigate = useNavigate ( )
36-
3735 // if you also pass onSuccess to mutate(), this one is not overridden — this
3836 // one runs first, then the one passed to mutate().
3937 //
4038 // We pull out the mutate functions because they are referentially stable,
4139 // while the whole useMutation result object is not. The async ones are used
4240 // when we need to confirm because the confirm modals want that.
4341 const opts = { onSuccess : options . onSuccess }
44- const { mutate : startInstance } = useApiMutation ( 'instanceStart' , opts )
42+ const { mutateAsync : startInstanceAsync } = useApiMutation ( 'instanceStart' , opts )
4543 const { mutateAsync : stopInstanceAsync } = useApiMutation ( 'instanceStop' , opts )
4644 const { mutate : rebootInstance } = useApiMutation ( 'instanceReboot' , opts )
4745 // delete has its own
4846 const { mutateAsync : deleteInstanceAsync } = useApiMutation ( 'instanceDelete' , {
4947 onSuccess : options . onDelete ,
5048 } )
5149
52- return useCallback (
53- ( instance ) => {
54- const instanceSelector = { project, instance : instance . name }
50+ const makeButtonActions = useCallback (
51+ ( instance : Instance ) => {
5552 const instanceParams = { path : { instance : instance . name } , query : { project } }
5653 return [
5754 {
5855 label : 'Start' ,
5956 onActivate ( ) {
60- startInstance ( instanceParams , {
61- onSuccess : ( ) => addToast ( < > Starting instance < HL > { instance . name } </ HL > </ > ) , // prettier-ignore
62- onError : ( error ) =>
63- addToast ( {
64- variant : 'error' ,
65- title : `Error starting instance '${ instance . name } '` ,
66- content : error . message ,
57+ confirmAction ( {
58+ actionType : 'primary' ,
59+ doAction : ( ) =>
60+ startInstanceAsync ( instanceParams , {
61+ onSuccess : ( ) => addToast ( < > Starting instance < HL > { instance . name } </ HL > </ > ) , // prettier-ignore
62+ onError : ( error ) =>
63+ addToast ( {
64+ variant : 'error' ,
65+ title : `Error starting instance '${ instance . name } '` ,
66+ content : error . message ,
67+ } ) ,
6768 } ) ,
69+ modalTitle : 'Confirm start instance' ,
70+ modalContent : (
71+ < p >
72+ Are you sure you want to start < HL > { instance . name } </ HL > ?
73+ </ p >
74+ ) ,
75+ errorTitle : `Error starting ${ instance . name } ` ,
6876 } )
6977 } ,
7078 disabled : ! instanceCan . start ( instance ) && (
@@ -97,9 +105,20 @@ export const useMakeInstanceActions = (
97105 } )
98106 } ,
99107 disabled : ! instanceCan . stop ( instance ) && (
100- < > Only { fancifyStates ( instanceCan . stop . states ) } instances can be stopped</ >
108+ // don't list all the states, it's overwhelming
109+ < > Only { fancifyStates ( [ 'running' ] ) } instances can be stopped</ >
101110 ) ,
102111 } ,
112+ ]
113+ } ,
114+ [ project , startInstanceAsync , stopInstanceAsync ]
115+ )
116+
117+ const makeMenuActions = useCallback (
118+ ( instance : Instance ) => {
119+ const instanceSelector = { project, instance : instance . name }
120+ const instanceParams = { path : { instance : instance . name } , query : { project } }
121+ return [
103122 {
104123 label : 'Reboot' ,
105124 onActivate ( ) {
@@ -143,13 +162,8 @@ export const useMakeInstanceActions = (
143162 } ,
144163 ]
145164 } ,
146- [
147- project ,
148- navigate ,
149- deleteInstanceAsync ,
150- rebootInstance ,
151- startInstance ,
152- stopInstanceAsync ,
153- ]
165+ [ project , deleteInstanceAsync , navigate , rebootInstance ]
154166 )
167+
168+ return { makeButtonActions, makeMenuActions }
155169}
0 commit comments