Skip to content

Commit da9d8b5

Browse files
authored
Drop instanceState row hack from instance NIC and disk tables (#3365)
We had a hack in a couple of spots to prevent certain row actions componenents from unmounting and remounting, which would cause an open row actions menu to close. #3364 makes the hack unnecessary, so we remove it. 4dccaed adds e2e tests for those exact scenarios and shows they pass in CI. But those tests are too specific, so they're not worth keeping around. The next commit reverts them. The last one adds some vitest browser tests that are a little more general, showing how an open menu behaves when its underlying state changes (in this case a delete button going from disabled to enabled).
1 parent 7ac1e3c commit da9d8b5

3 files changed

Lines changed: 40 additions & 74 deletions

File tree

‎app/pages/project/instances/NetworkingTab.tsx‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
type ExternalIp,
2525
type ExternalSubnet,
2626
type InstanceNetworkInterface,
27-
type InstanceState,
2827
type IpVersion,
2928
} from '@oxide/api'
3029
import { IpGlobal24Icon, Networking24Icon } from '@oxide/design-system/icons/react'
@@ -194,15 +193,7 @@ export async function clientLoader({ params }: LoaderFunctionArgs) {
194193
return null
195194
}
196195

197-
// Bit of a hack: by putting the instance state in the row data, we can avoid
198-
// remaking the row actions callback whenever the instance state changes, which
199-
// causes the whole table to get re-rendered, which jarringly closes any open
200-
// row actions menus
201-
type NicRow = InstanceNetworkInterface & {
202-
instanceState: InstanceState
203-
}
204-
205-
const colHelper = createColumnHelper<NicRow>()
196+
const colHelper = createColumnHelper<InstanceNetworkInterface>()
206197
const staticCols = [
207198
colHelper.accessor('name', {
208199
header: 'name',
@@ -417,8 +408,8 @@ export default function NetworkingTab() {
417408
const multipleNics = nics.length > 1
418409

419410
const makeActions = useCallback(
420-
(nic: NicRow): MenuAction[] => {
421-
const canUpdateNic = instanceCan.updateNic({ runState: nic.instanceState })
411+
(nic: InstanceNetworkInterface): MenuAction[] => {
412+
const canUpdateNic = instanceCan.updateNic(instance)
422413

423414
const deleteDisabledReason = () => {
424415
if (!canUpdateNic) {
@@ -480,19 +471,14 @@ export default function NetworkingTab() {
480471
},
481472
]
482473
},
483-
[deleteNic, editNic, instanceSelector, multipleNics]
474+
[deleteNic, editNic, instance, instanceSelector, multipleNics]
484475
)
485476

486477
const columns = useColsWithActions(staticCols, makeActions)
487478

488-
const nicRows = useMemo(
489-
() => nics.map((nic) => ({ ...nic, instanceState: instance.runState })),
490-
[nics, instance]
491-
)
492-
493479
const tableInstance = useReactTable({
494480
columns,
495-
data: nicRows,
481+
data: nics,
496482
getCoreRowModel: getCoreRowModel(),
497483
})
498484

‎app/pages/project/instances/StorageTab.tsx‎

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
useApiMutation,
2020
usePrefetchedQuery,
2121
type Disk,
22-
type InstanceState,
2322
} from '@oxide/api'
2423
import { Storage24Icon } from '@oxide/design-system/icons/react'
2524

@@ -61,15 +60,7 @@ export async function clientLoader({ params }: LoaderFunctionArgs) {
6160
return null
6261
}
6362

64-
// Bit of a hack: by putting the instance state in the row data, we can avoid
65-
// remaking the row actions callback whenever the instance state changes, which
66-
// causes the whole table to get re-rendered, which jarringly closes any open
67-
// row actions menus
68-
type InstanceDisk = Disk & {
69-
instanceState: InstanceState
70-
}
71-
72-
const colHelper = createColumnHelper<InstanceDisk>()
63+
const colHelper = createColumnHelper<Disk>()
7364

7465
export const handle = { crumb: 'Storage' }
7566

@@ -152,7 +143,7 @@ export default function StorageTab() {
152143

153144
// shared between boot and other disks
154145
const getSnapshotAction = useCallback(
155-
(disk: InstanceDisk) => ({
146+
(disk: Disk) => ({
156147
label: 'Snapshot',
157148
disabled: snapshotDisabledReason(disk),
158149
onActivate() {
@@ -173,11 +164,11 @@ export default function StorageTab() {
173164
)
174165

175166
const makeBootDiskActions = useCallback(
176-
(disk: InstanceDisk): MenuAction[] => [
167+
(disk: Disk): MenuAction[] => [
177168
getSnapshotAction(disk),
178169
{
179170
label: 'Unset as boot disk',
180-
disabled: !instanceCan.updateBootDisk({ runState: disk.instanceState }) && (
171+
disabled: !instanceCan.updateBootDisk(instance) && (
181172
<>
182173
Instance must be <span className="text-raise">stopped</span> before boot disk
183174
can be changed
@@ -223,26 +214,15 @@ export default function StorageTab() {
223214
onActivate() {}, // it's always disabled, so noop is ok
224215
},
225216
],
226-
[
227-
instanceUpdate,
228-
// don't put the entire instance in here. it is not referentially
229-
// stable across polls, so the menus will close during polling
230-
instance.id,
231-
instance.autoRestartPolicy,
232-
instance.ncpus,
233-
instance.memory,
234-
instance.cpuPlatform,
235-
instance.enableJumboFrames,
236-
getSnapshotAction,
237-
]
217+
[instanceUpdate, instance, getSnapshotAction]
238218
)
239219

240220
const makeOtherDiskActions = useCallback(
241-
(disk: InstanceDisk): MenuAction[] => [
221+
(disk: Disk): MenuAction[] => [
242222
getSnapshotAction(disk),
243223
{
244224
label: 'Set as boot disk',
245-
disabled: !instanceCan.updateBootDisk({ runState: disk.instanceState }) && (
225+
disabled: !instanceCan.updateBootDisk(instance) && (
246226
<>
247227
Instance must be <span className="text-raise">stopped</span> before boot disk
248228
can be changed
@@ -286,7 +266,7 @@ export default function StorageTab() {
286266
},
287267
{
288268
label: 'Detach',
289-
disabled: !instanceCan.detachDisk({ runState: disk.instanceState }) && (
269+
disabled: !instanceCan.detachDisk(instance) && (
290270
<>
291271
Instance must be <span className="text-raise">stopped</span> before disk can be
292272
detached
@@ -304,20 +284,7 @@ export default function StorageTab() {
304284
}),
305285
},
306286
],
307-
[
308-
detachDisk,
309-
instanceUpdate,
310-
// don't put the entire instance in here. it is not referentially
311-
// stable across polls, so the menus will close during polling
312-
instance.id,
313-
instance.autoRestartPolicy,
314-
instance.ncpus,
315-
instance.memory,
316-
instance.cpuPlatform,
317-
instance.enableJumboFrames,
318-
getSnapshotAction,
319-
bootDisks,
320-
]
287+
[detachDisk, instanceUpdate, instance, getSnapshotAction, bootDisks]
321288
)
322289

323290
// attach step of the create-then-attach flow only; the attach modal owns its
@@ -340,19 +307,13 @@ export default function StorageTab() {
340307

341308
const bootDisksTable = useReactTable({
342309
columns: useColsWithActions(staticCols, makeBootDiskActions),
343-
data: useMemo(
344-
() => bootDisks.map((disk) => ({ ...disk, instanceState: instance.runState })),
345-
[bootDisks, instance.runState]
346-
),
310+
data: bootDisks,
347311
getCoreRowModel: getCoreRowModel(),
348312
})
349313

350314
const otherDisksTable = useReactTable({
351315
columns: useColsWithActions(staticCols, makeOtherDiskActions),
352-
data: useMemo(
353-
() => otherDisks.map((disk) => ({ ...disk, instanceState: instance.runState })),
354-
[otherDisks, instance.runState]
355-
),
316+
data: otherDisks,
356317
getCoreRowModel: getCoreRowModel(),
357318
})
358319

‎app/table/columns/action-col.browser.spec.tsx‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ type Item = { id: string; name: string }
1818
const data: Item[] = [{ id: '1', name: 'alpha' }]
1919
const staticCols: ColumnDef<Item>[] = [{ accessorKey: 'name', header: 'Name' }]
2020

21-
function ActionsTable({ version }: { version: number }) {
22-
// depends on `version` so its identity changes when the prop changes, like a
23-
// real page whose actions depend on a background query result
21+
function ActionsTable({ version, locked }: { version: number; locked?: boolean }) {
22+
// depends on `version` and `locked` so its identity changes when the props
23+
// change, like a real page whose actions depend on a polled query result
2424
const makeActions = useCallback(
2525
(item: Item): MenuAction[] => [
26-
{ label: `Rename ${item.name} v${version}`, onActivate: () => {} },
26+
{
27+
label: `Rename ${item.name} v${version}`,
28+
onActivate: () => {},
29+
disabled: locked && `${item.name} is locked`,
30+
},
2731
],
28-
[version]
32+
[version, locked]
2933
)
3034
const columns = useColsWithActions(staticCols, makeActions)
3135
const table = useReactTable({
@@ -53,3 +57,18 @@ test('open row actions menu survives makeActions identity change', async () => {
5357
.element(screen.getByRole('menuitem', { name: 'Rename alpha v2' }))
5458
.toBeVisible()
5559
})
60+
61+
test('open row actions menu updates items when actions change', async () => {
62+
const screen = await render(<ActionsTable version={1} locked />)
63+
64+
await screen.getByRole('button', { name: 'Row actions' }).click()
65+
const item = screen.getByRole('menuitem', { name: 'Rename alpha v1' })
66+
await expect.element(item).toHaveAttribute('aria-disabled', 'true')
67+
68+
// like an instance poll landing while a menu is open: the item should flip
69+
// to enabled in place rather than the menu closing or going stale
70+
await screen.rerender(<ActionsTable version={1} locked={false} />)
71+
72+
await expect.element(item).toBeVisible()
73+
await expect.element(item).not.toHaveAttribute('aria-disabled')
74+
})

0 commit comments

Comments
 (0)