1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
import { useState } from "react";
import { Modal, Pressable, ScrollView, View } from "react-native";
import { KeyboardAwareScrollView } from "react-native-keyboard-controller";
import { Plus, Trash2, X } from "lucide-react-native";
import { useColorScheme } from "nativewind";
import { Button } from "./ui/Button";
import { Input } from "./ui/Input";
import { Text } from "./ui/Text";
interface CustomHeadersModalProps {
visible: boolean;
customHeaders: Record<string, string>;
onClose: () => void;
onSave: (headers: Record<string, string>) => void;
}
export function CustomHeadersModal({
visible,
customHeaders,
onClose,
onSave,
}: CustomHeadersModalProps) {
const { colorScheme } = useColorScheme();
const iconColor = colorScheme === "dark" ? "#d1d5db" : "#374151";
// Convert headers object to array of entries for easier manipulation
const [headers, setHeaders] = useState<{ key: string; value: string }[]>(
Object.entries(customHeaders).map(([key, value]) => ({ key, value })),
);
const [newHeaderKey, setNewHeaderKey] = useState("");
const [newHeaderValue, setNewHeaderValue] = useState("");
const handleAddHeader = () => {
if (!newHeaderKey.trim() || !newHeaderValue.trim()) {
return;
}
// Check if header already exists
const existingIndex = headers.findIndex((h) => h.key === newHeaderKey);
if (existingIndex >= 0) {
// Update existing header
const updatedHeaders = [...headers];
updatedHeaders[existingIndex].value = newHeaderValue;
setHeaders(updatedHeaders);
} else {
// Add new header
setHeaders([...headers, { key: newHeaderKey, value: newHeaderValue }]);
}
setNewHeaderKey("");
setNewHeaderValue("");
};
const handleRemoveHeader = (index: number) => {
setHeaders(headers.filter((_, i) => i !== index));
};
const handleSave = () => {
// Convert array back to object
const headersObject = headers.reduce(
(acc, { key, value }) => {
if (key.trim() && value.trim()) {
acc[key] = value;
}
return acc;
},
{} as Record<string, string>,
);
onSave(headersObject);
onClose();
};
const handleCancel = () => {
// Reset to original headers
setHeaders(
Object.entries(customHeaders).map(([key, value]) => ({ key, value })),
);
setNewHeaderKey("");
setNewHeaderValue("");
onClose();
};
return (
<Modal
visible={visible}
transparent
animationType="slide"
onRequestClose={handleCancel}
>
<View className="flex-1 justify-end">
<Pressable
className="absolute inset-0 bg-black/50"
onPress={handleCancel}
/>
<View className="max-h-[85%] rounded-t-3xl bg-card">
<KeyboardAwareScrollView
contentContainerClassName="p-6"
bottomOffset={20}
keyboardShouldPersistTaps="handled"
>
{/* Header */}
<View className="mb-4 flex flex-row items-center justify-between">
<Text className="text-lg font-semibold">Custom Headers</Text>
<Pressable onPress={handleCancel} className="p-2">
<X size={24} color={iconColor} />
</Pressable>
</View>
<Text className="mb-4 text-sm text-gray-600 dark:text-gray-400">
Add custom HTTP headers that will be sent with every API request.
</Text>
{/* Existing Headers List */}
<View className="mb-4 max-h-64">
{headers.length === 0 ? (
<Text className="py-4 text-center text-sm text-gray-500 dark:text-gray-400">
No custom headers configured
</Text>
) : (
<ScrollView>
{headers.map((header, index) => (
<View
key={index}
className="mb-2 flex-row items-center gap-2 rounded-lg border border-border bg-background p-3"
>
<View className="flex-1">
<Text className="text-sm font-semibold">
{header.key}
</Text>
<Text
className="text-xs text-gray-600 dark:text-gray-400"
numberOfLines={1}
>
{header.value}
</Text>
</View>
<Pressable
onPress={() => handleRemoveHeader(index)}
className="p-2"
>
<Trash2 size={18} color="#ef4444" />
</Pressable>
</View>
))}
</ScrollView>
)}
</View>
{/* Add New Header */}
<View className="gap-2 border-t border-border pt-4">
<Text className="text-sm font-semibold">Add New Header</Text>
<Input
placeholder="Header Name (e.g., X-Custom-Header)"
value={newHeaderKey}
onChangeText={setNewHeaderKey}
autoCapitalize="none"
inputClasses="bg-background"
/>
<Input
placeholder="Header Value"
value={newHeaderValue}
onChangeText={setNewHeaderValue}
autoCapitalize="none"
inputClasses="bg-background"
/>
<Button
variant="secondary"
onPress={handleAddHeader}
disabled={!newHeaderKey.trim() || !newHeaderValue.trim()}
>
<Plus size={16} color={iconColor} />
<Text className="text-sm">Add Header</Text>
</Button>
</View>
{/* Action Buttons */}
<View className="mt-4 flex flex-row gap-2 border-t border-border pt-4">
<Button
variant="secondary"
onPress={handleCancel}
androidRootClassName="flex-1"
>
<Text>Cancel</Text>
</Button>
<Button
variant="primary"
onPress={handleSave}
androidRootClassName="flex-1"
>
<Text>Save</Text>
</Button>
</View>
</KeyboardAwareScrollView>
</View>
</View>
</Modal>
);
}
|