|
| 1 | +import { useState } from "react"; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { Input } from "@/components/ui/input"; |
| 4 | +import { Textarea } from "@/components/ui/textarea"; |
| 5 | +import { Switch } from "@/components/ui/switch"; |
| 6 | +import { Plus, Trash2, Eye, EyeOff } from "lucide-react"; |
| 7 | +import { CustomHeaders as CustomHeadersType, CustomHeader, createEmptyHeader } from "@/lib/types/customHeaders"; |
| 8 | + |
| 9 | +interface CustomHeadersProps { |
| 10 | + headers: CustomHeadersType; |
| 11 | + onChange: (headers: CustomHeadersType) => void; |
| 12 | + className?: string; |
| 13 | +} |
| 14 | + |
| 15 | +const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) => { |
| 16 | + const [isJsonMode, setIsJsonMode] = useState(false); |
| 17 | + const [jsonValue, setJsonValue] = useState(""); |
| 18 | + const [jsonError, setJsonError] = useState<string | null>(null); |
| 19 | + const [visibleValues, setVisibleValues] = useState<Set<number>>(new Set()); |
| 20 | + |
| 21 | + const updateHeader = (index: number, field: keyof CustomHeader, value: string | boolean) => { |
| 22 | + const newHeaders = [...headers]; |
| 23 | + newHeaders[index] = { ...newHeaders[index], [field]: value }; |
| 24 | + onChange(newHeaders); |
| 25 | + }; |
| 26 | + |
| 27 | + const addHeader = () => { |
| 28 | + onChange([...headers, createEmptyHeader()]); |
| 29 | + }; |
| 30 | + |
| 31 | + const removeHeader = (index: number) => { |
| 32 | + const newHeaders = headers.filter((_, i) => i !== index); |
| 33 | + onChange(newHeaders); |
| 34 | + }; |
| 35 | + |
| 36 | + const toggleValueVisibility = (index: number) => { |
| 37 | + const newVisible = new Set(visibleValues); |
| 38 | + if (newVisible.has(index)) { |
| 39 | + newVisible.delete(index); |
| 40 | + } else { |
| 41 | + newVisible.add(index); |
| 42 | + } |
| 43 | + setVisibleValues(newVisible); |
| 44 | + }; |
| 45 | + |
| 46 | + const switchToJsonMode = () => { |
| 47 | + const jsonObject: Record<string, string> = {}; |
| 48 | + headers.forEach((header) => { |
| 49 | + if (header.enabled && header.name.trim() && header.value.trim()) { |
| 50 | + jsonObject[header.name.trim()] = header.value.trim(); |
| 51 | + } |
| 52 | + }); |
| 53 | + setJsonValue(JSON.stringify(jsonObject, null, 2)); |
| 54 | + setJsonError(null); |
| 55 | + setIsJsonMode(true); |
| 56 | + }; |
| 57 | + |
| 58 | + const switchToFormMode = () => { |
| 59 | + try { |
| 60 | + const parsed = JSON.parse(jsonValue); |
| 61 | + if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { |
| 62 | + setJsonError("JSON must be an object with string key-value pairs"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const newHeaders: CustomHeadersType = Object.entries(parsed).map(([name, value]) => ({ |
| 67 | + name, |
| 68 | + value: String(value), |
| 69 | + enabled: true, |
| 70 | + })); |
| 71 | + |
| 72 | + onChange(newHeaders); |
| 73 | + setJsonError(null); |
| 74 | + setIsJsonMode(false); |
| 75 | + } catch (error) { |
| 76 | + setJsonError("Invalid JSON format"); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + const handleJsonChange = (value: string) => { |
| 81 | + setJsonValue(value); |
| 82 | + setJsonError(null); |
| 83 | + }; |
| 84 | + |
| 85 | + if (isJsonMode) { |
| 86 | + return ( |
| 87 | + <div className={`space-y-3 ${className}`}> |
| 88 | + <div className="flex justify-between items-center gap-2"> |
| 89 | + <h4 className="text-sm font-semibold flex-shrink-0">Custom Headers (JSON)</h4> |
| 90 | + <Button |
| 91 | + type="button" |
| 92 | + variant="outline" |
| 93 | + size="sm" |
| 94 | + onClick={switchToFormMode} |
| 95 | + className="flex-shrink-0" |
| 96 | + > |
| 97 | + Switch to Form |
| 98 | + </Button> |
| 99 | + </div> |
| 100 | + <div className="space-y-2"> |
| 101 | + <Textarea |
| 102 | + value={jsonValue} |
| 103 | + onChange={(e) => handleJsonChange(e.target.value)} |
| 104 | + placeholder='{\n "Authorization": "Bearer token123",\n "X-Tenant-ID": "acme-inc",\n "X-Environment": "staging"\n}' |
| 105 | + className="font-mono text-sm min-h-[100px] resize-none" |
| 106 | + /> |
| 107 | + {jsonError && ( |
| 108 | + <p className="text-sm text-red-600">{jsonError}</p> |
| 109 | + )} |
| 110 | + <p className="text-xs text-muted-foreground"> |
| 111 | + Enter headers as a JSON object with string key-value pairs. |
| 112 | + </p> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + return ( |
| 119 | + <div className={`space-y-3 ${className}`}> |
| 120 | + <div className="flex justify-between items-center gap-2"> |
| 121 | + <h4 className="text-sm font-semibold flex-shrink-0">Custom Headers</h4> |
| 122 | + <div className="flex gap-1 flex-shrink-0"> |
| 123 | + <Button |
| 124 | + type="button" |
| 125 | + variant="outline" |
| 126 | + size="sm" |
| 127 | + onClick={switchToJsonMode} |
| 128 | + className="text-xs px-2" |
| 129 | + > |
| 130 | + JSON |
| 131 | + </Button> |
| 132 | + <Button |
| 133 | + type="button" |
| 134 | + variant="outline" |
| 135 | + size="sm" |
| 136 | + onClick={addHeader} |
| 137 | + className="text-xs px-2" |
| 138 | + > |
| 139 | + <Plus className="w-3 h-3 mr-1" /> |
| 140 | + Add |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + |
| 145 | + {headers.length === 0 ? ( |
| 146 | + <div className="text-center py-4 text-muted-foreground"> |
| 147 | + <p className="text-sm">No custom headers configured</p> |
| 148 | + <p className="text-xs mt-1">Click "Add" to get started</p> |
| 149 | + </div> |
| 150 | + ) : ( |
| 151 | + <div className="space-y-2 max-h-[300px] overflow-y-auto"> |
| 152 | + {headers.map((header, index) => ( |
| 153 | + <div |
| 154 | + key={index} |
| 155 | + className="flex items-start gap-2 p-2 border rounded-md" |
| 156 | + > |
| 157 | + <Switch |
| 158 | + checked={header.enabled} |
| 159 | + onCheckedChange={(enabled) => updateHeader(index, "enabled", enabled)} |
| 160 | + className="shrink-0 mt-2" |
| 161 | + /> |
| 162 | + <div className="flex-1 min-w-0 space-y-2"> |
| 163 | + <Input |
| 164 | + placeholder="Header Name" |
| 165 | + value={header.name} |
| 166 | + onChange={(e) => updateHeader(index, "name", e.target.value)} |
| 167 | + className="font-mono text-xs" |
| 168 | + /> |
| 169 | + <div className="relative"> |
| 170 | + <Input |
| 171 | + placeholder="Header Value" |
| 172 | + value={header.value} |
| 173 | + onChange={(e) => updateHeader(index, "value", e.target.value)} |
| 174 | + type={visibleValues.has(index) ? "text" : "password"} |
| 175 | + className="font-mono text-xs pr-8" |
| 176 | + /> |
| 177 | + <Button |
| 178 | + type="button" |
| 179 | + variant="ghost" |
| 180 | + size="sm" |
| 181 | + onClick={() => toggleValueVisibility(index)} |
| 182 | + className="absolute right-1 top-1/2 -translate-y-1/2 h-6 w-6 p-0" |
| 183 | + > |
| 184 | + {visibleValues.has(index) ? ( |
| 185 | + <EyeOff className="w-3 h-3" /> |
| 186 | + ) : ( |
| 187 | + <Eye className="w-3 h-3" /> |
| 188 | + )} |
| 189 | + </Button> |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + <Button |
| 193 | + type="button" |
| 194 | + variant="ghost" |
| 195 | + size="sm" |
| 196 | + onClick={() => removeHeader(index)} |
| 197 | + className="shrink-0 text-red-600 hover:text-red-700 hover:bg-red-50 h-6 w-6 p-0 mt-2" |
| 198 | + > |
| 199 | + <Trash2 className="w-3 h-3" /> |
| 200 | + </Button> |
| 201 | + </div> |
| 202 | + ))} |
| 203 | + </div> |
| 204 | + )} |
| 205 | + |
| 206 | + {headers.length > 0 && ( |
| 207 | + <p className="text-xs text-muted-foreground"> |
| 208 | + Use the toggle to enable/disable headers. Only enabled headers with both name and value will be sent. |
| 209 | + </p> |
| 210 | + )} |
| 211 | + </div> |
| 212 | + ); |
| 213 | +}; |
| 214 | + |
| 215 | +export default CustomHeaders; |
0 commit comments