Spaces:
Sleeping
Sleeping
File size: 13,672 Bytes
7b0525c 5a24a1a 7b0525c b52b399 7b0525c 92fa1bd 7b0525c 92fa1bd 7b0525c f3f5cf0 5a24a1a 92fa1bd 7b0525c 92fa1bd 7b0525c 92fa1bd 7b0525c 92fa1bd 5a24a1a 92fa1bd f3f5cf0 92fa1bd 7b0525c 92a4c44 7b0525c c0f0601 7b0525c c0f0601 7b0525c c0f0601 7b0525c c0f0601 7b0525c 5a24a1a 9869286 5a24a1a 7b0525c f3f5cf0 7b0525c 92fa1bd 7b0525c 5683f0b | 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 | import React, { useState } from 'react';
import { DataItem, LinkGroup } from '../types';
import { Button } from './Button';
import { IconPicker } from './IconPicker';
import { Save, X, Link as LinkIcon, Download, Hash, Sparkles } from 'lucide-react';
interface DataFormProps {
initialData?: DataItem | null;
onSave: (data: Omit<DataItem, 'id' | 'createdAt'>) => void | Promise<void>;
onCancel: () => void;
}
export const DataForm: React.FC<DataFormProps> = ({ initialData, onSave, onCancel }) => {
const [title, setTitle] = useState(initialData?.title || '');
const [icon, setIcon] = useState(initialData?.icon || 'layers');
const [summary, setSummary] = useState(initialData?.summary || '');
const [downloadUrl, setDownloadUrl] = useState(initialData?.downloadUrl || '');
const [visitUrl, setVisitUrl] = useState(initialData?.visitUrl || '');
const [tagsString, setTagsString] = useState(initialData?.tags.join(', ') || '');
const [categories, setCategories] = useState<string[]>(initialData?.categories || []);
const [categoryInput, setCategoryInput] = useState('');
const [linkGroups, setLinkGroups] = useState<LinkGroup[]>(initialData?.linkGroups || []);
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (isSubmitting) return;
setIsSubmitting(true);
try {
await onSave({
icon: icon || 'layers',
title,
content: "", // Legacy
summary,
downloadUrl: downloadUrl || undefined,
visitUrl: visitUrl || undefined,
linkGroups,
tags: tagsString.split(',').map(t => t.trim()).filter(Boolean),
categories: categories,
});
} finally {
setIsSubmitting(false);
}
};
return (
<div className="max-w-4xl mx-auto animate-fade-in pb-20">
<div className="flex items-center justify-between mb-8">
<h2 className="text-2xl font-bold text-white">
{initialData ? 'Editar Entrada' : 'Crear Nueva Entrada'}
</h2>
<Button variant="ghost" onClick={onCancel} className="text-gray-500 hover:text-white">
<X size={24} />
</Button>
</div>
<form onSubmit={handleSubmit} className="space-y-8">
{/* MAIN INFO CARD */}
<div className="bg-surface p-8 rounded-2xl border border-white/5 shadow-xl space-y-6">
<div>
<label className="block text-sm font-medium text-gray-400 mb-2">Título</label>
<div className="flex gap-4 items-start">
<IconPicker
selectedIcon={icon}
onSelect={setIcon}
/>
<input
required
type="text"
className="w-full bg-background border border-white/10 rounded-lg p-4 text-white text-lg focus:border-primary focus:ring-1 focus:ring-primary outline-none transition-colors"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Ej: Apuntes de React..."
/>
</div>
</div>
<div>
<div className="flex justify-between items-center mb-2">
<label className="block text-sm font-medium text-gray-400">Resumen / Descripción Principal</label>
</div>
<textarea
rows={4}
className="w-full bg-background border border-white/10 rounded-lg p-4 text-white focus:border-primary focus:ring-1 focus:ring-primary outline-none transition-colors resize-y"
value={summary}
onChange={(e) => setSummary(e.target.value)}
placeholder="Descripción principal de la tarjeta..."
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-6 bg-background/30 rounded-xl border border-white/5">
<div>
<label className="flex items-center gap-2 text-sm font-medium text-primary mb-2">
<LinkIcon size={16} /> Enlace de Visita
</label>
<input
type="url"
className="w-full bg-surface border border-white/10 rounded-lg p-3 text-white focus:border-primary outline-none text-sm placeholder-gray-600"
value={visitUrl}
onChange={(e) => setVisitUrl(e.target.value)}
placeholder="https://mi-sitio-web.com"
/>
</div>
<div>
<label className="flex items-center gap-2 text-sm font-medium text-primary mb-2">
<Download size={16} /> Enlace de Descarga
</label>
<input
type="url"
className="w-full bg-surface border border-white/10 rounded-lg p-3 text-white focus:border-primary outline-none text-sm placeholder-gray-600"
value={downloadUrl}
onChange={(e) => setDownloadUrl(e.target.value)}
placeholder="https://drive.google.com/..."
/>
</div>
</div>
{/* LINK GROUPS */}
<div className="space-y-4">
<div className="flex items-center justify-between">
<label className="block text-sm font-medium text-gray-400">Grupos de Enlaces Extra</label>
<button
type="button"
onClick={() => setLinkGroups([...linkGroups, { id: Date.now().toString(), title: `Opción ${linkGroups.length + 1}` }])}
className="text-xs text-primary hover:text-primary/80 flex items-center gap-1"
>
+ Añadir Enlace Extra
</button>
</div>
{linkGroups.map((group) => (
<div key={group.id} className="p-4 bg-background/50 rounded-xl border border-white/5 space-y-4 relative group">
<button
type="button"
onClick={() => setLinkGroups(linkGroups.filter(g => g.id !== group.id))}
className="absolute top-2 right-2 p-1 text-gray-500 hover:text-red-400 opacity-0 group-hover:opacity-100 transition-opacity"
title="Eliminar grupo"
>
<X size={16} />
</button>
<div>
<input
type="text"
value={group.title}
onChange={e => setLinkGroups(linkGroups.map(g => g.id === group.id ? { ...g, title: e.target.value } : g))}
placeholder="Título del grupo de enlaces..."
className="w-full bg-transparent border-b border-white/10 p-2 text-white outline-none focus:border-primary text-sm font-medium mb-2"
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label className="flex items-center gap-2 text-xs text-primary mb-1">
<LinkIcon size={12} /> URL Visitar
</label>
<input
type="url"
value={group.visitUrl || ''}
onChange={e => setLinkGroups(linkGroups.map(g => g.id === group.id ? { ...g, visitUrl: e.target.value } : g))}
placeholder="https://..."
className="w-full bg-surface border border-white/10 rounded p-2 text-white focus:border-primary outline-none text-xs"
/>
</div>
<div>
<label className="flex items-center gap-2 text-xs text-primary mb-1">
<Download size={12} /> URL Descargar
</label>
<input
type="url"
value={group.downloadUrl || ''}
onChange={e => setLinkGroups(linkGroups.map(g => g.id === group.id ? { ...g, downloadUrl: e.target.value } : g))}
placeholder="https://..."
className="w-full bg-surface border border-white/10 rounded p-2 text-white focus:border-primary outline-none text-xs"
/>
</div>
</div>
</div>
))}
</div>
<div>
<div>
<label className="block text-sm font-medium text-gray-400 mb-2">Categorías</label>
</div>
<div className="mb-6">
<div className="flex flex-wrap gap-2 mb-2">
{categories.map((cat, idx) => (
<span key={idx} className="flex items-center gap-1 text-sm text-primary bg-primary/10 px-3 py-1 rounded-lg border border-primary/30 uppercase tracking-wide font-medium">
{cat}
<button type="button" onClick={() => setCategories(categories.filter((_, i) => i !== idx))} className="ml-1 hover:text-red-400">
<X size={12} />
</button>
</span>
))}
</div>
<div className="relative">
<Sparkles size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-primary" />
<input
type="text"
className="w-full bg-background border border-white/10 rounded-lg p-3 pl-10 text-white focus:border-primary outline-none"
value={categoryInput}
onChange={(e) => setCategoryInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
const val = categoryInput.trim();
if (val && !categories.includes(val)) {
setCategories([...categories, val]);
setCategoryInput('');
}
}
}}
placeholder="Escribe una categoría y presiona Enter..."
/>
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-400 mb-2">Etiquetas</label>
</div>
<div className="relative">
<Hash size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500" />
<input
type="text"
className="w-full bg-background border border-white/10 rounded-lg p-3 pl-10 text-white focus:border-primary outline-none"
value={tagsString}
onChange={(e) => setTagsString(e.target.value)}
placeholder="etiqueta1, etiqueta2..."
/>
</div>
<div className="flex justify-end mt-2">
<button
type="button"
onClick={() => {
const stopWords = new Set([
'el', 'la', 'los', 'las', 'un', 'una', 'unos', 'unas', 'y', 'o', 'pero', 'si', 'no',
'de', 'del', 'a', 'al', 'en', 'con', 'por', 'para', 'sin', 'sobre', 'entre',
'mi', 'tu', 'su', 'nuestro', 'vuestro', 'sus', 'mis', 'tus',
'que', 'cual', 'quien', 'donde', 'cuando', 'como', 'porque',
'es', 'son', 'fue', 'fueron', 'era', 'eran', 'está', 'están',
'este', 'esta', 'ese', 'esa', 'aquel', 'aquella', 'esto', 'eso', 'aquello',
'hizo', 'hacer', 'todo', 'toda', 'todos', 'todas', 'muy', 'más', 'tan',
'ya', 'hoy', 'ayer', 'ahora', 'después', 'antes', 'mira', 'voy'
]);
const text = `${title} ${summary}`.toLowerCase();
// RegEx to remove special chars, keep accents/letters/numbers
const words = text.replace(/[^\w\sáéíóúñü]/g, '').split(/\s+/);
const uniqueKeywords = new Set<string>();
words.forEach(word => {
if (word.length > 2 && !stopWords.has(word)) {
uniqueKeywords.add(word);
}
});
// Limit to 15 tags
const keywordsArray = Array.from(uniqueKeywords).slice(0, 15);
setTagsString(keywordsArray.join(', '));
}}
className="text-xs text-primary hover:text-primary/80 transition-colors flex items-center gap-1 bg-primary/10 px-3 py-1.5 rounded-lg border border-primary/20"
title="Extraer palabras clave automáticamente"
disabled={!title && !summary}
>
<Sparkles size={14} /> Generar Automáticamente
</button>
</div>
</div>
</div>
{/* ACTION BUTTONS */}
<div className="flex gap-4 pt-6 border-t border-white/5 sticky bottom-0 bg-[#121212] py-6 z-10">
<Button type="button" variant="ghost" onClick={onCancel} className="flex-1">
Cancelar
</Button>
<Button type="submit" className="flex-1 py-3 text-lg shadow-[0_0_20px_rgba(187,134,252,0.2)]" disabled={isSubmitting}>
<Save size={20} /> {isSubmitting ? 'Guardando...' : 'Guardar Cambios'}
</Button>
</div>
</form>
</div>
);
}; |