-
Notifications
You must be signed in to change notification settings - Fork 21
Simplify IpPoolSelector and call sites
#3032
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
Changes from all commits
8da99ee
fc1f8bf
688f05e
03e2f66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,8 @@ | |
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { useEffect, useMemo } from 'react' | ||
| import type { Control, UseFormSetValue } from 'react-hook-form' | ||
| import { useMemo } from 'react' | ||
| import type { Control } from 'react-hook-form' | ||
| import * as R from 'remeda' | ||
|
|
||
| import { poolHasIpVersion, type IpVersion, type UnicastIpPool } from '@oxide/api' | ||
|
|
@@ -19,30 +19,22 @@ const ALL_IP_VERSIONS: IpVersion[] = ['v4', 'v6'] | |
| type IpPoolSelectorProps = { | ||
| control: Control<any> | ||
| poolFieldName: string | ||
| ipVersionFieldName: string | ||
| pools: UnicastIpPool[] | ||
| /** Current value of the pool field */ | ||
| currentPool: string | undefined | ||
| /** Function to update form values */ | ||
| setValue: UseFormSetValue<any> | ||
| disabled?: boolean | ||
| /** Compatible IP versions based on network interface type */ | ||
| compatibleVersions?: IpVersion[] | ||
| /** | ||
| * If true, automatically select a default pool when none is selected. | ||
| * If false, allow the field to remain empty to use API defaults. | ||
| * Default to true, to automatically select a default pool if available. | ||
| * If true, the pool field is required and defaults should be selected by | ||
| * the parent when available. If false, allow the field to remain empty to | ||
| * use API defaults. | ||
| */ | ||
| autoSelectDefault?: boolean | ||
| } | ||
|
|
||
| export function IpPoolSelector({ | ||
| control, | ||
| poolFieldName, | ||
| ipVersionFieldName, | ||
| pools, | ||
| currentPool, | ||
| setValue, | ||
| disabled = false, | ||
| compatibleVersions = ALL_IP_VERSIONS, | ||
| // When both a default IPv4 and default IPv6 pool exist, the component picks the | ||
|
|
@@ -59,44 +51,6 @@ export function IpPoolSelector({ | |
|
|
||
| const hasNoPools = sortedPools.length === 0 | ||
|
|
||
| // Set default pool selection on mount if none selected, or if current pool is no longer valid | ||
| useEffect(() => { | ||
| if (sortedPools.length > 0 && autoSelectDefault) { | ||
| const currentPoolValid = | ||
| currentPool && sortedPools.some((p) => p.name === currentPool) | ||
|
|
||
| if (!currentPoolValid) { | ||
| // Only auto-select when there's an actual default pool | ||
| const defaultPool = sortedPools.find((p) => p.isDefault) | ||
|
|
||
| if (defaultPool) { | ||
| setValue(poolFieldName, defaultPool.name) | ||
| setValue(ipVersionFieldName, defaultPool.ipVersion) | ||
| } else { | ||
| // Clear selection when current pool is invalid and no compatible default exists | ||
| setValue(poolFieldName, '') | ||
| } | ||
| } | ||
| } | ||
| }, [ | ||
| currentPool, | ||
| sortedPools, | ||
| poolFieldName, | ||
| ipVersionFieldName, | ||
| setValue, | ||
| autoSelectDefault, | ||
| ]) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is only really necessary for instance create because that's the only one where the stack can change. We calculate it for ephemeral IP modal but we can do it once up front because it's in the context of an instance where the stack is fixed. |
||
|
|
||
| // Update IP version when pool changes | ||
| useEffect(() => { | ||
| if (currentPool) { | ||
| const selectedPool = sortedPools.find((p) => p.name === currentPool) | ||
| if (selectedPool) { | ||
| setValue(ipVersionFieldName, selectedPool.ipVersion) | ||
| } | ||
| } | ||
| }, [currentPool, sortedPools, ipVersionFieldName, setValue]) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not necessary at all if you derive it at submit time |
||
|
|
||
| return ( | ||
| <div className="space-y-4"> | ||
| {hasNoPools ? ( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Note
v4fallback where when there are no defaults doesn't really make sense because auto will fail in that case. What we should do is make the user pick. The simplest thing would be to always make the user pick and just not use the auto side here.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.
I believe this is now how you have it, where the form is disabled with the
No default compatible pool available; select a pool to continuemessage, so I don't know that anyone is going to end up hitting this.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.
Ah, that's right. Insight applied here for more cleanup 72b671a