Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | 3x | import { useState, useEffect } from 'react';
import { useForm, Controller } from 'react-hook-form';
import { adminService, type ProviderSchema } from '@/services/admin.service';
import { Input } from '@/components/ui/input';
import { Switch } from '@/components/ui/switch';
import { Button } from '@/components/ui/button';
import {
Field,
FieldLabel,
FieldContent,
FieldError
} from '@/components/ui/field';
import { Loader2, Lock, CheckCircle2, XCircle } from 'lucide-react';
import { useToast } from '@/hooks/use-toast';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { ProviderLogo } from './ProviderLogo';
import { Badge } from '@/components/ui/badge';
import { TagInput } from '@/components/ui/tag-input';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
interface ConfigurationFormProps {
category: string;
providerName: string;
displayName: string;
logoUrl?: string;
configId?: string;
accountId?: string;
onSuccess: () => void;
onCancel: () => void;
}
export const ConfigurationForm = ({
category,
providerName,
displayName,
logoUrl,
configId,
accountId,
onSuccess,
onCancel
}: ConfigurationFormProps) => {
const [schema, setSchema] = useState<ProviderSchema | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [isTesting, setIsTesting] = useState(false);
const [testResult, setTestResult] = useState<{ success: boolean; message: string } | null>(null);
const { toast } = useToast();
const {
control,
handleSubmit,
reset,
watch,
formState: { isSubmitting, errors, isValid }
} = useForm({
mode: 'onChange'
});
const formValues = watch();
useEffect(() => {
const loadData = async () => {
setIsLoading(true);
try {
const [schemaData, initialValues] = await Promise.all([
adminService.getProviderSchema(category, providerName),
configId ? adminService.getConfigValues(configId) : Promise.resolve({})
]);
const properties = (schemaData as ProviderSchema)?.properties || {};
// Extract default values from schema
const defaultValues: Record<string, unknown> = {};
Object.entries(properties).forEach(([key, prop]) => {
if (prop.default !== undefined) {
defaultValues[key] = prop.default;
}
});
setSchema(schemaData as ProviderSchema);
reset({ ...defaultValues, ...initialValues });
} catch (error) {
console.error("Failed to load configuration data", error);
toast({
title: "Error",
description: "Failed to load provider configuration schema.",
variant: "destructive"
});
} finally {
setIsLoading(false);
}
};
loadData();
}, [category, providerName, configId, reset, toast]);
const onSubmit = async (values: Record<string, unknown>) => {
try {
if (configId) {
await adminService.updateConfigValues(configId, values);
toast({
title: "Success",
description: "Configuration updated successfully."
});
} else {
await adminService.createConfig({
category,
provider_name: providerName,
display_name: displayName,
config: values,
account_id: accountId,
enabled: true
});
toast({
title: "Success",
description: "Configuration created successfully."
});
}
onSuccess();
} catch {
toast({
title: "Error",
description: "Failed to save configuration.",
variant: "destructive"
});
}
};
const handleTestConnection = async () => {
setIsTesting(true);
setTestResult(null);
try {
// Filter out masked secrets that weren't changed if needed?
// The handleTestConnection should probably pass the actual values.
// If the user hasn't changed a secret, it will be "••••••••".
// The backend updateConfigValues handles this, but test-connection might need it too.
// However, the test-connection endpoint in backend DOES NOT have access to current DB config.
// So if it sees "••••••••", it will fail.
// THIS IS A KNOWN LIMITATION for testing EXISTING configs with secrets.
// We should warn the user or handle it.
const result = await adminService.testConnection({
category,
provider_name: providerName,
config: formValues
});
setTestResult(result);
} catch (error: unknown) {
const err = error as { response?: { data?: { detail?: string } } };
setTestResult({
success: false,
message: err.response?.data?.detail || "Connection test failed."
});
} finally {
setIsTesting(false);
}
};
if (isLoading) {
return (
<div className="flex items-center justify-center p-8">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
);
}
const properties = schema?.properties || {};
const requiredFields = schema?.required || [];
// Check if any required field is still masked or empty
const hasMaskedSecret = Object.entries(formValues).some(
([key, val]) => requiredFields.includes(key) && val === "••••••••"
);
return (
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col h-full overflow-hidden">
<div className="px-6 py-4 border-b bg-muted/30">
<div className="flex items-center gap-4">
<ProviderLogo
logoUrl={logoUrl}
providerName={providerName}
className="h-10 w-10 text-primary"
size={40}
/>
<div className="flex flex-col">
<h3 className="text-lg font-semibold leading-none">{displayName}</h3>
<div className="flex items-center gap-2 mt-1">
<span className="text-xs text-muted-foreground font-mono">{providerName}</span>
<Badge variant="secondary" className="text-[10px] h-4 px-1.5 uppercase">
{category.replace('_', ' ')}
</Badge>
</div>
</div>
</div>
</div>
<ScrollArea className="flex-1 overflow-y-auto px-6 py-4">
<div className="space-y-6">
{Object.entries(properties).map(([key, prop]) => {
const isRequired = requiredFields.includes(key);
const isSecret = prop.writeOnly || prop.format === 'password' || key.toLowerCase().includes('secret') || key.toLowerCase().includes('key');
return (
<Field key={key}>
<FieldLabel className="flex items-center gap-2">
{prop.title || key}
{isRequired && <span className="text-destructive">*</span>}
{isSecret && <Lock className="h-3 w-3 text-muted-foreground" />}
</FieldLabel>
<FieldContent>
<Controller
name={key}
control={control}
rules={{ required: isRequired }}
render={({ field }) => {
if (prop.type === 'boolean') {
return (
<div className="flex items-center space-x-2">
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
<span className="text-sm text-muted-foreground">
{field.value ? 'Enabled' : 'Disabled'}
</span>
</div>
);
}
if (prop.enum) {
return (
<Select
onValueChange={field.onChange}
value={field.value || prop.default || ""}
>
<SelectTrigger className="w-full">
<SelectValue placeholder={`Select ${prop.title || key}`} />
</SelectTrigger>
<SelectContent>
{prop.enum.map((option: string) => (
<SelectItem key={option} value={option}>
{option}
</SelectItem>
))}
</SelectContent>
</Select>
);
}
if (prop.type === 'array') {
return (
<TagInput
value={field.value || prop.default || []}
onValueChange={field.onChange}
placeholder={prop.description || "Enter values..."}
/>
);
}
if (prop.type === 'number' || prop.type === 'integer') {
return (
<Input
{...field}
type="number"
placeholder={prop.description}
/>
);
}
return (
<Input
{...field}
type={isSecret ? "password" : "text"}
placeholder={prop.description}
/>
);
}}
/>
<FieldError errors={[errors[key]]} />
</FieldContent>
</Field>
);
})}
</div>
</ScrollArea>
<div className="p-6 border-t bg-background mt-auto">
{testResult && (
<Alert variant={testResult.success ? "default" : "destructive"} className="mb-4">
{testResult.success ? (
<CheckCircle2 className="h-4 w-4" />
) : (
<XCircle className="h-4 w-4" />
)}
<AlertTitle>{testResult.success ? "Connection Successful" : "Connection Failed"}</AlertTitle>
<AlertDescription>{testResult.message}</AlertDescription>
</Alert>
)}
{hasMaskedSecret && (
<p className="text-xs text-muted-foreground mb-4 italic">
Note: Connection testing may fail if secrets are masked. Please re-enter secrets to test.
</p>
)}
<div className="flex flex-col-reverse sm:flex-row sm:justify-between gap-3">
<Button
type="button"
variant="outline"
onClick={handleTestConnection}
disabled={isTesting || isSubmitting || !isValid}
>
{isTesting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Testing...
</>
) : (
"Test Connection"
)}
</Button>
<div className="flex flex-col-reverse sm:flex-row gap-3">
<Button type="button" variant="ghost" onClick={onCancel} disabled={isSubmitting}>
Cancel
</Button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{configId ? "Save Changes" : "Create Configuration"}
</Button>
</div>
</div>
</div>
</form>
);
};
|