Skip to content
Open
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
# Mutations & UI flow

- Wrap writes in `useApiMutation` and surface results with `addToast` (`app/stores/toast.ts`). Guard destructive flows with the zustand confirm helpers `confirmDelete`/`confirmAction` (`app/stores/confirm-delete.tsx`, `app/stores/confirm-action.ts`), passing a `mutateAsync` lambda so the modal can catch failures and toast them.
- In toasts and confirm modal content, wrap resource names in `<HL>` (e.g., `<p>Are you sure you want to detach <HL>{disk.name}</HL>?</p>`).
- When a form's `onSuccess` always navigates away, pass `loading={mutation.isPending || mutation.isSuccess}` to the form shell. `isPending` alone flips false before the navigation unmounts the modal, so the button's spinner animates back out right before close. Skip `isSuccess` if the form can stay open and be reused after success, or if the mutation lives in a component that survives the modal (e.g., a tab page with `{open && <Modal/>}`) — there success closes the modal synchronously so `isPending` alone is glitch-free, and a sticky `isSuccess` would strand a spinner on next open.
- Mutation error display depends on context. In forms, errors display inline via `submitError={mutation.error}` — do not add `onError` with a toast to the `useApiMutation` call. In `confirmAction`/`confirmDelete` flows, the confirm modal catches the error and shows a toast using `errorTitle` — do not also add `onError` on the mutation, or the user will see two toasts. For standalone actions (fire-and-forget `mutate` calls not wrapped in a confirm modal or form), use `onError` on the mutation to show an error toast.
- Keep page scaffolding consistent: `PageHeader`, `PageTitle`, `DocsPopover`, `RefreshButton`, `PropertiesTable`, and `CardBlock` provide the expected layout for new system pages.
Expand Down
4 changes: 4 additions & 0 deletions app/api/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ const diskStateActions = {
detach: ['attached'],
// https://github.com/oxidecomputer/omicron/blob/3093818/nexus/db-queries/src/db/datastore/instance.rs#L1077-L1081
setAsBootDisk: ['attached'],
// https://github.com/oxidecomputer/omicron/blob/9971869/nexus/src/app/sagas/finalize_disk.rs#L155-L186
finalizeImport: ['import_ready'],
// https://github.com/oxidecomputer/omicron/blob/9971869/nexus/src/app/disk.rs#L684-L697
cancelBulkWrites: ['importing_from_bulk_writes'],
} satisfies Record<string, DiskState['state'][]>

// snapshot has a type check in addition to state check
Expand Down
1 change: 1 addition & 0 deletions app/forms/image-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function getTmpDiskName(imageName: string) {
'import-start-500',
'import-stop-500',
'disk-finalize-500',
'cancel-upload',
])
if (specialNames.has(imageName)) return imageName
}
Expand Down
130 changes: 97 additions & 33 deletions app/pages/project/disks/DisksPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { DiskStateBadge, DiskTypeBadge, ReadOnlyBadge } from '~/components/State
import { makeCrumb } from '~/hooks/use-crumbs'
import { getProjectSelector, useProjectSelector } from '~/hooks/use-params'
import { useQuickActions } from '~/hooks/use-quick-actions'
import { confirmAction } from '~/stores/confirm-action'
import { confirmDelete } from '~/stores/confirm-delete'
import { addToast } from '~/stores/toast'
import { DiskSourceName } from '~/table/cells/DiskSourceCell'
Expand Down Expand Up @@ -87,6 +88,11 @@ export async function clientLoader({ params }: Route.ClientLoaderArgs) {

const colHelper = createColumnHelper<Disk>()

const invalidateDiskListAndView = () => {
queryClient.invalidateEndpoint('diskList')
queryClient.invalidateEndpoint('diskView')
}

export default function DisksPage() {
const { project } = useProjectSelector()

Expand Down Expand Up @@ -115,41 +121,99 @@ export default function DisksPage() {
},
})

// the states that allow these actions are both temporary, so a failure is
// most likely to be due to a state change from afar. for that reason it's
// worth invalidating on failures as well as successes
const { mutateAsync: finalize } = useApiMutation(api.diskFinalizeImport, {
onSuccess: invalidateDiskListAndView,
onError: invalidateDiskListAndView,
})
const { mutateAsync: stopBulkWriteImport } = useApiMutation(api.diskBulkWriteImportStop, {
onSuccess: invalidateDiskListAndView,
onError: invalidateDiskListAndView,
})

const makeActions = useCallback(
(disk: Disk): MenuAction[] => [
{
label: 'Snapshot',
onActivate() {
// prettier-ignore
addToast(<>Creating snapshot of disk <HL>{disk.name}</HL></>)
createSnapshot({
query: { project },
body: {
name: genName(disk.name),
disk: disk.name,
description: '',
},
})
(disk: Disk): MenuAction[] => {
const canStopImport = diskCan.finalizeImport(disk) || diskCan.cancelBulkWrites(disk)

return [
...(canStopImport
? [
{
label: 'Stop import',
onActivate() {
confirmAction({
doAction: async () => {
if (diskCan.cancelBulkWrites(disk)) {
await stopBulkWriteImport({
path: { disk: disk.name },
query: { project },
})
}

await finalize({
path: { disk: disk.name },
query: { project },
body: {},
})

addToast(
<>
Import stopped for <HL>{disk.name}</HL>
</>
)
},
modalTitle: 'Stop import',
modalContent: (
<p>
Are you sure you want to stop import for <HL>{disk.name}</HL>? This
will leave it in the <HL>detached</HL> state.
</p>
),
errorTitle: 'Failed to stop import',
actionType: 'danger',
})
},
},
]
: []),
{
label: 'Snapshot',
onActivate() {
// prettier-ignore
addToast(<>Creating snapshot of disk <HL>{disk.name}</HL></>)
createSnapshot({
query: { project },
body: {
name: genName(disk.name),
disk: disk.name,
description: '',
},
})
},
disabled: snapshotDisabledReason(disk),
},
disabled: snapshotDisabledReason(disk),
},
{
label: 'Delete',
onActivate: confirmDelete({
doDelete: () => deleteDisk({ path: { disk: disk.name }, query: { project } }),
label: disk.name,
resourceKind: 'disk',
}),
disabled:
!diskCan.delete(disk) &&
(disk.state.state === 'attached' ? (
'Disk must be detached before it can be deleted'
) : (
<>Only disks in state {fancifyStates(diskCan.delete.states)} can be deleted</>
)),
},
],
[createSnapshot, deleteDisk, project]
{
label: 'Delete',
onActivate: confirmDelete({
doDelete: () => deleteDisk({ path: { disk: disk.name }, query: { project } }),
label: disk.name,
resourceKind: 'disk',
}),
disabled:
!diskCan.delete(disk) &&
(disk.state.state === 'attached' ? (
'Disk must be detached before it can be deleted'
) : canStopImport ? (
'Import must be stopped before the disk can be deleted'
) : (
<>Only disks in state {fancifyStates(diskCan.delete.states)} can be deleted</>
)),
},
]
},
[createSnapshot, deleteDisk, stopBulkWriteImport, finalize, project]
)

const columns = useColsWithActions(
Expand Down
43 changes: 43 additions & 0 deletions mock-api/disk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,49 @@ export const disks: Json<Disk>[] = [
disk_type: 'distributed',
read_only: false,
},
{
id: '7b898827-35a1-4459-a4e3-34db90640b74',
name: 'tmp-for-image-29884739',
description: 'stuck in import_ready after bailing on an image upload early',
project_id: project.id,
time_created: new Date().toISOString(),
time_modified: new Date().toISOString(),
state: { state: 'import_ready' },
device_path: '/import',
size: 8 * GiB,
block_size: 2048,
disk_type: 'distributed',
read_only: false,
},
{
id: 'f874c0b9-72ad-4eac-8e55-e6090e10366a',
name: 'tmp-for-image-59986861',
description: 'stuck in bulk-write after bailing on an image upload early',
project_id: project.id,
time_created: new Date().toISOString(),
time_modified: new Date().toISOString(),
state: { state: 'importing_from_bulk_writes' },
device_path: '/import',
size: 8 * GiB,
block_size: 2048,
disk_type: 'distributed',
read_only: false,
},
{
id: '0f60c28e-ead0-48f0-aab9-e74b917dc8e4',
name: 'disk-finalize-fail',
description:
"stuck in bulk-write after bailing on an image upload early, but can't be finalized",
project_id: project.id,
time_created: new Date().toISOString(),
time_modified: new Date().toISOString(),
state: { state: 'importing_from_bulk_writes' },
device_path: '/import',
size: 8 * GiB,
block_size: 2048,
disk_type: 'distributed',
read_only: false,
},
{
id: '3f23c80f-c523-4d86-8292-2ca3f807bb12',
name: 'disk-snapshot-error',
Expand Down
4 changes: 3 additions & 1 deletion mock-api/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ export const handlers = makeHandlers({
diskFinalizeImport: ({ path, query, body }) => {
const disk = lookup.disk({ ...path, ...query })

if (disk.name === 'disk-finalize-500') throw internalError('disk finalize failed')
if (disk.name === 'disk-finalize-500' || disk.name === 'disk-finalize-fail') {
throw internalError('disk finalize failed')
}

if (disk.state.state !== 'import_ready') {
throw `Cannot finalize disk in state ${disk.state.state}. Must be import_ready.`
Expand Down
65 changes: 64 additions & 1 deletion test/e2e/disks.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import {
clickRowAction,
clickRowActions,
expect,
expectNoToast,
expectRowVisible,
Expand Down Expand Up @@ -93,7 +94,7 @@ test('List disks and snapshot', async ({ page }) => {
await page.goto('/projects/mock-project/disks')

const table = page.getByRole('table')
await expect(table.getByRole('row')).toHaveCount(16) // 15 + header
await expect(table.getByRole('row')).toHaveCount(19) // 18 + header

// check one attached and one not attached
await expectRowVisible(table, {
Expand Down Expand Up @@ -166,6 +167,68 @@ test('Read-only disk snapshot disabled', async ({ page }) => {
)
})

test('Stop import from import_ready', async ({ page }) => {
const diskImportReadyName = 'tmp-for-image-29884739'
await page.goto('/projects/mock-project/disks')
const table = page.getByRole('table')
await expectRowVisible(table, { name: diskImportReadyName, state: 'import ready' })

await clickRowActions(page, diskImportReadyName)
await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeDisabled()
await page.getByRole('menuitem', { name: 'Stop import' }).click()

const modal = page.getByRole('dialog', { name: 'Stop import' })
await expect(modal).toBeVisible()
await modal.getByRole('button', { name: 'Confirm' }).click()

await expectToast(page, `Import stopped for ${diskImportReadyName}`)
await expectRowVisible(table, { name: diskImportReadyName, state: 'detached' })
await clickRowActions(page, diskImportReadyName)
await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeEnabled()
})

test('Stop import from importing_from_bulk_writes', async ({ page }) => {
const diskImportingName = 'tmp-for-image-59986861'
await page.goto('/projects/mock-project/disks')
const table = page.getByRole('table')
await expectRowVisible(table, {
name: diskImportingName,
state: 'importing from bulk writes',
})

await clickRowActions(page, diskImportingName)
await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeDisabled()
await page.getByRole('menuitem', { name: 'Stop import' }).click()

const modal = page.getByRole('dialog', { name: 'Stop import' })
await expect(modal).toBeVisible()
await modal.getByRole('button', { name: 'Confirm' }).click()

await expectToast(page, `Import stopped for ${diskImportingName}`)
await expectRowVisible(table, { name: diskImportingName, state: 'detached' })
await clickRowActions(page, diskImportingName)
await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeEnabled()
})

test('Stop import from importing_from_bulk_writes shows error and refreshes state when finalize fails', async ({
page,
}) => {
const diskName = 'disk-finalize-fail'
await page.goto('/projects/mock-project/disks')
const table = page.getByRole('table')
await expectRowVisible(table, { name: diskName, state: 'importing from bulk writes' })

await clickRowActions(page, diskName)
await page.getByRole('menuitem', { name: 'Stop import' }).click()
await page
.getByRole('dialog', { name: 'Stop import' })
.getByRole('button', { name: 'Confirm' })
.click()

await expectToast(page, 'Failed to stop import')
await expectRowVisible(table, { name: diskName, state: 'import ready' })
})

test.describe('Disk create', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/projects/mock-project/disks-new')
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/image-upload.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ test.describe('Image upload', () => {

for (const state of cancelStates) {
test(`cancel in state '${state}'`, async ({ page }) => {
await fillForm(page, 'new-image')
await fillForm(page, 'cancel-upload')

await page.getByRole('button', { name: 'Upload image' }).click()

Expand All @@ -219,7 +219,9 @@ test.describe('Image upload', () => {
await page.getByRole('button', { name: 'Cancel' }).click()
await page.getByRole('link', { name: 'Disks' }).click()
await expect(page.getByRole('cell', { name: 'disk-1', exact: true })).toBeVisible()
await expect(page.getByRole('cell', { name: 'tmp' })).toBeHidden()
await expect(
page.getByRole('cell', { name: 'cancel-upload', exact: true })
).toBeHidden()
})
}

Expand Down
Loading