-
Notifications
You must be signed in to change notification settings - Fork 11
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
Extract common fields and refactor VPC router route forms #2436
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eec6ced
Extract common fields and refactor
charliepark 6fcce46
Clarify comment
charliepark ddc10cf
Update useEffect to include value
charliepark 611b475
Inline props and remove unnecessary consts
charliepark 00c02db
Pass form to move more shared code into common component
charliepark 398d58c
Reintroduce typechecking against API for destination types and target…
charliepark d89087d
typo
charliepark 9d1acd1
Move default values const out of component
charliepark 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 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,117 @@ | ||
/* | ||
* 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 { UseFormReturn } from 'react-hook-form' | ||
|
||
import type { | ||
RouteDestination, | ||
RouterRouteCreate, | ||
RouterRouteUpdate, | ||
RouteTarget, | ||
} from '~/api' | ||
import { DescriptionField } from '~/components/form/fields/DescriptionField' | ||
import { ListboxField } from '~/components/form/fields/ListboxField' | ||
import { NameField } from '~/components/form/fields/NameField' | ||
import { TextField } from '~/components/form/fields/TextField' | ||
import { Message } from '~/ui/lib/Message' | ||
|
||
export type RouteFormValues = RouterRouteCreate | Required<RouterRouteUpdate> | ||
|
||
export const routeFormMessage = { | ||
vpcSubnetNotModifiable: | ||
'Routes of type VPC Subnet within the system router are not modifiable', | ||
internetGatewayTargetValue: | ||
'For ‘Internet gateway’ targets, the value must be ‘outbound’', | ||
// https://github.com/oxidecomputer/omicron/blob/914f5fd7d51f9b060dcc0382a30b607e25df49b2/nexus/src/app/vpc_router.rs#L201-L204 | ||
noNewRoutesOnSystemRouter: 'User-provided routes cannot be added to a system router', | ||
// https://github.com/oxidecomputer/omicron/blob/914f5fd7d51f9b060dcc0382a30b607e25df49b2/nexus/src/app/vpc_router.rs#L300-L304 | ||
noDeletingRoutesOnSystemRouter: 'System routes cannot be deleted', | ||
// https://github.com/oxidecomputer/omicron/blob/914f5fd7d51f9b060dcc0382a30b607e25df49b2/nexus/src/app/vpc_router.rs#L136-L138 | ||
noDeletingSystemRouters: 'System routers cannot be deleted', | ||
} | ||
|
||
// VPCs cannot be specified as a destination in custom routers | ||
// https://github.com/oxidecomputer/omicron/blob/4f27433d1bca57eb02073a4ea1cd14557f70b8c7/nexus/src/app/vpc_router.rs#L363 | ||
const destTypes: Record<Exclude<RouteDestination['type'], 'vpc'>, string> = { | ||
ip: 'IP', | ||
ip_net: 'IP network', | ||
subnet: 'Subnet', | ||
} | ||
|
||
// Subnets and VPCs cannot be used as a target in custom routers | ||
// https://github.com/oxidecomputer/omicron/blob/4f27433d1bca57eb02073a4ea1cd14557f70b8c7/nexus/src/app/vpc_router.rs#L362-L368 | ||
const targetTypes: Record<Exclude<RouteTarget['type'], 'subnet' | 'vpc'>, string> = { | ||
ip: 'IP', | ||
instance: 'Instance', | ||
internet_gateway: 'Internet gateway', | ||
drop: 'Drop', | ||
} | ||
|
||
const toItems = (mapping: Record<string, string>) => | ||
Object.entries(mapping).map(([value, label]) => ({ value, label })) | ||
|
||
type RouteFormFieldsProps = { | ||
form: UseFormReturn<RouteFormValues> | ||
isDisabled?: boolean | ||
} | ||
export const RouteFormFields = ({ form, isDisabled }: RouteFormFieldsProps) => { | ||
const { control } = form | ||
const targetType = form.watch('target.type') | ||
return ( | ||
<> | ||
{isDisabled && ( | ||
<Message variant="info" content={routeFormMessage.vpcSubnetNotModifiable} /> | ||
)} | ||
<NameField name="name" control={control} disabled={isDisabled} /> | ||
<DescriptionField name="description" control={control} disabled={isDisabled} /> | ||
<ListboxField | ||
name="destination.type" | ||
label="Destination type" | ||
control={control} | ||
items={toItems(destTypes)} | ||
placeholder="Select a destination type" | ||
required | ||
disabled={isDisabled} | ||
/> | ||
<TextField | ||
name="destination.value" | ||
label="Destination value" | ||
control={control} | ||
placeholder="Enter a destination value" | ||
required | ||
disabled={isDisabled} | ||
/> | ||
<ListboxField | ||
name="target.type" | ||
label="Target type" | ||
control={control} | ||
items={toItems(targetTypes)} | ||
placeholder="Select a target type" | ||
required | ||
onChange={(value) => { | ||
form.setValue('target.value', value === 'internet_gateway' ? 'outbound' : '') | ||
}} | ||
disabled={isDisabled} | ||
/> | ||
{targetType !== 'drop' && ( | ||
<TextField | ||
name="target.value" | ||
label="Target value" | ||
control={control} | ||
placeholder="Enter a target value" | ||
required | ||
// 'internet_gateway' targetTypes can only have the value 'outbound', so we disable the field | ||
disabled={isDisabled || targetType === 'internet_gateway'} | ||
description={ | ||
targetType === 'internet_gateway' && routeFormMessage.internetGatewayTargetValue | ||
} | ||
/> | ||
)} | ||
</> | ||
) | ||
} |
This file contains 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 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.
I see this is a change from the previous logic. Good one, it makes way more sense to always clear it. There is one minor issue but it's tolerable: "changing" the type to the same value it already is fires
onChange
and clears the value (I tried it). The brute force fix would be to check if the new type is different from the current type before firingsetValue
. I'm also fine without that. I checked the headless listbox docs and it doesn't seem like there's an obvious way to tell it not to fireonChange
unless the value has actually changed.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 experimented with a few iterations with the brute force "pass a conditional prop through with the frozen originalTargetType and compare it to the form's targetType at the onChange firing time", but things got fairly squirrelly. For now, I'll get this folded in, as I think clearing the value is better as the default action. Absolutely good to revisit it down the road if we find UX friction on the field being problematic.