|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import * as Accordion from '@radix-ui/react-accordion' |
| 9 | +import { useState } from 'react' |
| 10 | +import { useNavigate } from 'react-router-dom' |
| 11 | +import type { SetRequired } from 'type-fest' |
| 12 | + |
| 13 | +import { |
| 14 | + apiQueryClient, |
| 15 | + useApiMutation, |
| 16 | + useApiQueryClient, |
| 17 | + usePrefetchedApiQuery, |
| 18 | + type FloatingIpCreate, |
| 19 | + type SiloIpPool, |
| 20 | +} from '@oxide/api' |
| 21 | +import { Badge, Message } from '@oxide/ui' |
| 22 | +import { validateIp } from '@oxide/util' |
| 23 | + |
| 24 | +import { AccordionItem } from 'app/components/AccordionItem' |
| 25 | +import { |
| 26 | + DescriptionField, |
| 27 | + ListboxField, |
| 28 | + NameField, |
| 29 | + SideModalForm, |
| 30 | + TextField, |
| 31 | +} from 'app/components/form' |
| 32 | +import { useForm, useProjectSelector, useToast } from 'app/hooks' |
| 33 | +import { pb } from 'app/util/path-builder' |
| 34 | + |
| 35 | +CreateFloatingIpSideModalForm.loader = async () => { |
| 36 | + await apiQueryClient.prefetchQuery('projectIpPoolList', { |
| 37 | + query: { limit: 1000 }, |
| 38 | + }) |
| 39 | + return null |
| 40 | +} |
| 41 | + |
| 42 | +const toListboxItem = (p: SiloIpPool) => { |
| 43 | + if (!p.isDefault) { |
| 44 | + return { value: p.name, label: p.name } |
| 45 | + } |
| 46 | + // For the default pool, add a label to the dropdown |
| 47 | + return { |
| 48 | + value: p.name, |
| 49 | + labelString: p.name, |
| 50 | + label: ( |
| 51 | + <> |
| 52 | + {p.name}{' '} |
| 53 | + <Badge className="ml-1" color="neutral"> |
| 54 | + default |
| 55 | + </Badge> |
| 56 | + </> |
| 57 | + ), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const defaultValues: SetRequired<FloatingIpCreate, 'address'> = { |
| 62 | + name: '', |
| 63 | + description: '', |
| 64 | + pool: undefined, |
| 65 | + address: '', |
| 66 | +} |
| 67 | + |
| 68 | +export function CreateFloatingIpSideModalForm() { |
| 69 | + // Fetch 1000 to we can be sure to get them all. |
| 70 | + const { data: allPools } = usePrefetchedApiQuery('projectIpPoolList', { |
| 71 | + query: { limit: 1000 }, |
| 72 | + }) |
| 73 | + |
| 74 | + const queryClient = useApiQueryClient() |
| 75 | + const projectSelector = useProjectSelector() |
| 76 | + const addToast = useToast() |
| 77 | + const navigate = useNavigate() |
| 78 | + |
| 79 | + const createFloatingIp = useApiMutation('floatingIpCreate', { |
| 80 | + onSuccess() { |
| 81 | + queryClient.invalidateQueries('floatingIpList') |
| 82 | + addToast({ content: 'Your Floating IP has been created' }) |
| 83 | + navigate(pb.floatingIps(projectSelector)) |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + const form = useForm({ defaultValues }) |
| 88 | + const isPoolSelected = !!form.watch('pool') |
| 89 | + |
| 90 | + const [openItems, setOpenItems] = useState<string[]>([]) |
| 91 | + |
| 92 | + return ( |
| 93 | + <SideModalForm |
| 94 | + id="create-floating-ip-form" |
| 95 | + title="Create Floating IP" |
| 96 | + form={form} |
| 97 | + onDismiss={() => navigate(pb.floatingIps(projectSelector))} |
| 98 | + onSubmit={({ address, ...rest }) => { |
| 99 | + createFloatingIp.mutate({ |
| 100 | + query: projectSelector, |
| 101 | + // if address is '', evaluate as false and send as undefined |
| 102 | + body: { address: address || undefined, ...rest }, |
| 103 | + }) |
| 104 | + }} |
| 105 | + loading={createFloatingIp.isPending} |
| 106 | + submitError={createFloatingIp.error} |
| 107 | + > |
| 108 | + <NameField name="name" control={form.control} /> |
| 109 | + <DescriptionField name="description" control={form.control} /> |
| 110 | + |
| 111 | + <Accordion.Root |
| 112 | + type="multiple" |
| 113 | + className="mt-12 max-w-lg" |
| 114 | + value={openItems} |
| 115 | + onValueChange={setOpenItems} |
| 116 | + > |
| 117 | + <AccordionItem |
| 118 | + isOpen={openItems.includes('advanced')} |
| 119 | + label="Advanced" |
| 120 | + value="advanced" |
| 121 | + > |
| 122 | + <Message |
| 123 | + variant="info" |
| 124 | + content="If you don’t specify a pool, the default will be used" |
| 125 | + /> |
| 126 | + |
| 127 | + <ListboxField |
| 128 | + name="pool" |
| 129 | + items={allPools.items.map((p) => toListboxItem(p))} |
| 130 | + label="IP pool" |
| 131 | + control={form.control} |
| 132 | + placeholder="Select pool" |
| 133 | + /> |
| 134 | + <TextField |
| 135 | + name="address" |
| 136 | + control={form.control} |
| 137 | + disabled={!isPoolSelected} |
| 138 | + transform={(v) => v.replace(/\s/g, '')} |
| 139 | + validate={(ip) => |
| 140 | + ip && !validateIp(ip).valid ? 'Not a valid IP address' : true |
| 141 | + } |
| 142 | + /> |
| 143 | + </AccordionItem> |
| 144 | + </Accordion.Root> |
| 145 | + </SideModalForm> |
| 146 | + ) |
| 147 | +} |
0 commit comments