Skip to content

Commit fc4d11b

Browse files
committed
simplify some of the instance state business
1 parent 7581cf4 commit fc4d11b

File tree

3 files changed

+25
-58
lines changed

3 files changed

+25
-58
lines changed

app/components/StopInstancePrompt.tsx

+15-53
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,49 @@
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, usePrefetchedApiQuery } from '@oxide/api'
1711

1812
import { HL } from '~/components/HL'
13+
import { useInstanceSelector } from '~/hooks/use-params'
1914
import { addToast } from '~/stores/toast'
2015
import { Button } from '~/ui/lib/Button'
2116
import { Message } from '~/ui/lib/Message'
2217

23-
const POLL_INTERVAL_FAST = 2000 // 2 seconds
24-
2518
type StopInstancePromptProps = {
26-
instance: Instance
2719
children: ReactNode
2820
}
2921

30-
export function StopInstancePrompt({ instance, children }: StopInstancePromptProps) {
31-
const queryClient = useApiQueryClient()
32-
const [isStoppingInstance, setIsStoppingInstance] = useState(false)
22+
export function StopInstancePrompt({ children }: StopInstancePromptProps) {
23+
const instanceSelector = useInstanceSelector()
24+
const { data: instance } = usePrefetchedApiQuery('instanceView', {
25+
path: { instance: instanceSelector.instance },
26+
query: { project: instanceSelector.project },
27+
})
3328

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-
)
29+
const isStoppingInstance = instance.runState === 'stopping'
4530

4631
const { mutateAsync: stopInstanceAsync } = useApiMutation('instanceStop', {
4732
onSuccess: () => {
48-
setIsStoppingInstance(true)
49-
addToast(
50-
<>
51-
Stopping instance <HL>{instance.name}</HL>
52-
</>
53-
)
33+
addToast(<>Stopping instance <HL>{instance.name}</HL></>) // prettier-ignore
5434
},
5535
onError: (error) => {
5636
addToast({
5737
variant: 'error',
5838
title: `Error stopping instance '${instance.name}'`,
5939
content: error.message,
6040
})
61-
setIsStoppingInstance(false)
6241
},
6342
})
6443

65-
const handleStopInstance = () => {
66-
stopInstanceAsync({
44+
const handleStopInstance = async () => {
45+
await stopInstanceAsync({
6746
path: { instance: instance.name },
6847
query: { project: instance.projectId },
6948
})
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
49+
// doing this after means it triggers polling by the top level InstancePage one
50+
apiQueryClient.invalidateQueries('instanceView')
8951
}
9052

9153
return (

app/forms/disk-attach.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type AttachDiskProps = {
2626
diskNamesToExclude?: string[]
2727
loading?: boolean
2828
submitError?: ApiError | null
29-
instance: Instance
29+
instance?: Instance
3030
}
3131

3232
/**
@@ -70,12 +70,16 @@ export function AttachDiskModalForm({
7070
onSubmit={onSubmit}
7171
width="medium"
7272
submitDisabled={
73-
!instanceCan.attachDisk(instance) ? 'Instance must be stopped' : undefined
73+
instance && !instanceCan.attachDisk(instance)
74+
? 'Instance must be stopped'
75+
: undefined
7476
}
7577
>
76-
<StopInstancePrompt instance={instance}>
77-
An instance must be stopped to attach a disk.
78-
</StopInstancePrompt>
78+
{instance && ['stopping', 'running'].includes(instance.runState) && (
79+
<StopInstancePrompt>
80+
An instance must be stopped to attach a disk.
81+
</StopInstancePrompt>
82+
)}
7983
<ComboboxField
8084
label="Disk name"
8185
placeholder="Select a disk"

app/pages/project/instances/StorageTab.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ export default function StorageTab() {
382382
}}
383383
loading={attachDisk.isPending}
384384
submitError={attachDisk.error}
385+
instance={instance}
385386
/>
386387
)}
387388
</div>

0 commit comments

Comments
 (0)