77 */
88import { useForm } from 'react-hook-form'
99import { useNavigate } from 'react-router'
10+ import type { RequiredDeep } from 'type-fest'
1011
11- import { api , q , queryClient , useApiMutation , type VpcCreate } from '@oxide/api'
12+ import {
13+ api ,
14+ q ,
15+ queryClient ,
16+ useApiMutation ,
17+ type VpcCreate ,
18+ type VpcCreateDefaults ,
19+ } from '@oxide/api'
1220
21+ import { CheckboxField } from '~/components/form/fields/CheckboxField'
1322import { DescriptionField } from '~/components/form/fields/DescriptionField'
1423import { NameField } from '~/components/form/fields/NameField'
1524import { TextField } from '~/components/form/fields/TextField'
@@ -19,6 +28,7 @@ import { titleCrumb } from '~/hooks/use-crumbs'
1928import { useProjectSelector } from '~/hooks/use-params'
2029import { addToast } from '~/stores/toast'
2130import { SideModalFormDocs } from '~/ui/lib/ModalLinks'
31+ import { TipIcon } from '~/ui/lib/TipIcon'
2232import { validateVpcIpv6Prefix } from '~/util/ip'
2333import { docLinks } from '~/util/links'
2434import { pb } from '~/util/path-builder'
@@ -28,15 +38,15 @@ type VpcCreateFormValues = {
2838 description : string
2939 dnsName : string
3040 ipv6Prefix : string
31- // defaults will go here once we support it in the UI. for now, omitting means
32- // all defaults
41+ createDefaultSubnet : boolean
3342}
3443
3544const defaultValues : VpcCreateFormValues = {
3645 name : '' ,
3746 description : '' ,
3847 dnsName : '' ,
3948 ipv6Prefix : '' ,
49+ createDefaultSubnet : true ,
4050}
4151
4252export const handle = titleCrumb ( 'New VPC' )
@@ -67,9 +77,16 @@ export default function CreateVpcSideModalForm() {
6777 form = { form }
6878 formType = "create"
6979 resourceName = "VPC"
70- onSubmit = { ( { ipv6Prefix, ...rest } ) => {
71- // omitting `defaults` means create all defaults, i.e., the original behavior
72- const body : VpcCreate = { ...rest , ipv6Prefix : ipv6Prefix . trim ( ) || undefined }
80+ onSubmit = { ( { ipv6Prefix, createDefaultSubnet, ...rest } ) => {
81+ const body : VpcCreate = {
82+ ...rest ,
83+ ipv6Prefix : ipv6Prefix . trim ( ) || undefined ,
84+ // RequiredDeep will make tsc fail if an API bump adds a new
85+ // default resource, so we can decide how to handle it
86+ defaults : createDefaultSubnet
87+ ? ( { subnet : { } } satisfies RequiredDeep < VpcCreateDefaults > )
88+ : { } ,
89+ }
7390 createVpc . mutate ( { query : projectSelector , body } )
7491 } }
7592 onDismiss = { ( ) => navigate ( pb . vpcs ( projectSelector ) ) }
@@ -90,6 +107,17 @@ export default function CreateVpcSideModalForm() {
90107 return validateVpcIpv6Prefix ( prefix )
91108 } }
92109 />
110+ < span className = "inline-flex items-center gap-1.5" >
111+ < CheckboxField name = "createDefaultSubnet" control = { form . control } >
112+ Create default subnet
113+ </ CheckboxField >
114+ < TipIcon >
115+ < p >
116+ An instance can only attach to a VPC with a subnet.
117+ < br /> A subnet can be added later.
118+ </ p >
119+ </ TipIcon >
120+ </ span >
93121 < SideModalFormDocs docs = { [ docLinks . vpcs ] } />
94122 </ SideModalForm >
95123 )
0 commit comments