import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Switch } from "@/components/ui/switch"; import { Plus, Trash2, Eye, EyeOff } from "lucide-react"; import { CustomHeaders as CustomHeadersType, CustomHeader, createEmptyHeader, } from "@/lib/types/customHeaders"; interface CustomHeadersProps { headers: CustomHeadersType; onChange: (headers: CustomHeadersType) => void; className?: string; } const CustomHeaders = ({ headers, onChange, className, }: CustomHeadersProps) => { const [isJsonMode, setIsJsonMode] = useState(false); const [jsonValue, setJsonValue] = useState(""); const [jsonError, setJsonError] = useState(null); const [visibleValues, setVisibleValues] = useState>(new Set()); const updateHeader = ( index: number, field: keyof CustomHeader, value: string | boolean, ) => { const newHeaders = [...headers]; newHeaders[index] = { ...newHeaders[index], [field]: value }; onChange(newHeaders); }; const addHeader = () => { onChange([...headers, createEmptyHeader()]); }; const removeHeader = (index: number) => { const newHeaders = headers.filter((_, i) => i !== index); onChange(newHeaders); }; const toggleValueVisibility = (index: number) => { const newVisible = new Set(visibleValues); if (newVisible.has(index)) { newVisible.delete(index); } else { newVisible.add(index); } setVisibleValues(newVisible); }; const switchToJsonMode = () => { const jsonObject: Record = {}; headers.forEach((header) => { if (header.enabled && header.name.trim() && header.value.trim()) { jsonObject[header.name.trim()] = header.value.trim(); } }); setJsonValue(JSON.stringify(jsonObject, null, 2)); setJsonError(null); setIsJsonMode(true); }; const switchToFormMode = () => { try { const parsed = JSON.parse(jsonValue); if ( typeof parsed !== "object" || parsed === null || Array.isArray(parsed) ) { setJsonError("JSON must be an object with string key-value pairs"); return; } const newHeaders: CustomHeadersType = Object.entries(parsed).map( ([name, value]) => ({ name, value: String(value), enabled: true, }), ); onChange(newHeaders); setJsonError(null); setIsJsonMode(false); } catch { setJsonError("Invalid JSON format"); } }; const handleJsonChange = (value: string) => { setJsonValue(value); setJsonError(null); }; if (isJsonMode) { return (

Custom Headers (JSON)