|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { Plus, Trash2 } from 'lucide-react'; |
| 3 | + |
| 4 | +import { |
| 5 | + formatGatewayHeaderEntries, |
| 6 | + parseGatewayHeaderEntries, |
| 7 | + type GatewayHeaderEntry, |
| 8 | +} from '@shared/gateway-headers'; |
| 9 | + |
| 10 | +import { Button } from '@/components/ui/button'; |
| 11 | +import { Input } from '@/components/ui/input'; |
| 12 | +import { useI18n } from './i18n'; |
| 13 | + |
| 14 | +type GatewayHeaderRow = GatewayHeaderEntry & { |
| 15 | + id: string; |
| 16 | +}; |
| 17 | + |
| 18 | +type GatewayHeadersEditorProps = { |
| 19 | + value: string; |
| 20 | + onChange: (value: string) => void; |
| 21 | + className?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +let nextHeaderRowId = 0; |
| 25 | + |
| 26 | +function createHeaderRow(entry?: Partial<GatewayHeaderEntry>): GatewayHeaderRow { |
| 27 | + nextHeaderRowId += 1; |
| 28 | + return { |
| 29 | + id: `gateway-header-${nextHeaderRowId}`, |
| 30 | + name: entry?.name ?? '', |
| 31 | + value: entry?.value ?? '', |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function rowsFromValue(value: string): GatewayHeaderRow[] { |
| 36 | + const rows = parseGatewayHeaderEntries(value).map((entry) => createHeaderRow(entry)); |
| 37 | + return rows.length > 0 ? rows : [createHeaderRow()]; |
| 38 | +} |
| 39 | + |
| 40 | +function rowsToValue(rows: readonly GatewayHeaderRow[]): string { |
| 41 | + return formatGatewayHeaderEntries(rows); |
| 42 | +} |
| 43 | + |
| 44 | +export function GatewayHeadersEditor({ |
| 45 | + value, |
| 46 | + onChange, |
| 47 | + className, |
| 48 | +}: GatewayHeadersEditorProps) { |
| 49 | + const { t } = useI18n(); |
| 50 | + const lastValueRef = useRef(value); |
| 51 | + const [rows, setRows] = useState<GatewayHeaderRow[]>(() => rowsFromValue(value)); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (value === lastValueRef.current) { |
| 55 | + return; |
| 56 | + } |
| 57 | + lastValueRef.current = value; |
| 58 | + setRows(rowsFromValue(value)); |
| 59 | + }, [value]); |
| 60 | + |
| 61 | + function emit(nextRows: GatewayHeaderRow[]) { |
| 62 | + setRows(nextRows); |
| 63 | + const nextValue = rowsToValue(nextRows); |
| 64 | + lastValueRef.current = nextValue; |
| 65 | + onChange(nextValue); |
| 66 | + } |
| 67 | + |
| 68 | + function updateRow(rowId: string, field: 'name' | 'value', nextValue: string) { |
| 69 | + emit(rows.map((row) => (row.id === rowId ? { ...row, [field]: nextValue } : row))); |
| 70 | + } |
| 71 | + |
| 72 | + function removeRow(rowId: string) { |
| 73 | + const nextRows = rows.filter((row) => row.id !== rowId); |
| 74 | + emit(nextRows.length > 0 ? nextRows : [createHeaderRow()]); |
| 75 | + } |
| 76 | + |
| 77 | + function addRow() { |
| 78 | + setRows((current) => [...current, createHeaderRow()]); |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <div className={['gateway-headers-editor', className].filter(Boolean).join(' ')}> |
| 83 | + <div className="gateway-headers-editor-list"> |
| 84 | + {rows.map((row) => ( |
| 85 | + <div className="gateway-headers-editor-row" key={row.id}> |
| 86 | + <Input |
| 87 | + autoCapitalize="off" |
| 88 | + autoComplete="off" |
| 89 | + aria-label={t('Header name')} |
| 90 | + className="gateway-headers-editor-input" |
| 91 | + placeholder={t('Header name')} |
| 92 | + spellCheck={false} |
| 93 | + type="text" |
| 94 | + value={row.name} |
| 95 | + onChange={(event) => updateRow(row.id, 'name', event.target.value)} |
| 96 | + /> |
| 97 | + <Input |
| 98 | + autoCapitalize="off" |
| 99 | + autoComplete="off" |
| 100 | + aria-label={t('Header value')} |
| 101 | + className="gateway-headers-editor-input" |
| 102 | + placeholder={t('Header value')} |
| 103 | + spellCheck={false} |
| 104 | + type="text" |
| 105 | + value={row.value} |
| 106 | + onChange={(event) => updateRow(row.id, 'value', event.target.value)} |
| 107 | + /> |
| 108 | + <Button |
| 109 | + aria-label={t('Remove header')} |
| 110 | + className="gateway-headers-editor-remove" |
| 111 | + disabled={rows.length === 1 && !row.name.trim() && !row.value.trim()} |
| 112 | + onClick={() => removeRow(row.id)} |
| 113 | + size="icon-sm" |
| 114 | + type="button" |
| 115 | + variant="ghost" |
| 116 | + > |
| 117 | + <Trash2 aria-hidden /> |
| 118 | + </Button> |
| 119 | + </div> |
| 120 | + ))} |
| 121 | + </div> |
| 122 | + <Button |
| 123 | + className="gateway-headers-editor-add" |
| 124 | + onClick={addRow} |
| 125 | + size="sm" |
| 126 | + type="button" |
| 127 | + variant="outline" |
| 128 | + > |
| 129 | + <Plus aria-hidden /> |
| 130 | + {t('Add header')} |
| 131 | + </Button> |
| 132 | + </div> |
| 133 | + ); |
| 134 | +} |
0 commit comments