|
| 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 { useState } from 'react' |
| 9 | +import type { Control } from 'react-hook-form' |
| 10 | +import { useController } from 'react-hook-form' |
| 11 | +import type { Merge } from 'type-fest' |
| 12 | + |
| 13 | +import type { CertificateCreate } from '@oxide/api' |
| 14 | +import { Button, Error16Icon, FieldLabel, MiniTable, Modal } from '@oxide/ui' |
| 15 | + |
| 16 | +import { DescriptionField, FileField, TextField, validateName } from 'app/components/form' |
| 17 | +import type { SiloCreateFormValues } from 'app/forms/silo-create' |
| 18 | +import { useForm } from 'app/hooks' |
| 19 | + |
| 20 | +export function TlsCertsField({ control }: { control: Control<SiloCreateFormValues> }) { |
| 21 | + const [showAddCert, setShowAddCert] = useState(false) |
| 22 | + |
| 23 | + const { |
| 24 | + field: { value: items, onChange }, |
| 25 | + } = useController({ control, name: 'tlsCertificates' }) |
| 26 | + |
| 27 | + return ( |
| 28 | + <> |
| 29 | + <div className="max-w-lg"> |
| 30 | + <FieldLabel id="tls-certificates-label" className="mb-3"> |
| 31 | + TLS Certificates |
| 32 | + </FieldLabel> |
| 33 | + {!!items.length && ( |
| 34 | + <MiniTable.Table className="mb-4"> |
| 35 | + <MiniTable.Header> |
| 36 | + <MiniTable.HeadCell>Name</MiniTable.HeadCell> |
| 37 | + {/* For remove button */} |
| 38 | + <MiniTable.HeadCell className="w-12" /> |
| 39 | + </MiniTable.Header> |
| 40 | + <MiniTable.Body> |
| 41 | + {items.map((item, index) => ( |
| 42 | + <MiniTable.Row |
| 43 | + tabIndex={0} |
| 44 | + aria-rowindex={index + 1} |
| 45 | + aria-label={`Name: ${item.name}, Description: ${item.description}`} |
| 46 | + key={item.name} |
| 47 | + > |
| 48 | + <MiniTable.Cell>{item.name}</MiniTable.Cell> |
| 49 | + <MiniTable.Cell> |
| 50 | + <button |
| 51 | + onClick={() => onChange(items.filter((i) => i.name !== item.name))} |
| 52 | + > |
| 53 | + <Error16Icon title={`remove ${item.name}`} /> |
| 54 | + </button> |
| 55 | + </MiniTable.Cell> |
| 56 | + </MiniTable.Row> |
| 57 | + ))} |
| 58 | + </MiniTable.Body> |
| 59 | + </MiniTable.Table> |
| 60 | + )} |
| 61 | + |
| 62 | + <Button size="sm" onClick={() => setShowAddCert(true)}> |
| 63 | + Add TLS certificate |
| 64 | + </Button> |
| 65 | + </div> |
| 66 | + |
| 67 | + {showAddCert && ( |
| 68 | + <AddCertModal |
| 69 | + onDismiss={() => setShowAddCert(false)} |
| 70 | + onSubmit={async (values) => { |
| 71 | + const certCreate: (typeof items)[number] = { |
| 72 | + ...values, |
| 73 | + // cert and key are required fields. they will always be present if we get here |
| 74 | + cert: await values.cert!.text(), |
| 75 | + key: await values.key!.text(), |
| 76 | + } |
| 77 | + onChange([...items, certCreate]) |
| 78 | + setShowAddCert(false) |
| 79 | + }} |
| 80 | + allNames={items.map((item) => item.name)} |
| 81 | + /> |
| 82 | + )} |
| 83 | + </> |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +export type CertFormValues = Merge< |
| 88 | + CertificateCreate, |
| 89 | + { key: File | null; cert: File | null } // swap strings for Files |
| 90 | +> |
| 91 | + |
| 92 | +const defaultValues: CertFormValues = { |
| 93 | + description: '', |
| 94 | + name: '', |
| 95 | + service: 'external_api', |
| 96 | + key: null, |
| 97 | + cert: null, |
| 98 | +} |
| 99 | + |
| 100 | +type AddCertModalProps = { |
| 101 | + onDismiss: () => void |
| 102 | + onSubmit: (values: CertFormValues) => void |
| 103 | + allNames: string[] |
| 104 | +} |
| 105 | + |
| 106 | +const AddCertModal = ({ onDismiss, onSubmit, allNames }: AddCertModalProps) => { |
| 107 | + const { control, handleSubmit } = useForm<CertFormValues>({ defaultValues }) |
| 108 | + |
| 109 | + return ( |
| 110 | + <Modal isOpen onDismiss={onDismiss} title="Add TLS certificate"> |
| 111 | + <Modal.Body> |
| 112 | + <form autoComplete="off" onSubmit={handleSubmit(onSubmit)}> |
| 113 | + <Modal.Section> |
| 114 | + <TextField |
| 115 | + name="name" |
| 116 | + control={control} |
| 117 | + required |
| 118 | + // this field is identical to NameField (which just does |
| 119 | + // validateName for you) except we also want to check that the |
| 120 | + // name is not in the list of certs you've already added |
| 121 | + validate={(name) => { |
| 122 | + if (allNames.includes(name)) { |
| 123 | + return 'A certificate with this name already exists' |
| 124 | + } |
| 125 | + return validateName(name, 'Name', true) |
| 126 | + }} |
| 127 | + /> |
| 128 | + <DescriptionField name="description" control={control} /> |
| 129 | + <FileField |
| 130 | + id="cert-input" |
| 131 | + name="cert" |
| 132 | + label="Cert" |
| 133 | + required |
| 134 | + control={control} |
| 135 | + /> |
| 136 | + <FileField id="key-input" name="key" label="Key" required control={control} /> |
| 137 | + </Modal.Section> |
| 138 | + </form> |
| 139 | + </Modal.Body> |
| 140 | + <Modal.Footer |
| 141 | + onDismiss={onDismiss} |
| 142 | + onAction={handleSubmit(onSubmit)} |
| 143 | + actionText="Add Certificate" |
| 144 | + /> |
| 145 | + </Modal> |
| 146 | + ) |
| 147 | +} |
0 commit comments