|
| 1 | +import { type FormEvent, useState, useEffect } from 'react' |
| 2 | +import type { |
| 3 | + ValueType, |
| 4 | + EntityType, |
| 5 | + AttributeDefinition, |
| 6 | +} from '../types/attributeDefinition' |
| 7 | + |
| 8 | +export interface AttributeDefinitionFormValues { |
| 9 | + key: string |
| 10 | + entity_type: EntityType |
| 11 | + display_name: string |
| 12 | + value_type: ValueType |
| 13 | + default_value: string |
| 14 | + allowed_values: string[] | undefined |
| 15 | + description: string |
| 16 | +} |
| 17 | + |
| 18 | +interface Props { |
| 19 | + mode: 'create' | 'edit' |
| 20 | + initial?: AttributeDefinition |
| 21 | + onSubmit: (values: AttributeDefinitionFormValues) => Promise<void> |
| 22 | + onCancel: () => void |
| 23 | + submitLabel: string |
| 24 | + isSubmitting: boolean |
| 25 | + error?: string | null |
| 26 | +} |
| 27 | + |
| 28 | +const VALUE_TYPES: ValueType[] = ['string', 'integer', 'boolean', 'list'] |
| 29 | +const ENTITY_TYPES: EntityType[] = ['user', 'table', 'column'] |
| 30 | + |
| 31 | +const inputCls = |
| 32 | + 'w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500' |
| 33 | + |
| 34 | +export function AttributeDefinitionForm({ |
| 35 | + mode, |
| 36 | + initial, |
| 37 | + onSubmit, |
| 38 | + onCancel, |
| 39 | + submitLabel, |
| 40 | + isSubmitting, |
| 41 | + error, |
| 42 | +}: Props) { |
| 43 | + const [key, setKey] = useState('') |
| 44 | + const [entityType, setEntityType] = useState<EntityType>('user') |
| 45 | + const [displayName, setDisplayName] = useState('') |
| 46 | + const [valueType, setValueType] = useState<ValueType>('string') |
| 47 | + const [defaultValue, setDefaultValue] = useState('') |
| 48 | + const [allowedValuesText, setAllowedValuesText] = useState('') |
| 49 | + const [description, setDescription] = useState('') |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + if (initial) { |
| 53 | + setKey(initial.key) |
| 54 | + setEntityType(initial.entity_type) |
| 55 | + setDisplayName(initial.display_name) |
| 56 | + setValueType(initial.value_type) |
| 57 | + setDefaultValue(initial.default_value ?? '') |
| 58 | + setAllowedValuesText(initial.allowed_values?.join(', ') ?? '') |
| 59 | + setDescription(initial.description ?? '') |
| 60 | + } |
| 61 | + }, [initial]) |
| 62 | + |
| 63 | + async function handleSubmit(e: FormEvent) { |
| 64 | + e.preventDefault() |
| 65 | + const allowedValues = allowedValuesText.trim() |
| 66 | + ? allowedValuesText |
| 67 | + .split(',') |
| 68 | + .map((v) => v.trim()) |
| 69 | + .filter(Boolean) |
| 70 | + : undefined |
| 71 | + |
| 72 | + await onSubmit({ |
| 73 | + key, |
| 74 | + entity_type: entityType, |
| 75 | + display_name: displayName, |
| 76 | + value_type: valueType, |
| 77 | + default_value: defaultValue, |
| 78 | + allowed_values: allowedValues, |
| 79 | + description, |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + return ( |
| 84 | + <form onSubmit={handleSubmit} className="space-y-5 max-w-lg"> |
| 85 | + {mode === 'create' && ( |
| 86 | + <> |
| 87 | + <div> |
| 88 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 89 | + Key <span className="text-red-500">*</span> |
| 90 | + </label> |
| 91 | + <input |
| 92 | + type="text" |
| 93 | + value={key} |
| 94 | + onChange={(e) => setKey(e.target.value)} |
| 95 | + placeholder="e.g., region, clearance_level" |
| 96 | + required |
| 97 | + pattern="[a-zA-Z][a-zA-Z0-9_]*" |
| 98 | + maxLength={64} |
| 99 | + className={`${inputCls} font-mono`} |
| 100 | + /> |
| 101 | + <p className="text-xs text-gray-400 mt-1"> |
| 102 | + Letters, digits, underscores. Used as {'{'}user.key{'}'} in expressions. |
| 103 | + </p> |
| 104 | + </div> |
| 105 | + <div> |
| 106 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 107 | + Entity type <span className="text-red-500">*</span> |
| 108 | + </label> |
| 109 | + <select |
| 110 | + value={entityType} |
| 111 | + onChange={(e) => setEntityType(e.target.value as EntityType)} |
| 112 | + className={inputCls} |
| 113 | + > |
| 114 | + {ENTITY_TYPES.map((t) => ( |
| 115 | + <option key={t} value={t}> |
| 116 | + {t} |
| 117 | + </option> |
| 118 | + ))} |
| 119 | + </select> |
| 120 | + </div> |
| 121 | + </> |
| 122 | + )} |
| 123 | + |
| 124 | + <div> |
| 125 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 126 | + Display name <span className="text-red-500">*</span> |
| 127 | + </label> |
| 128 | + <input |
| 129 | + type="text" |
| 130 | + value={displayName} |
| 131 | + onChange={(e) => setDisplayName(e.target.value)} |
| 132 | + placeholder="e.g., Region, Clearance Level" |
| 133 | + required |
| 134 | + className={inputCls} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + |
| 138 | + <div> |
| 139 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 140 | + Value type <span className="text-red-500">*</span> |
| 141 | + </label> |
| 142 | + <select |
| 143 | + value={valueType} |
| 144 | + onChange={(e) => setValueType(e.target.value as ValueType)} |
| 145 | + className={inputCls} |
| 146 | + > |
| 147 | + {VALUE_TYPES.map((t) => ( |
| 148 | + <option key={t} value={t}> |
| 149 | + {t === 'list' ? 'list (multiple strings)' : t} |
| 150 | + </option> |
| 151 | + ))} |
| 152 | + </select> |
| 153 | + {valueType === 'list' && ( |
| 154 | + <p className="text-xs text-gray-400 mt-1"> |
| 155 | + List attributes store multiple string values. Use with <code className="bg-gray-100 px-1 rounded">IN {'{'}user.key{'}'}</code> in filter expressions. |
| 156 | + </p> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + |
| 160 | + <div> |
| 161 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 162 | + Allowed values |
| 163 | + </label> |
| 164 | + <input |
| 165 | + type="text" |
| 166 | + value={allowedValuesText} |
| 167 | + onChange={(e) => setAllowedValuesText(e.target.value)} |
| 168 | + placeholder="Comma-separated, e.g., us-east, eu-west, ap-south" |
| 169 | + className={inputCls} |
| 170 | + /> |
| 171 | + <p className="text-xs text-gray-400 mt-1"> |
| 172 | + {valueType === 'list' |
| 173 | + ? 'Constrains which strings can appear as elements in the list.' |
| 174 | + : 'Leave empty to allow any value of the selected type.'} |
| 175 | + </p> |
| 176 | + </div> |
| 177 | + |
| 178 | + <div> |
| 179 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 180 | + Default value |
| 181 | + </label> |
| 182 | + <input |
| 183 | + type="text" |
| 184 | + value={defaultValue} |
| 185 | + onChange={(e) => setDefaultValue(e.target.value)} |
| 186 | + placeholder={valueType === 'list' ? 'JSON array, e.g., ["default"]' : 'Optional'} |
| 187 | + className={inputCls} |
| 188 | + /> |
| 189 | + </div> |
| 190 | + |
| 191 | + <div> |
| 192 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 193 | + Description <span className="text-gray-400 font-normal">(optional)</span> |
| 194 | + </label> |
| 195 | + <textarea |
| 196 | + value={description} |
| 197 | + onChange={(e) => setDescription(e.target.value)} |
| 198 | + placeholder="Optional description" |
| 199 | + rows={2} |
| 200 | + className={inputCls} |
| 201 | + /> |
| 202 | + </div> |
| 203 | + |
| 204 | + {error && ( |
| 205 | + <div className="bg-red-50 border border-red-200 text-red-700 text-sm rounded-lg px-4 py-3"> |
| 206 | + {error} |
| 207 | + </div> |
| 208 | + )} |
| 209 | + |
| 210 | + <div className="flex gap-3 pt-2"> |
| 211 | + <button |
| 212 | + type="submit" |
| 213 | + disabled={isSubmitting} |
| 214 | + className="bg-blue-600 hover:bg-blue-700 disabled:opacity-60 text-white font-medium rounded-lg px-5 py-2 text-sm transition-colors" |
| 215 | + > |
| 216 | + {isSubmitting ? 'Saving...' : submitLabel} |
| 217 | + </button> |
| 218 | + <button |
| 219 | + type="button" |
| 220 | + onClick={onCancel} |
| 221 | + className="text-gray-600 hover:text-gray-900 font-medium text-sm px-3 py-2 transition-colors" |
| 222 | + > |
| 223 | + Cancel |
| 224 | + </button> |
| 225 | + </div> |
| 226 | + </form> |
| 227 | + ) |
| 228 | +} |
0 commit comments