|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { useEffect, useState, type ReactNode } from 'react' |
| 9 | + |
| 10 | +import { |
| 11 | + instanceTransitioning, |
| 12 | + useApiMutation, |
| 13 | + useApiQuery, |
| 14 | + useApiQueryClient, |
| 15 | + type Instance, |
| 16 | +} from '@oxide/api' |
| 17 | + |
| 18 | +import { HL } from '~/components/HL' |
| 19 | +import { addToast } from '~/stores/toast' |
| 20 | +import { Button } from '~/ui/lib/Button' |
| 21 | +import { Message } from '~/ui/lib/Message' |
| 22 | + |
| 23 | +const POLL_INTERVAL_FAST = 2000 // 2 seconds |
| 24 | + |
| 25 | +type StopInstancePromptProps = { |
| 26 | + instance: Instance |
| 27 | + children: ReactNode |
| 28 | +} |
| 29 | + |
| 30 | +export function StopInstancePrompt({ instance, children }: StopInstancePromptProps) { |
| 31 | + const queryClient = useApiQueryClient() |
| 32 | + const [isStoppingInstance, setIsStoppingInstance] = useState(false) |
| 33 | + |
| 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', { |
| 47 | + onSuccess: () => { |
| 48 | + setIsStoppingInstance(true) |
| 49 | + addToast( |
| 50 | + <> |
| 51 | + Stopping instance <HL>{instance.name}</HL> |
| 52 | + </> |
| 53 | + ) |
| 54 | + }, |
| 55 | + onError: (error) => { |
| 56 | + addToast({ |
| 57 | + variant: 'error', |
| 58 | + title: `Error stopping instance '${instance.name}'`, |
| 59 | + content: error.message, |
| 60 | + }) |
| 61 | + setIsStoppingInstance(false) |
| 62 | + }, |
| 63 | + }) |
| 64 | + |
| 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 | + |
| 91 | + return ( |
| 92 | + <Message |
| 93 | + variant="notice" |
| 94 | + content={ |
| 95 | + <> |
| 96 | + {children}{' '} |
| 97 | + <Button |
| 98 | + size="xs" |
| 99 | + className="mt-3" |
| 100 | + variant="notice" |
| 101 | + onClick={handleStopInstance} |
| 102 | + loading={isStoppingInstance} |
| 103 | + > |
| 104 | + Stop instance |
| 105 | + </Button> |
| 106 | + </> |
| 107 | + } |
| 108 | + /> |
| 109 | + ) |
| 110 | +} |
0 commit comments