55 *
66 * Copyright Oxide Computer Company
77 */
8- import { useEffect , useState , type ReactNode } from 'react'
8+ import { type ReactNode } from 'react'
99
10- import {
11- instanceTransitioning ,
12- useApiMutation ,
13- useApiQuery ,
14- useApiQueryClient ,
15- type Instance ,
16- } from '@oxide/api'
10+ import { apiQueryClient , useApiMutation , type Instance } from '@oxide/api'
1711
1812import { HL } from '~/components/HL'
1913import { addToast } from '~/stores/toast'
2014import { Button } from '~/ui/lib/Button'
2115import { Message } from '~/ui/lib/Message'
2216
23- const POLL_INTERVAL_FAST = 2000 // 2 seconds
24-
2517type StopInstancePromptProps = {
2618 instance : Instance
2719 children : ReactNode
2820}
2921
3022export function StopInstancePrompt ( { instance, children } : StopInstancePromptProps ) {
31- const queryClient = useApiQueryClient ( )
32- const [ isStoppingInstance , setIsStoppingInstance ] = useState ( false )
23+ const isStoppingInstance = instance . runState === 'stopping'
3324
34- const { data } = useApiQuery (
35- 'instanceView' ,
36- {
37- path : { instance : instance . name } ,
38- query : { project : instance . projectId } ,
39- } ,
40- {
41- refetchInterval :
42- isStoppingInstance || instanceTransitioning ( instance ) ? POLL_INTERVAL_FAST : false ,
43- }
44- )
45-
46- const { mutateAsync : stopInstanceAsync } = useApiMutation ( 'instanceStop' , {
25+ const stopInstance = useApiMutation ( 'instanceStop' , {
4726 onSuccess : ( ) => {
48- setIsStoppingInstance ( true )
49- addToast (
50- < >
51- Stopping instance < HL > { instance . name } </ HL >
52- </ >
53- )
27+ // trigger polling by the top level InstancePage one
28+ apiQueryClient . invalidateQueries ( 'instanceView' )
29+ addToast ( < > Stopping instance < HL > { instance . name } </ HL > </ > ) // prettier-ignore
5430 } ,
5531 onError : ( error ) => {
5632 addToast ( {
5733 variant : 'error' ,
5834 title : `Error stopping instance '${ instance . name } '` ,
5935 content : error . message ,
6036 } )
61- setIsStoppingInstance ( false )
6237 } ,
6338 } )
6439
65- const handleStopInstance = ( ) => {
66- stopInstanceAsync ( {
67- path : { instance : instance . name } ,
68- query : { project : instance . projectId } ,
69- } )
70- }
71-
72- const currentInstance = data || instance
73-
74- useEffect ( ( ) => {
75- if ( ! data ) {
76- return
77- }
78- if ( isStoppingInstance && data . runState === 'stopped' ) {
79- queryClient . invalidateQueries ( 'instanceView' )
80- setIsStoppingInstance ( false )
81- }
82- } , [ isStoppingInstance , data , queryClient ] )
83-
84- if (
85- ! currentInstance ||
86- ( currentInstance . runState !== 'stopping' && currentInstance . runState !== 'running' )
87- ) {
88- return null
89- }
90-
9140 return (
9241 < Message
9342 variant = "notice"
@@ -98,7 +47,12 @@ export function StopInstancePrompt({ instance, children }: StopInstancePromptPro
9847 size = "xs"
9948 className = "mt-3"
10049 variant = "notice"
101- onClick = { handleStopInstance }
50+ onClick = { ( ) =>
51+ stopInstance . mutateAsync ( {
52+ path : { instance : instance . name } ,
53+ query : { project : instance . projectId } ,
54+ } )
55+ }
10256 loading = { isStoppingInstance }
10357 >
10458 Stop instance
0 commit comments