/** * Форма создания/редактирования персоны * Позволяет пользователю создавать новых персон или редактировать существующих */ import { Tag as TagIcon, Upload, User, X } from "lucide-react" import { useRef, useState } from "react" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import type { PersonProfile } from "../types/person" interface PersonFormProps { person?: PersonProfile isOpen: boolean onClose: () => void onSave: (personData: Partial) => Promise isLoading?: boolean } export function PersonForm({ person, isOpen, onClose, onSave, isLoading = false }: PersonFormProps) { const [formData, setFormData] = useState({ name: person?.name || "", notes: person?.notes || "", tags: person?.tags || [], thumbnails: person?.thumbnails || [], }) const [newTag, setNewTag] = useState("") const [thumbnailPreview, setThumbnailPreview] = useState( person?.thumbnails?.find((t) => t.isPrimary)?.imageUrl || person?.thumbnails?.[0]?.imageUrl || null, ) const fileInputRef = useRef(null) const handleInputChange = (field: string, value: string) => { setFormData((prev) => ({ ...prev, [field]: value, })) } const handleAddTag = () => { if (newTag.trim() && !formData.tags.includes(newTag.trim())) { setFormData((prev) => ({ ...prev, tags: [...prev.tags, newTag.trim()], })) setNewTag("") } } const handleRemoveTag = (tagToRemove: string) => { setFormData((prev) => ({ ...prev, tags: prev.tags.filter((tag) => tag !== tagToRemove), })) } const handleFileUpload = (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (file) { // В реальном приложении здесь был бы загрузка файла на сервер // Пока просто создаем URL для предварительного просмотра const fileUrl = URL.createObjectURL(file) setThumbnailPreview(fileUrl) // В реальном приложении здесь создавался бы объект PersonThumbnail // Пока просто обновляем preview } } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!formData.name.trim()) { return } try { const personData: Partial = { name: formData.name.trim() || undefined, notes: formData.notes.trim() || undefined, tags: formData.tags.length > 0 ? formData.tags : [], thumbnails: formData.thumbnails, } await onSave(personData) onClose() } catch (error) { console.error("Error saving person:", error) // Здесь можно добавить toast с ошибкой } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault() handleAddTag() } } return ( {person ? "Редактировать персону" : "Добавить персону"}
{/* Аватар */}
{thumbnailPreview ? ( Preview ) : ( )}
{thumbnailPreview && ( )}
{/* Имя */}
handleInputChange("name", e.target.value)} placeholder="Введите имя персоны" required />
{/* Примечания */}