|
| 1 | +import {useEffect, useState} from 'react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | + |
| 4 | +import {Button} from 'sentry/components/core/button'; |
| 5 | +import {Input} from 'sentry/components/core/input'; |
| 6 | +import type {FormFieldProps} from 'sentry/components/forms/formField'; |
| 7 | +import FormField from 'sentry/components/forms/formField'; |
| 8 | +import FormFieldControlState from 'sentry/components/forms/formField/controlState'; |
| 9 | +import {IconAdd, IconDelete} from 'sentry/icons'; |
| 10 | +import {t} from 'sentry/locale'; |
| 11 | +import {space} from 'sentry/styles/space'; |
| 12 | +import {uniqueId} from 'sentry/utils/guid'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Matches characters that are not valid in a header name. |
| 16 | + */ |
| 17 | +const INVALID_NAME_HEADER_REGEX = new RegExp(/[^a-zA-Z0-9_-]+/g); |
| 18 | + |
| 19 | +type HeaderEntry = [id: string, name: string, value: string]; |
| 20 | + |
| 21 | +// XXX(epurkhiser): The types of the FormField render props are absolutely |
| 22 | +// abysmal, so we're leaving this untyped for now. |
| 23 | + |
| 24 | +function UptimHeadersControl(props: any) { |
| 25 | + const {onChange, onBlur, disabled, model, name, value} = props; |
| 26 | + |
| 27 | + // Store itmes in local state so we can add empty values without persisting |
| 28 | + // those into the form model. |
| 29 | + const [items, setItems] = useState<HeaderEntry[]>(() => |
| 30 | + Object.keys(value).length > 0 |
| 31 | + ? value.map((v: any) => [uniqueId(), ...v] as HeaderEntry) |
| 32 | + : [[uniqueId(), '', '']] |
| 33 | + ); |
| 34 | + |
| 35 | + // Persist the field value back to the form model on changes to the items |
| 36 | + // list. Empty items are discarded and not persisted. |
| 37 | + useEffect(() => { |
| 38 | + const newValue = items.filter(item => item[1] !== '').map(item => [item[1], item[2]]); |
| 39 | + |
| 40 | + onChange(newValue, {}); |
| 41 | + onBlur(newValue, {}); |
| 42 | + }, [items, onChange, onBlur]); |
| 43 | + |
| 44 | + function addItem() { |
| 45 | + setItems(currentItems => [...currentItems, [uniqueId(), '', '']]); |
| 46 | + } |
| 47 | + |
| 48 | + function removeItem(index: number) { |
| 49 | + setItems(currentItems => currentItems.toSpliced(index, 1)); |
| 50 | + } |
| 51 | + |
| 52 | + function handleNameChange(index: number, newName: string) { |
| 53 | + setItems(currentItems => |
| 54 | + currentItems.toSpliced(index, 1, [ |
| 55 | + items[index]![0], |
| 56 | + newName.replaceAll(INVALID_NAME_HEADER_REGEX, ''), |
| 57 | + items[index]![2], |
| 58 | + ]) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + function handleValueChange(index: number, newHeaderValue: string) { |
| 63 | + setItems(currentItems => |
| 64 | + currentItems.toSpliced(index, 1, [ |
| 65 | + items[index]![0], |
| 66 | + items[index]![1], |
| 67 | + newHeaderValue, |
| 68 | + ]) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Disambiguates headers that are named the same by adding a `(x)` number to |
| 74 | + * the end of the name in the order they were added. |
| 75 | + */ |
| 76 | + function disambiguateHeaderName(index: number) { |
| 77 | + const headerName = items[index]![1]; |
| 78 | + const matchingIndexes = items |
| 79 | + .map((item, idx) => [idx, item[1]]) |
| 80 | + .filter(([_, itemName]) => itemName === headerName) |
| 81 | + .map(([idx]) => idx); |
| 82 | + |
| 83 | + const duplicateIndex = matchingIndexes.indexOf(index) + 1; |
| 84 | + |
| 85 | + return duplicateIndex === 1 ? headerName : `${headerName} (${duplicateIndex})`; |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <HeadersContainer> |
| 90 | + {items.length > 0 && ( |
| 91 | + <HeaderItems> |
| 92 | + {items.map(([id, headerName, headerValue], index) => ( |
| 93 | + <HeaderRow key={id}> |
| 94 | + <Input |
| 95 | + monospace |
| 96 | + disabled={disabled} |
| 97 | + value={headerName ?? ''} |
| 98 | + placeholder="X-Header-Value" |
| 99 | + onChange={e => handleNameChange(index, e.target.value)} |
| 100 | + aria-label={t('Name of header %s', index + 1)} |
| 101 | + /> |
| 102 | + <Input |
| 103 | + monospace |
| 104 | + disabled={disabled} |
| 105 | + value={headerValue ?? ''} |
| 106 | + placeholder={t('Header Value')} |
| 107 | + onChange={e => handleValueChange(index, e.target.value)} |
| 108 | + aria-label={ |
| 109 | + headerName |
| 110 | + ? t('Value of %s', disambiguateHeaderName(index)) |
| 111 | + : t('Value of header %s', index + 1) |
| 112 | + } |
| 113 | + /> |
| 114 | + <Button |
| 115 | + disabled={disabled} |
| 116 | + icon={<IconDelete />} |
| 117 | + size="sm" |
| 118 | + borderless |
| 119 | + aria-label={ |
| 120 | + headerName |
| 121 | + ? t('Remove %s', disambiguateHeaderName(index)) |
| 122 | + : t('Remove header %s', index + 1) |
| 123 | + } |
| 124 | + onClick={() => removeItem(index)} |
| 125 | + /> |
| 126 | + </HeaderRow> |
| 127 | + ))} |
| 128 | + </HeaderItems> |
| 129 | + )} |
| 130 | + <HeaderActions> |
| 131 | + <Button disabled={disabled} icon={<IconAdd />} size="sm" onClick={addItem}> |
| 132 | + {t('Add Header')} |
| 133 | + </Button> |
| 134 | + <FormFieldControlState model={model} name={name} /> |
| 135 | + </HeaderActions> |
| 136 | + </HeadersContainer> |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +export function UptimeHeadersField(props: Omit<FormFieldProps, 'children'>) { |
| 141 | + return ( |
| 142 | + <FormField defaultValue={[]} {...props} hideControlState flexibleControlStateSize> |
| 143 | + {({ref: _ref, ...fieldProps}) => <UptimHeadersControl {...fieldProps} />} |
| 144 | + </FormField> |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +const HeadersContainer = styled('div')` |
| 149 | + display: flex; |
| 150 | + flex-direction: column; |
| 151 | + gap: ${space(1)}; |
| 152 | +`; |
| 153 | + |
| 154 | +const HeaderActions = styled('div')` |
| 155 | + display: flex; |
| 156 | + gap: ${space(1.5)}; |
| 157 | +`; |
| 158 | + |
| 159 | +const HeaderItems = styled('fieldset')` |
| 160 | + display: grid; |
| 161 | + grid-template-columns: minmax(200px, 1fr) 2fr max-content; |
| 162 | + gap: ${space(1)}; |
| 163 | + width: 100%; |
| 164 | +`; |
| 165 | + |
| 166 | +const HeaderRow = styled('div')` |
| 167 | + display: grid; |
| 168 | + grid-template-columns: subgrid; |
| 169 | + grid-column: 1 / -1; |
| 170 | + align-items: center; |
| 171 | +`; |
0 commit comments