import { useState, useEffect } from 'react' import { X, Upload } from 'lucide-react' import TagInput from './TagInput' const DEFAULTS = { id: '', name: '', host: '', port: '8001', api_key: '', description: '', tags: [] } const FIELDS = [ { key: 'name', label: 'Nom affiché', placeholder: 'Mon VPS 1', required: true, type: 'text' }, { key: 'id', label: 'Identifiant unique', placeholder: 'vps-1', required: true, type: 'text' }, { key: 'host', label: 'IP ou hostname', placeholder: '192.168.1.10', required: true, type: 'text' }, { key: 'port', label: 'Port agent', placeholder: '8001', required: true, type: 'number' }, { key: 'api_key', label: 'Clé API agent', placeholder: '••••••••', required: true, type: 'password' }, { key: 'description', label: 'Description', placeholder: 'Optionnel', required: false, type: 'text' }, ] export default function AddVpsModal({ onSave, onClose }) { const [mode, setMode] = useState('manual') // 'manual' | 'import' const [form, setForm] = useState(DEFAULTS) const [saving, setSaving] = useState(false) const [error, setError] = useState('') const [json, setJson] = useState('') const [jsonError, setJsonError] = useState('') useEffect(() => { const handler = (e) => { if (e.key === 'Escape') onClose() } window.addEventListener('keydown', handler) return () => window.removeEventListener('keydown', handler) }, [onClose]) const set = (key) => (e) => setForm(f => ({ ...f, [key]: e.target.value })) const handleImportJson = () => { setJsonError('') try { const parsed = JSON.parse(json.trim()) const required = ['id', 'name', 'host', 'port', 'api_key'] const missing = required.filter(k => !parsed[k]) if (missing.length > 0) { setJsonError(`Champs manquants : ${missing.join(', ')}`) return } setForm({ id: String(parsed.id), name: String(parsed.name), host: String(parsed.host), port: String(parsed.port), api_key: String(parsed.api_key), description: String(parsed.description ?? ''), tags: Array.isArray(parsed.tags) ? parsed.tags : [], }) setMode('manual') } catch { setJsonError('JSON invalide. Assurez-vous d\'avoir collé la config exportée.') } } const handleSubmit = async (e) => { e.preventDefault() setSaving(true) setError('') try { await onSave({ ...form, port: parseInt(form.port, 10) }) } catch (err) { setError(err.message) setSaving(false) } } return (
{ if (e.target === e.currentTarget) onClose() }} >

Ajouter un VPS

{mode === 'import' ? (

Collez le JSON copié via le bouton Exporter d'une autre instance.