-
Notifications
You must be signed in to change notification settings - Fork 12
Prototype: Stop instance disk modal #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
benjaminleonard
wants to merge
19
commits into
main
Choose a base branch
from
stop-instance-disk-modal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b4ce08d
Attach disk modal instead of side modal
benjaminleonard 61438c6
Experimental stop instance in modal
benjaminleonard 036b295
Tweak disabled text button colour
benjaminleonard 5bdb549
Type fix
benjaminleonard b1dc605
Cleanup
benjaminleonard a0e2b0c
Also needed on `ModalForm`
benjaminleonard 5824157
Already defaults to medium
benjaminleonard 13ac403
convert image promote modal
david-crespo f591a2b
clean up props a bit
david-crespo 1cecbcc
convert image demote too
david-crespo 1b568f9
demotion notice copy tweak
david-crespo aa7ecdb
convert floating IP attach
david-crespo f17a1ac
remove unnecessary fragment
david-crespo 7581cf4
merge modalform thing into this one
david-crespo 2f72238
simplify some of the instance state business
david-crespo e918849
Merge branch 'attach-disk-modal' of https://github.com/oxidecomputer/…
benjaminleonard 39ac93a
Attach floating IP modal and prop tweaks
benjaminleonard dbdf80b
Merge branch 'attach-disk-modal' into stop-instance-disk-modal
benjaminleonard 5ba0578
Update test
benjaminleonard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* Copyright Oxide Computer Company | ||
*/ | ||
import { type ReactNode } from 'react' | ||
|
||
import { apiQueryClient, useApiMutation, type Instance } from '@oxide/api' | ||
|
||
import { HL } from '~/components/HL' | ||
import { addToast } from '~/stores/toast' | ||
import { Button } from '~/ui/lib/Button' | ||
import { Message } from '~/ui/lib/Message' | ||
|
||
type StopInstancePromptProps = { | ||
instance: Instance | ||
children: ReactNode | ||
} | ||
|
||
export function StopInstancePrompt({ instance, children }: StopInstancePromptProps) { | ||
const isStoppingInstance = instance.runState === 'stopping' | ||
|
||
const stopInstance = useApiMutation('instanceStop', { | ||
onSuccess: () => { | ||
// trigger polling by the top level InstancePage one | ||
apiQueryClient.invalidateQueries('instanceView') | ||
addToast(<>Stopping instance <HL>{instance.name}</HL></>) // prettier-ignore | ||
}, | ||
onError: (error) => { | ||
addToast({ | ||
variant: 'error', | ||
title: `Error stopping instance '${instance.name}'`, | ||
content: error.message, | ||
}) | ||
}, | ||
}) | ||
|
||
return ( | ||
<Message | ||
variant="notice" | ||
content={ | ||
<> | ||
{children}{' '} | ||
<Button | ||
size="xs" | ||
className="mt-3" | ||
variant="notice" | ||
onClick={() => | ||
stopInstance.mutateAsync({ | ||
path: { instance: instance.name }, | ||
query: { project: instance.projectId }, | ||
}) | ||
} | ||
loading={isStoppingInstance} | ||
> | ||
Stop instance | ||
</Button> | ||
</> | ||
} | ||
/> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* Copyright Oxide Computer Company | ||
*/ | ||
|
||
import { useId, type ReactNode } from 'react' | ||
import type { FieldValues, UseFormReturn } from 'react-hook-form' | ||
|
||
import type { ApiError } from '@oxide/api' | ||
|
||
import { Message } from '~/ui/lib/Message' | ||
import { Modal, type ModalProps } from '~/ui/lib/Modal' | ||
|
||
type ModalFormProps<TFieldValues extends FieldValues> = { | ||
form: UseFormReturn<TFieldValues> | ||
children: ReactNode | ||
|
||
/** Must be provided with a reason describing why it's disabled */ | ||
submitDisabled?: boolean | ||
onSubmit: (values: TFieldValues) => void | ||
submitLabel: string | ||
|
||
// require loading and error so we can't forget to hook them up. there are a | ||
// few forms that don't need them, so we'll use dummy values | ||
|
||
/** Error from the API call */ | ||
submitError: ApiError | null | ||
loading: boolean | ||
} & Omit<ModalProps, 'isOpen'> | ||
|
||
export function ModalForm<TFieldValues extends FieldValues>({ | ||
form, | ||
children, | ||
onDismiss, | ||
submitDisabled = false, | ||
submitError, | ||
title, | ||
onSubmit, | ||
submitLabel = 'Save', | ||
loading, | ||
width = 'medium', | ||
overlay = true, | ||
}: ModalFormProps<TFieldValues>) { | ||
const id = useId() | ||
const { isSubmitting } = form.formState | ||
|
||
return ( | ||
<Modal isOpen onDismiss={onDismiss} title={title} width={width} overlay={overlay}> | ||
<Modal.Body> | ||
<Modal.Section> | ||
{submitError && ( | ||
<Message variant="error" title="Error" content={submitError.message} /> | ||
)} | ||
<form | ||
id={id} | ||
className="ox-form" | ||
autoComplete="off" | ||
onSubmit={(e) => { | ||
if (!onSubmit) return | ||
// This modal being in a portal doesn't prevent the submit event | ||
// from bubbling up out of the portal. Normally that's not a | ||
// problem, but sometimes (e.g., instance create) we render the | ||
// SideModalForm from inside another form, in which case submitting | ||
// the inner form submits the outer form unless we stop propagation | ||
e.stopPropagation() | ||
form.handleSubmit(onSubmit)(e) | ||
}} | ||
> | ||
{children} | ||
</form> | ||
</Modal.Section> | ||
</Modal.Body> | ||
<Modal.Footer | ||
onDismiss={onDismiss} | ||
formId={id} | ||
actionText={submitLabel} | ||
disabled={submitDisabled} | ||
actionLoading={loading || isSubmitting} | ||
/> | ||
</Modal> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is very clever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only downside is that you have a delay in the loading state because it doesn't change to loading while the initial stop request goes through. I tried this
and it does respond immediately, but it flashes back to off briefly in between the stop request completing and the invalidation running and flipping the instance to 'stopping'. I can't think of a fix other than adding back in the explicit state thing you had. Those things make me nervous because the logic has to be bulletproof to avoid accidentally getting stuck in a loading state, but I'm sure we can do it.