@@ -29,29 +29,35 @@ type Options = {
29
29
}
30
30
31
31
export const useMakeInstanceActions = (
32
- projectSelector : { project : string } ,
32
+ { project } : { project : string } ,
33
33
options : Options = { }
34
34
) : MakeActions < Instance > => {
35
35
const navigate = useNavigate ( )
36
36
37
37
// if you also pass onSuccess to mutate(), this one is not overridden — this
38
- // one runs first, then the one passed to mutate()
38
+ // one runs first, then the one passed to mutate().
39
+ //
40
+ // We pull out the mutate functions because they are referentially stable,
41
+ // while the whole useMutation result object is not. The async ones are used
42
+ // when we need to confirm because the confirm modals want that.
39
43
const opts = { onSuccess : options . onSuccess }
40
- const startInstance = useApiMutation ( 'instanceStart' , opts )
41
- const stopInstance = useApiMutation ( 'instanceStop' , opts )
42
- const rebootInstance = useApiMutation ( 'instanceReboot' , opts )
44
+ const { mutate : startInstance } = useApiMutation ( 'instanceStart' , opts )
45
+ const { mutateAsync : stopInstanceAsync } = useApiMutation ( 'instanceStop' , opts )
46
+ const { mutate : rebootInstance } = useApiMutation ( 'instanceReboot' , opts )
43
47
// delete has its own
44
- const deleteInstance = useApiMutation ( 'instanceDelete' , { onSuccess : options . onDelete } )
48
+ const { mutateAsync : deleteInstanceAsync } = useApiMutation ( 'instanceDelete' , {
49
+ onSuccess : options . onDelete ,
50
+ } )
45
51
46
52
return useCallback (
47
53
( instance ) => {
48
- const instanceSelector = { ... projectSelector , instance : instance . name }
49
- const instanceParams = { path : { instance : instance . name } , query : projectSelector }
54
+ const instanceSelector = { project , instance : instance . name }
55
+ const instanceParams = { path : { instance : instance . name } , query : { project } }
50
56
return [
51
57
{
52
58
label : 'Start' ,
53
59
onActivate ( ) {
54
- startInstance . mutate ( instanceParams , {
60
+ startInstance ( instanceParams , {
55
61
onSuccess : ( ) => addToast ( { title : `Starting instance '${ instance . name } '` } ) ,
56
62
onError : ( error ) =>
57
63
addToast ( {
@@ -71,7 +77,7 @@ export const useMakeInstanceActions = (
71
77
confirmAction ( {
72
78
actionType : 'danger' ,
73
79
doAction : ( ) =>
74
- stopInstance . mutateAsync ( instanceParams , {
80
+ stopInstanceAsync ( instanceParams , {
75
81
onSuccess : ( ) =>
76
82
addToast ( { title : `Stopping instance '${ instance . name } '` } ) ,
77
83
} ) ,
@@ -93,7 +99,7 @@ export const useMakeInstanceActions = (
93
99
{
94
100
label : 'Reboot' ,
95
101
onActivate ( ) {
96
- rebootInstance . mutate ( instanceParams , {
102
+ rebootInstance ( instanceParams , {
97
103
onSuccess : ( ) => addToast ( { title : `Rebooting instance '${ instance . name } '` } ) ,
98
104
onError : ( error ) =>
99
105
addToast ( {
@@ -117,7 +123,7 @@ export const useMakeInstanceActions = (
117
123
label : 'Delete' ,
118
124
onActivate : confirmDelete ( {
119
125
doDelete : ( ) =>
120
- deleteInstance . mutateAsync ( instanceParams , {
126
+ deleteInstanceAsync ( instanceParams , {
121
127
onSuccess : ( ) =>
122
128
addToast ( { title : `Deleting instance '${ instance . name } '` } ) ,
123
129
} ) ,
@@ -132,6 +138,13 @@ export const useMakeInstanceActions = (
132
138
} ,
133
139
]
134
140
} ,
135
- [ projectSelector , deleteInstance , navigate , rebootInstance , startInstance , stopInstance ]
141
+ [
142
+ project ,
143
+ navigate ,
144
+ deleteInstanceAsync ,
145
+ rebootInstance ,
146
+ startInstance ,
147
+ stopInstanceAsync ,
148
+ ]
136
149
)
137
150
}
0 commit comments